當我從EditText使用方法getText()時,我有一個空指針異常。我所在班級位於我的事件從片段(不知道擴展,如果這是此錯誤^^EditText getText拋出空指針異常
這裏的一個原因是我的事件
// EVENT BOUTON Ajouter
buttonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
String nom = ((EditText) v.findViewById(R.id.nom)).getText().toString();
String prenom = ((EditText) v.findViewById(R.id.prenom)).getText().toString();
String numero = ((EditText) v.findViewById(R.id.numero)).getText().toString();
String groupe = ((Spinner) v.findViewById(R.id.groupeSelection)).getSelectedItem().toString();
String ville = ((EditText) v.findViewById(R.id.ville)).getText().toString();
ImageView iv = (ImageView) v.findViewById(R.id.profil_picture);
Bitmap bitmap = ((BitmapDrawable)iv.getDrawable()).getBitmap();
new Contact(nom, prenom, numero, ville, Repertoire.getInstance().getGroupeByName(groupe), bitmap);
} catch (NullPointerException e) {
throw e;
} finally {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.popBackStack();
}
}
});
我用這個XML:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/padding"
android:paddingLeft="@dimen/padding"
android:paddingRight="@dimen/padding"
android:paddingTop="32dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/nom"
android:textSize="@dimen/text"
/>
<EditText
android:id="@+id/nom"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/prenom"
android:textSize="@dimen/text"
/>
<EditText
android:id="@+id/prenom"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/numero"
android:textSize="@dimen/text"
/>
<EditText
android:id="@+id/numero"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/groupe"
android:textSize="@dimen/text"
/>
<Spinner
android:id="@+id/groupeSelection"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/btn_dropdown"
android:spinnerMode="dropdown" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ville"
android:textSize="@dimen/text"
/>
<EditText
android:id="@+id/ville"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
請分享您的NullPointerException – OYRM
可以請您發佈logcat .. – Lal
顯示您在哪裏充氣佈局。 – barq