0
我爲AlertDialog創建了一個包含EditText的自定義佈局。 Alertdialog有2個按鈕用於提交,其他用於取消。當我點擊提交時,我想從EditText獲取消息並顯示在Logcat中。當我點擊提交按鈕時,我得到一個NullPointerException。不知何故,我無法獲取消息。請指出我哪裏錯了?無法從自定義AlertDialog中的EditText獲取消息
自定義版式文件:dialog_reportbug
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/reportbug"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/reportbug"
android:inputType="textMultiLine" >
<requestFocus />
</EditText>
</LinearLayout>
這裏是我的java文件中的代碼:
LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.dialog_reportbug, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Bug Report")
.setCancelable(true)
.setView(dialoglayout)
.setPositiveButton("Submit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
EditText report = (EditText) findViewById(R.id.reportbug);
String msg = report.getText().toString();
Log.d("Msg",msg);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// do something
}
});
builder.show();
哇。謝謝。有效。 – 2014-11-02 07:58:19
歡迎你。不要忘記標記爲答案:)你需要每次都膨脹視圖,並且想要使用充氣視圖的組件。 – 2014-11-02 08:01:25