Mam problem z uzyskaniem danych, które użytkownik wprowadza w dialog
(z niestandardowym układem). Spójrz na kod:Okno dialogowe Androida z niestandardowym układem: zwracanie danych
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>
Kod:
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
otwiera układ stworzyłem, ale
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();
}
});
produkuje błąd wykonania: java.lang.NullPointerException
. Gdzie jest problem?
wysłać logcat a my spojrzeć –