getIntent().getExtras() 上的空指针;
Nullpointer on getIntent().getExtras();
RegisterActivity1 中的代码:
Bundle extras = new Bundle();
...
case R.id.continue_button:
extras.putString("email",eMail_eingabe.getText().toString().trim());
extras.putString("pw1", passwort_1_eingabe.getText().toString().trim());
extras.putString("pw2", passwort_2_eingabe.getText().toString().trim());
i.putExtras(extras);
this.startActivity(i);
break;
RegisterActivity2 中的代码:
Bundle extras = getIntent().getExtras(); //Nullpointer oocurs here
semail = extras.getString("email");
spw1 = extras.getString("pw1");
spw2 = extras.getString("pw2");
我正在尝试将电子邮件和密码从 activity 1 传递到 activity 2,但到目前为止我所做的一切似乎都没有奏效。我总是在这里得到一个空指针异常:
Bundle extras = getIntent().getExtras();
关于如何解决这个问题的任何提示?
这是 Activity1 的完整方法,如果它与覆盖方法有关...
@Override
public void onClick(View view) {
String email = eMail_eingabe.getText().toString().trim();
String password = passwort_1_eingabe.getText().toString().trim();
if (eMail_eingabe.getText().toString().isEmpty()) {
//eMail_eingabe.setError("Bitte email eingeben");
eMail_eingabe.setText("Bitte email eingeben");
eMail_eingabe.requestFocus();
return;
}
if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
eMail_eingabe.setError("Bitte eine gültige E-Mail eingeben");
eMail_eingabe.requestFocus();
return;
}
if (password.length() < 6) {
passwort_1_eingabe.setError("Bitte mindestens 6 Zeichen eingeben");
passwort_1_eingabe.requestFocus();
return;
} else if (passwort_1_eingabe.getText().toString().isEmpty()) {
passwort_1_eingabe.setError("Bitte passwort eingeben");
passwort_1_eingabe.requestFocus();
return;
} else if (passwort_2_eingabe.getText().toString().isEmpty()) {
passwort_2_eingabe.setError("Bitte passwort eingeben");
passwort_2_eingabe.requestFocus();
return;
}
if (!passwort_1_eingabe.getText().toString().equalsIgnoreCase(passwort_2_eingabe.getText().toString())) {
passwort_2_eingabe.setError("Passwort stimmt nicht überein");
passwort_2_eingabe.requestFocus();
return;
}
switch (view.getId()) {
case R.id.continue_button:
extras.putString("email", eMail_eingabe.getText().toString().trim());
extras.putString("pw1", passwort_1_eingabe.getText().toString().trim());
extras.putString("pw2", passwort_2_eingabe.getText().toString().trim());
i.putExtras(extras);
this.startActivity(i);
break;
}
}
异常:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.hoimi, PID: 7812
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.hoimi/com.example.hoimi.student.Register2_Student_Activity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3355)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3614)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:86)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2199)
at android.os.Handler.dispatchMessage(Handler.java:112)
at android.os.Looper.loop(Looper.java:216)
at android.app.ActivityThread.main(ActivityThread.java:7625)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
at com.example.hoimi.student.Register2_Student_Activity.<init>(Register2_Student_Activity.java:22)
at java.lang.Class.newInstance(Native Method)
at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:69)
at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:41)
at android.app.Instrumentation.newActivity(Instrumentation.java:1224)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3340)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3614)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:86)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2199)
at android.os.Handler.dispatchMessage(Handler.java:112)
at android.os.Looper.loop(Looper.java:216)
at android.app.ActivityThread.main(ActivityThread.java:7625)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
当您调用 getIntent()
时,它 returns 您在之前 Activity 中创建的意图。
public Intent getIntent ()
Return the intent that started this activity.
所以第二个 activity,你的意图是 null
在 onCreate
中启动您的意图
Bundle extras = getIntent().getExtras();
必须在onCreate方法中调用,我之前调用过所以没用
有人给出了正确的答案,但后来删除了他的post(因为他得到了 2 个反对票??)
总之,非常感谢。
问题不是因为 getExtras()
,而是因为 getIntent()
。用 null 检查你的 getIntent() 并进一步使用。
您的代码应该类似于
if(getIntent() != null) {
Bundle extras = getIntent().getExtras(); //Nullpointer oocurs here
semail = extras.getString("email");
spw1 = extras.getString("pw1");
spw2 = extras.getString("pw2");
}
祝你好运。
RegisterActivity1 中的代码:
Bundle extras = new Bundle();
...
case R.id.continue_button:
extras.putString("email",eMail_eingabe.getText().toString().trim());
extras.putString("pw1", passwort_1_eingabe.getText().toString().trim());
extras.putString("pw2", passwort_2_eingabe.getText().toString().trim());
i.putExtras(extras);
this.startActivity(i);
break;
RegisterActivity2 中的代码:
Bundle extras = getIntent().getExtras(); //Nullpointer oocurs here
semail = extras.getString("email");
spw1 = extras.getString("pw1");
spw2 = extras.getString("pw2");
我正在尝试将电子邮件和密码从 activity 1 传递到 activity 2,但到目前为止我所做的一切似乎都没有奏效。我总是在这里得到一个空指针异常:
Bundle extras = getIntent().getExtras();
关于如何解决这个问题的任何提示?
这是 Activity1 的完整方法,如果它与覆盖方法有关...
@Override
public void onClick(View view) {
String email = eMail_eingabe.getText().toString().trim();
String password = passwort_1_eingabe.getText().toString().trim();
if (eMail_eingabe.getText().toString().isEmpty()) {
//eMail_eingabe.setError("Bitte email eingeben");
eMail_eingabe.setText("Bitte email eingeben");
eMail_eingabe.requestFocus();
return;
}
if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
eMail_eingabe.setError("Bitte eine gültige E-Mail eingeben");
eMail_eingabe.requestFocus();
return;
}
if (password.length() < 6) {
passwort_1_eingabe.setError("Bitte mindestens 6 Zeichen eingeben");
passwort_1_eingabe.requestFocus();
return;
} else if (passwort_1_eingabe.getText().toString().isEmpty()) {
passwort_1_eingabe.setError("Bitte passwort eingeben");
passwort_1_eingabe.requestFocus();
return;
} else if (passwort_2_eingabe.getText().toString().isEmpty()) {
passwort_2_eingabe.setError("Bitte passwort eingeben");
passwort_2_eingabe.requestFocus();
return;
}
if (!passwort_1_eingabe.getText().toString().equalsIgnoreCase(passwort_2_eingabe.getText().toString())) {
passwort_2_eingabe.setError("Passwort stimmt nicht überein");
passwort_2_eingabe.requestFocus();
return;
}
switch (view.getId()) {
case R.id.continue_button:
extras.putString("email", eMail_eingabe.getText().toString().trim());
extras.putString("pw1", passwort_1_eingabe.getText().toString().trim());
extras.putString("pw2", passwort_2_eingabe.getText().toString().trim());
i.putExtras(extras);
this.startActivity(i);
break;
}
}
异常:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.hoimi, PID: 7812
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.hoimi/com.example.hoimi.student.Register2_Student_Activity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3355)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3614)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:86)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2199)
at android.os.Handler.dispatchMessage(Handler.java:112)
at android.os.Looper.loop(Looper.java:216)
at android.app.ActivityThread.main(ActivityThread.java:7625)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
at com.example.hoimi.student.Register2_Student_Activity.<init>(Register2_Student_Activity.java:22)
at java.lang.Class.newInstance(Native Method)
at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:69)
at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:41)
at android.app.Instrumentation.newActivity(Instrumentation.java:1224)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3340)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3614)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:86)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2199)
at android.os.Handler.dispatchMessage(Handler.java:112)
at android.os.Looper.loop(Looper.java:216)
at android.app.ActivityThread.main(ActivityThread.java:7625)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
当您调用 getIntent()
时,它 returns 您在之前 Activity 中创建的意图。
public Intent getIntent ()
Return the intent that started this activity.
所以第二个 activity,你的意图是 null
在 onCreate
中启动您的意图
Bundle extras = getIntent().getExtras();
必须在onCreate方法中调用,我之前调用过所以没用
有人给出了正确的答案,但后来删除了他的post(因为他得到了 2 个反对票??)
总之,非常感谢。
问题不是因为 getExtras()
,而是因为 getIntent()
。用 null 检查你的 getIntent() 并进一步使用。
您的代码应该类似于
if(getIntent() != null) {
Bundle extras = getIntent().getExtras(); //Nullpointer oocurs here
semail = extras.getString("email");
spw1 = extras.getString("pw1");
spw2 = extras.getString("pw2");
}
祝你好运。