2013-04-16 48 views
0

我有一個對話框,其中顯示兩個edittextboxes。Android:對話框中的空指針異常

enter image description here

當我填寫的字段,然後點擊提交按鈕,我需要保存在數據庫中的值。我正在嘗試獲取在字段中輸入的值,但引發了空指針異常。

我的代碼如下

View v = getLayoutInflater().inflate(R.layout.sample, null); 
      new AlertDialog.Builder(Context) 
        .setTitle(R.string.Details) 
        .setView(v) 
        .setNegativeButton(R.string.Cancel, 
          new DialogInterface.OnClickListener() { 

           @Override 
           public void onClick(DialogInterface dialog, 
             int which) { 
            // TODO Auto-generated method stub 

           } 
          }) 
        .setPositiveButton(R.string.Submit, 
          new DialogInterface.OnClickListener() { 

           @Override 
           public void onClick(DialogInterface dialog, 
             int which) { 
            final EditText txtname=(EditText)findViewById(R.id.editText1); 
            final EditText txtnumber=(EditText)findViewById(R.id.editText2); 
            Save(txtname.getText().toString(),txtnumber.getText().toString()); 
           } 

當我調試和檢查,它顯示了txtName的和txtnumber值空。我哪裏錯了?我使用的是佈局,以顯示領域

佈局,

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<TableRow> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="10dp" 
     android:text="Name" /> 

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="195dp" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="20dp" 
     android:ems="10" 
     android:hint="FirstName LastName" 
     android:inputType="textPersonName" /> 

</TableRow> 

<TableRow> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="10dp" 
     android:text="Phone" /> 

    <EditText 
     android:id="@+id/editText2" 
     android:layout_width="190dp" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="20dp" 
     android:ems="10" 
     android:inputType="number" 
     android:maxLength="10" /> 

</TableRow> 

請幫我解決這個例外!

+1

當你有一個例外,總是提供堆棧跟蹤和線被引用。 – david99world

回答

5

txtnametxtnumber是空

我。Ë

final EditText txtname=(EditText)findViewById(R.id.editText1); 
final EditText txtnumber=(EditText)findViewById(R.id.editText2); 

,當您試圖訪問空引用您將得到NullPointerException

試圖讓他們這樣

final EditText txtname=(EditText)v.findViewById(R.id.editText1); 
final EditText txtnumber=(EditText)v.findViewById(R.id.editText2); 
+0

謝謝!這有幫助。 – Mahe

1

我想你是看錯了地方。嘗試使用:

final View v = getLayoutInflater().inflate(R.layout.sample, null); 

,比

final EditText txtname=(EditText)v.findViewById(R.id.editText1); 
final EditText txtnumber=(EditText)v.findViewById(R.id.editText2); 

找到的EditText在 「V」 的看法。

+1

相同的評論。 v應該是最終被訪問的。 – njzk2

+0

你說得對。編輯我的答案。 – AlexVogel

1

由於您沒有提供完整的代碼和堆棧跟蹤,我正在猜測,但我假設它是正確的: 您正在使用來自活動內容視圖的findViewById獲取編輯文本,並且不是從膨脹的角度來看。 變化:

findViewById(R.id.editText1); 
findViewById(R.id.editText2); 

v.findViewById(R.id.editText1); 
v.findViewById(R.id.editText1); 
+0

除了在這種情況下,v應該是最終可以在此範圍內訪問< – njzk2

+0

是的。您的解決方案更容易 – IncrediApp

1

你需要得到來自dialod的佈局意見

public void onClick(DialogInterface dialog, 
            int which) { 
    final EditText txtname=(EditText)dialog.findViewById(R.id.editText1); 
    final EditText txtnumber=(EditText)dialog.findViewById(R.id.editText2); 
           Save(txtname.getText().toString(),txtnumber.getText().toString()); 
          } 
1

請檢查資源ID也會使原因NPE。

1

findViewById是相對於活動。您的觀點不在活動中,而是在對話中。您需要致電

dialog.findViewById 

從此範圍。