ciao a tutti!
sono nuovo nel mondo della programmazione android ed ho provato a seguire qualche tutorial per creare un'applicazione di prova per iniziare dalle basi!!
ho creato una semplice app nella quale inserisci nome e cognome e ti restituisce in una nuova activity i valori inseriti, il problema è che quando premo il tasto invia l'app va in crash e non riesco a capirne il motivo!
vi posto il codice, magari potete aiutarmi!
src/MainActivity.java
1package com.example.helloapi8;
2
3import java.text.Normalizer.Form;
4
5import android.app.Activity;
6import android.content.Intent;
7import android.os.Bundle;
8import android.view.View;
9import android.view.View.OnClickListener;
10import android.widget.Button;
11import android.widget.EditText;
12
13public class MainActivity extends Activity implements OnClickListener{
14
15
16public void onCreate(Bundle savedInstanceState) {
17super.onCreate(savedInstanceState);
18setContentView(R.layout.activity_main);
19final Button button = (Button) findViewById(R.id.form_button);
20button.setOnClickListener(this);
21}
22
23public void onClick(View v) {
24//recuperiamo i dati dalla form
25final EditText edit_name = (EditText)findViewById(R.id.edit_name);
26final EditText edit_lastname = (EditText)findViewById(R.id.edit_lastname);
27//creiamo un oggetto bundle ovvero un associazione nome-dati
28Bundle bundle = new Bundle();
29//mettiamo i dati recuperati nell'oggetto bundle
30bundle.putString("name", edit_name.getText().toString());
31bundle.putString("lastname", edit_lastname.getText().toString());
32//creiamo l'intento per la chiamata dell'activity Form.class
33Intent intent = new Intent(this, Form.class);
34//associamo all'intento il bundle
35intent.putExtras(bundle);
36//lanciamo l'activity
37startActivity(intent);
38}
39}
40
res/layout/activity_main.xml
601<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2xmlns:tools="http://schemas.android.com/tools"
3android:layout_width="match_parent"
4android:layout_height="match_parent" >
5
6<TextView
7android:id="@+id/lbltop"
8android:layout_width="wrap_content"
9android:layout_height="wrap_content"
10android:layout_alignParentTop="true"
11android:layout_centerHorizontal="true"
12android:layout_marginTop="16dp"
13android:text="@string/lbltop" />
14
15<TextView
16android:id="@+id/lblnome"
17android:layout_width="wrap_content"
18android:layout_height="wrap_content"
19android:layout_marginTop="42dp"
20android:text="@string/lblnome" />
21
22<EditText
23android:id="@+id/edit_name"
24android:layout_width="wrap_content"
25android:layout_height="wrap_content"
26android:layout_alignParentLeft="true"
27android:layout_below="@+id/lblnome"
28android:ems="10" >
29
30<requestFocus />
31</EditText>
32
33<TextView
34android:id="@+id/lblcognome"
35android:layout_width="wrap_content"
36android:layout_height="wrap_content"
37android:layout_alignParentLeft="true"
38android:layout_below="@+id/edit_name"
39android:layout_marginTop="14dp"
40android:text="@string/lblcognome" />
41
42<EditText
43android:id="@+id/edit_lastname"
44android:layout_width="wrap_content"
45android:layout_height="wrap_content"
46android:layout_alignParentLeft="true"
47android:layout_below="@+id/lblcognome"
48android:ems="10" />
49
50<Button
51android:id="@+id/form_button"
52android:layout_width="wrap_content"
53android:layout_height="wrap_content"
54android:layout_below="@+id/edit_lastname"
55android:layout_centerHorizontal="true"
56android:layout_marginTop="22dp"
57android:text="@string/form_button" />
58
59</RelativeLayout>
60
/src/Form.java
221package com.example.helloapi8;
2
3import android.app.Activity;
4import android.os.Bundle;
5import android.widget.TextView;
6
7public class Form extends Activity {
8
9
10public void onCreate(Bundle savedInstanceState) {
11super.onCreate(savedInstanceState);
12setContentView(R.layout.activity_form);
13final TextView text_name = (TextView) findViewById(R.id.lblvnome2);
14final TextView text_lastname = (TextView) findViewById(R.id.lblvcognome2);
15Bundle bundle = this.getIntent().getExtras();
16text_name.setText(bundle.getString("name"));
17text_lastname.setText(bundle.getString("lastname"));
18}
19
20
21}
22
res/activity/activity_form.xml
431<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2xmlns:tools="http://schemas.android.com/tools"
3android:layout_width="match_parent"
4android:layout_height="match_parent" >
5
6<TextView
7android:id="@+id/lblvnome"
8android:layout_width="wrap_content"
9android:layout_height="wrap_content"
10android:layout_alignParentLeft="true"
11android:layout_alignParentTop="true"
12android:layout_marginTop="20dp"
13android:text="@string/lblvnome" />
14
15<TextView
16android:id="@+id/lblvnome2"
17android:layout_width="wrap_content"
18android:layout_height="wrap_content"
19android:layout_alignParentLeft="true"
20android:layout_below="@+id/lblvnome"
21android:layout_marginTop="16dp"
22android:text="@string/lblvnome2" />
23
24<TextView
25android:id="@+id/lblvcognome"
26android:layout_width="wrap_content"
27android:layout_height="wrap_content"
28android:layout_alignParentLeft="true"
29android:layout_below="@+id/lblvnome2"
30android:layout_marginTop="15dp"
31android:text="@string/lblvcognome" />
32
33<TextView
34android:id="@+id/lblvcognome2"
35android:layout_width="wrap_content"
36android:layout_height="wrap_content"
37android:layout_alignParentLeft="true"
38android:layout_below="@+id/lblvcognome"
39android:layout_marginTop="20dp"
40android:text="@string/lblvcognome2" />
41
42</RelativeLayout>
43
res/values/Strings.xml
171<resources>
2
3<string name="app_name">Applicazione Prova</string>
4<string name="menu_settings">Impostazioni</string>
5<string name="title_activity_main">Applicazione</string>
6<string name="lblnome">Inserisci il tuo nome</string>
7<string name="lblcognome">Inserisci il tuo cognome</string>
8<string name="lbltop">Premere invio per inviare i dati</string>
9<string name="form_button">Invia</string>
10<string name="hello_world">Hello world!</string>
11<string name="title_activity_form">Form</string>
12<string name="lblvnome">Nome inserito</string>
13<string name="lblvnome2"></string>
14<string name="lblvcognome">Cognome Inserito</string>
15<string name="lblvcognome2"></string>
16
17</resources>
AndroidManifest.xml
341<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2package="com.example.helloapi8"
3android:versionCode="1"
4android:versionName="1.0" >
5
6<uses-sdk
7android:minSdkVersion="16"
8android:targetSdkVersion="15" />
9
10<application
11android:icon="@drawable/ic_launcher"
12android:label="@string/app_name"
13android:theme="@style/AppTheme" >
14<activity
15android:name=".MainActivity"
16android:label="@string/title_activity_main" >
17<intent-filter>
18<action android:name="android.intent.action.MAIN" />
19
20<category android:name="android.intent.category.LAUNCHER" />
21</intent-filter>
22</activity>
23<activity
24android:name=".Form"
25android:label="@string/title_activity_form" >
26<intent-filter>
27<action android:name="android.intent.action.MAIN" />
28
29<category android:name="android.intent.category.LAUNCHER" />
30</intent-filter>
31</activity>
32</application>
33
34</manifest>
una volta premuto il tasto avvio purtroppo l'applicazione va in crash e non capisco perchè...potete aiutarmi?
grazie!!