Tôi gặp sự cố khi nhận dữ liệu, người dùng đó nhập vào dialog
(với bố cục tùy chỉnh). Nhìn vào mã:Hộp thoại Android có bố cục tùy chỉnh: trả lại dữ liệu
unit.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</EditText>
</RelativeLayout>
mã:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
builder.setView(inflater.inflate(R.layout.unit, null));
AlertDialog dialog = builder.create();
dialog.show();
Dialog
mở ra với bố trí Tôi đã tạo ra, nhưng điều đó
builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
EditText text = (EditText)findViewById(R.id.editText1);
Toast.makeText(getApplicationContext(), text.getText(), Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
sản xuất lỗi thời gian chạy: java.lang.NullPointerException
. Vấn đề ở đâu?
gửi LogCat của bạn và chúng tôi sẽ xem xét –