Problème récupération de plusieurs champs EditText
Bonjour,
J'ai un problème avec kla récupèration de plusieurs champs EditText. Il em sort une exception.
Voilà ce que j'ai fait :
activity_main.xml
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:fitsSystemWindows="true"
tools:context="fr.laposte.dsic.beacon.MonitoringActivity">
<include layout="@layout/toolbar" />
<include layout="@layout/form_pro" />
</LinearLayout> |
toolbar.xml
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:paddingLeft="10sp"
android:paddingRight="10sp"
android:paddingTop="0sp"
android:paddingBottom="0sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:layout_margin="0dp"
android:padding="0dp">
<!-- <ImageView
android:layout_width="100dp"
android:layout_height="60dp"
android:id="@+id/imageView"
android:scaleType="centerCrop"
android:src="@mipmap/logo_solution_business"/> -->
<TextView
android:id="@+id/txtLastname"
android:text="@string/app_name"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_vertical"
android:maxLines="2"
android:textColor="@color/colorToolbarText"
android:layout_margin="0dp"
android:padding="10sp"/>
</LinearLayout>
</LinearLayout> |
form_pro.xml
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
| <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="15sp"
android:paddingRight="15sp"
android:paddingTop="15sp"
android:paddingBottom="15sp"
tools:context=".MonitoringActivity"
tools:showIn="@layout/activity_main">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="5sp"
android:id="@+id/llIntro">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Merci de renseigner les informations ci-dessous :"
android:textSize="15sp"
android:textStyle="bold"
android:id="@+id/txtIntro"
android:paddingTop="5sp"
android:lines="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="5sp"
android:id="@+id/llLastname">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nom :"
android:textSize="15sp"
android:textStyle="bold"
android:lines="1"
android:maxLines="1" />
<EditText
android:id="@+id/txtLastname"
android:inputType="text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="5sp"
android:id="@+id/llFirstname">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Prénom :"
android:textSize="15sp"
android:textStyle="bold"
android:lines="1"
android:maxLines="1" />
<EditText
android:id="@+id/txtFirstname"
android:inputType="text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="5sp"
android:id="@+id/llEmail">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email :"
android:textSize="15sp"
android:textStyle="bold"
android:lines="1"
android:maxLines="1" />
<EditText
android:id="@+id/txtEmail"
android:inputType="textEmailAddress"
android:ems="10"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="5sp"
android:id="@+id/llActivitySector">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Secteur d'activité :"
android:textSize="15sp"
android:textStyle="bold"
android:lines="1"
android:maxLines="1" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtAS"
android:paddingTop="10sp" />
</LinearLayout>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:paddingTop="10sp"
android:id="@+id/llBtn">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
android:id="@+id/button"
android:onClick="onValidateForm"
android:layout_gravity="center_horizontal" />
</LinearLayout>
</LinearLayout> |
Quand je fais :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| public void onValidateForm(View view) {
Log.d(TAG, "J'ai validé le formulaire...");
Boolean valid = true;
EditText txtLastname = (EditText)view.findViewById(R.id.txtLastname);
EditText txtFirstname = (EditText)view.findViewById(R.id.txtFirstname);
EditText txtEmail = (EditText)view.findViewById(R.id.txtEmail);
Spinner txtAS = (Spinner)view.findViewById(R.id.txtAS);
dialogErrors( txtLastname.getText().toString() );
if( !isStringValidLength(txtLastname.getText().toString()) ) {
valid = false;
dialogErrors("Le nom saisi est erroné");
}
else if( !isStringValidLength(txtFirstname.getText().toString()) ) {
valid = false;
dialogErrors("Le prénom saisi est erroné");
}
//Verification des champs
else if( !isEmailValid(txtEmail.getText().toString()) ) {
valid = false;
dialogErrors("L'email saisi est erroné");
}
[...]
} |
et que j'exécute, il me sort cette erreur :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| FATAL EXCEPTION: main
Process: fr.laposte.dsic.beacon, PID: 26694
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21153)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5204)*
at android.view.View$PerformClick.run(View.java:21153)*
at android.os.Handler.handleCallback(Handler.java:739)*
at android.os.Handler.dispatchMessage(Handler.java:95)*
at android.os.Looper.loop(Looper.java:148)*
at android.app.ActivityThread.main(ActivityThread.java:5417)*
at java.lang.reflect.Method.invoke(Native Method)*
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)*
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)*
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at fr.laposte.dsic.beacon.MonitoringActivity.onValidateForm(MonitoringActivity.java:184)
at java.lang.reflect.Method.invoke(Native Method)*
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)*
at android.view.View.performClick(View.java:5204)*
at android.view.View$PerformClick.run(View.java:21153)*
at android.os.Handler.handleCallback(Handler.java:739)*
at android.os.Handler.dispatchMessage(Handler.java:95)*
at android.os.Looper.loop(Looper.java:148)*
at android.app.ActivityThread.main(ActivityThread.java:5417)*
at java.lang.reflect.Method.invoke(Native Method)*
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)*
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)* |
Avez-vous une piste ?
Merci