這裏是我的email_dialog.xml
佈局:NullPointerException異常上的EditText位於對話框
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TableRow
android:id="@+id/tableRow0"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TableRow>
<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/txtEmailAddress5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:text="[email protected]"/>
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/btnCancelEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="200px"
android:text="Cancel" />
<Button
android:id="@+id/btnOkEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="200px"
android:text="Email" />
</TableRow>
這裏是我的方法調用,並使用它:
void showEmailDialog() {
// Final prevents the error in the newest onClick callback.
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.email_dialog);
dialog.setTitle("Enter Email Address");
dialog.setCancelable(true);
final EditText txtEA = (EditText) findViewById(R.id.txtEmailAddress5);
final Button cancelButton = (Button) dialog.findViewById(R.id.btnCancelEmail);
final Button sendButton = (Button) dialog.findViewById(R.id.btnOkEmail);
// set up cancel button
cancelButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
// set up send button
sendButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "sendButton onClick()");
String emailAddress;
Log.d(TAG, "sendButton onClick() - String emailAddress");
Log.d(TAG,
"sendButton onClick() - txtEmailAddress = (EditText)");
emailAddress = txtEA.getText().toString();
Log.d(TAG,
"sendButton onClick() - emailAddress = getText().toString();");
sendEmail(emailAddress);
dialog.dismiss();
}
});
dialog.show();
//
}
TAG
定義正確不用擔心有。 我不斷收到:
txtEA.getText().toString()
拋出空點異常。我有正確的R.id
價值,我驗證了50倍,我確認setContentView()
之前我嘗試訪問EditText
和兩個按鈕與setOnClickListener
完美。
我絕對可以使用另一套眼睛!我挖掘了類似的問題並嘗試了他們的解決方案,但是他們都沒有解決我的問題!
哇,我是盲人。我會整天盯着那個。謝謝你解決我的遺忘! – Falcon165o 2012-03-07 19:14:39