2016-02-26 197 views
-3

當我從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> 
+1

請分享您的NullPointerException – OYRM

+0

可以請您發佈logcat .. – Lal

+0

顯示您在哪裏充氣佈局。 – barq

回答

1

它的投擲錯誤,因爲它無法找到你的點擊視圖中的文本ID v。

v.findViewById 

會給你看wh ich在按鈕視圖中...

發佈您的整個代碼,以獲得進一步的幫助。但它會返回null,因爲按鈕沒有hv中的任何編輯文本

+0

Ahm ....星期五結束......搜索他爲什麼不能在按鈕視圖中獲得editText的人。是的,我發現我需要保存我的視圖,並使用它來獲取editText值。 – Kpone

+0

sry沒有得到你 –