Android 工作室“尝试在空对象引用上调用虚方法”
Android studio “ Attempt to invoke virtual method on a null object reference”
我无法克服这个“尝试在空对象引用上调用虚拟方法”错误。
所以我的 Button
在下面的代码中打开一个 CustomDialog
和一个 EditText
用户可以在其中更新他的电子邮件地址。
btnChangeEmail=(Button)findViewById(R.id.buttonChangeEmail);
btnChangeEmail.setOnClickListener(new OnClickListener() {
@Override
//Change Email Dialog
public void onClick (View arg0){
final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.email_dialog);
//dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
Button dialogButton = (Button) dialog.findViewById(R.id.buttonSave);
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
EditText txtNewEmail = (EditText)findViewById(R.id.editTextNewEmail);
AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setTitle("Are you sure?");
alert.setMessage("Use " + txtNewEmail.getText().toString() + " as your new address?");
alert.setCancelable(false);
alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alertDialog = alert.create();
alertDialog.show();
}
});
Button exitButton = (Button) dialog.findViewById(R.id.buttonExit);
exitButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
Window window = dialog.getWindow();
float h = (float) (getResources().getDisplayMetrics().heightPixels * .9);
float w = (float) (getResources().getDisplayMetrics().widthPixels);
int height = (int) h;
int width = (int) w;
window.setLayout(width, height);
}
}
通过单击上述自定义对话框中的保存按钮,它会打开一个警报,提示用户是否真的想继续并覆盖他注册的电子邮件地址。我清楚地实例化了 txtNewEmail 但总是在它说
的那一行得到空对象引用错误
alert.setMessage("Use " + txtNewEmail.getText().toString() + "作为你的新地址?");
感谢任何帮助。谢谢。
这是我的完整 logcat 错误...
03-05 01:51:57.704 4512-4512/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.twenty, PID: 4512
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.example.twenty.SettingsActivity.onClick(SettingsActivity.java:193)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
删除并添加在 Listener 之外发起的 txtNewEmail
final EditText txtNewEmail = (EditText)findViewById(R.id.editTextNewEmail);
btnChangeEmail=(Button)findViewById(R.id.buttonChangeEmail);
EditText txtNewEmail = (EditText)findViewById(R.id.editTextNewEmail);
此对象为空(txtNewEmail) 由视图父调用此方法(findViewById)
改变
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
和
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
alert.dismiss();
}
});
您在 onClickListener 中的 edittext 也将像这样使用对话框来查找 id
EditText txtNewEmail = (EditText)dialog.findViewById(R.id.editTextNewEmail);
我无法克服这个“尝试在空对象引用上调用虚拟方法”错误。
所以我的 Button
在下面的代码中打开一个 CustomDialog
和一个 EditText
用户可以在其中更新他的电子邮件地址。
btnChangeEmail=(Button)findViewById(R.id.buttonChangeEmail);
btnChangeEmail.setOnClickListener(new OnClickListener() {
@Override
//Change Email Dialog
public void onClick (View arg0){
final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.email_dialog);
//dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
Button dialogButton = (Button) dialog.findViewById(R.id.buttonSave);
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
EditText txtNewEmail = (EditText)findViewById(R.id.editTextNewEmail);
AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setTitle("Are you sure?");
alert.setMessage("Use " + txtNewEmail.getText().toString() + " as your new address?");
alert.setCancelable(false);
alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alertDialog = alert.create();
alertDialog.show();
}
});
Button exitButton = (Button) dialog.findViewById(R.id.buttonExit);
exitButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
Window window = dialog.getWindow();
float h = (float) (getResources().getDisplayMetrics().heightPixels * .9);
float w = (float) (getResources().getDisplayMetrics().widthPixels);
int height = (int) h;
int width = (int) w;
window.setLayout(width, height);
}
}
通过单击上述自定义对话框中的保存按钮,它会打开一个警报,提示用户是否真的想继续并覆盖他注册的电子邮件地址。我清楚地实例化了 txtNewEmail 但总是在它说
的那一行得到空对象引用错误alert.setMessage("Use " + txtNewEmail.getText().toString() + "作为你的新地址?");
感谢任何帮助。谢谢。
这是我的完整 logcat 错误...
03-05 01:51:57.704 4512-4512/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.twenty, PID: 4512
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.example.twenty.SettingsActivity.onClick(SettingsActivity.java:193)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
删除并添加在 Listener 之外发起的 txtNewEmail
final EditText txtNewEmail = (EditText)findViewById(R.id.editTextNewEmail);
btnChangeEmail=(Button)findViewById(R.id.buttonChangeEmail);
EditText txtNewEmail = (EditText)findViewById(R.id.editTextNewEmail);
此对象为空(txtNewEmail) 由视图父调用此方法(findViewById)
改变
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
和
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
alert.dismiss();
}
});
您在 onClickListener 中的 edittext 也将像这样使用对话框来查找 id
EditText txtNewEmail = (EditText)dialog.findViewById(R.id.editTextNewEmail);