Dialog 实例中的 EditText 为空
EditText is null in Dialog instance
我首先弹出一个对话框 运行 并提示用户在 editText 视图中输入 his/her 用户名,但每当我尝试通过 editText.getText();
访问它的文本时我得到一个 NullPointerException 说 editText 是 null.What 我在这里失踪了吗?我尝试扩充布局并从 Activity 访问 .getText()
方法,但什么也没有。这是我的代码:
public class UsernameDialog extends DialogFragment {
@BindView(R.id.editText) EditText mEditText;
@BindView(R.id.username)
TextView menu_usr;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// inflate the layout using the dialog themed context
final Context context = getActivity();
final LayoutInflater inflater = LayoutInflater.from(context);
final View view = inflater.inflate(R.layout.username_dialog,null,false);
DialogInterface.OnClickListener posListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
menu_usr.setText(mEditText.getText());
Log.d("USERNAME",mEditText.getText().toString());
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
.setTitle("Choose your username")
.setView(view)
.setPositiveButton("OK",posListener);
return builder.create();
}
}
错误日志:
Process: com.dcv.spdesigns.dokkancards, PID: 373
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.dcv.spdesigns.dokkancards.ui.UsernameDialog.onClick(UsernameDialog.java:48)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:177)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
添加
Butterknife.bind(this,view)
我首先弹出一个对话框 运行 并提示用户在 editText 视图中输入 his/her 用户名,但每当我尝试通过 editText.getText();
访问它的文本时我得到一个 NullPointerException 说 editText 是 null.What 我在这里失踪了吗?我尝试扩充布局并从 Activity 访问 .getText()
方法,但什么也没有。这是我的代码:
public class UsernameDialog extends DialogFragment {
@BindView(R.id.editText) EditText mEditText;
@BindView(R.id.username)
TextView menu_usr;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// inflate the layout using the dialog themed context
final Context context = getActivity();
final LayoutInflater inflater = LayoutInflater.from(context);
final View view = inflater.inflate(R.layout.username_dialog,null,false);
DialogInterface.OnClickListener posListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
menu_usr.setText(mEditText.getText());
Log.d("USERNAME",mEditText.getText().toString());
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
.setTitle("Choose your username")
.setView(view)
.setPositiveButton("OK",posListener);
return builder.create();
}
}
错误日志:
Process: com.dcv.spdesigns.dokkancards, PID: 373
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.dcv.spdesigns.dokkancards.ui.UsernameDialog.onClick(UsernameDialog.java:48)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:177)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
添加
Butterknife.bind(this,view)