Android Intent 命令出错
Android error at Intent command
我的代码有问题。当我单击标题 "New Profile" 时,必须启动一个新的 Activity,但是当我单击时,会显示一条警告:“不幸的是 %AppName% 已停止。” .
这是 Android 监视器的日志
--------- beginning of crash
05-17 01:19:54.545 2568-2568/com.example.felipe.myappproject E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.felipe.myappproject, PID: 2568
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.felipe.myappproject/com.example.felipe.myappproject.NewProfileActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
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)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.content.ContextWrapper.getPackageName(ContextWrapper.java:133)
at android.content.ComponentName.<init>(ComponentName.java:128)
at android.content.Intent.<init>(Intent.java:4449)
at com.example.felipe.myappproject.NewProfileActivity.<init>(NewProfileActivity.java:14)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
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)
这是我的 NewProfileActivity.java
public class NewProfileActivity extends AppCompatActivity {
EditText profile, password, confirmpass;
Intent insert = new Intent(NewProfileActivity.this, MainActivity.class);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_profile);
}
public void backMain(View v){
startActivity(back);
}
public void insertData(View v) {
profile = (EditText) findViewById(R.id.profileName);
password = (EditText) findViewById(R.id.password);
confirmpass = (EditText) findViewById(R.id.confirmPassword);
insert.putExtra("profile", profile.getText().toString());
insert.putExtra("password", password.getText().toString());
insert.putExtra("profile", confirmpass.getText().toString());
compare(password.getText().toString(), confirmpass.getText().toString());
}
public void compare(String x, String y){
final AlertDialog alert = new AlertDialog.Builder(NewProfileActivity.this).create();
if(x.equals(y)){
alert.setTitle("New Profile!");
alert.setMessage("Profile "+profile+" registered");
alert.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}
);
startActivity(insert);
} else {
alert.setTitle("Ops...");
alert.setMessage("Passwords don't macth");
alert.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}
);
}
}
}
我找不到我做错了什么。我试图通过删除 "public void backMain(View v)" 以外的所有内容来简化代码,但我仍然遇到同样的问题。
根据@MikeM 的评论,您在一开始就初始化了 Intent
,即使您作为参数传递的 class 也没有完全初始化。
你应该像这样使用它之前简单地初始化它:
public class NewProfileActivity extends AppCompatActivity {
EditText profile, password, confirmpass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_profile);
}
public void backMain(View v) {
// startActivity(back);
}
public void insertData(View v) {
profile = (EditText) findViewById(R.id.profileName);
password = (EditText) findViewById(R.id.password);
confirmpass = (EditText) findViewById(R.id.confirmPassword);
compare(password.getText().toString(), confirmpass.getText().toString());
}
public void compare(String x, String y) {
final AlertDialog alert = new AlertDialog.Builder(NewProfileActivity.this).create();
if (x.equals(y)) {
alert.setTitle("New Profile!");
alert.setMessage("Profile " + profile + " registered");
alert.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}
);
// CODES Transferred here ====
Intent insert = new Intent(NewProfileActivity.this, MainActivity.class);
insert.putExtra("profile", profile.getText().toString());
insert.putExtra("password", password.getText().toString());
insert.putExtra("profile", confirmpass.getText().toString());
// CODES Transferred here ====
startActivity(insert);
} else {
alert.setTitle("Ops...");
alert.setMessage("Passwords don't macth");
alert.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}
);
}
}
}
干杯! :D
我的代码有问题。当我单击标题 "New Profile" 时,必须启动一个新的 Activity,但是当我单击时,会显示一条警告:“不幸的是 %AppName% 已停止。” .
这是 Android 监视器的日志
--------- beginning of crash
05-17 01:19:54.545 2568-2568/com.example.felipe.myappproject E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.felipe.myappproject, PID: 2568
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.felipe.myappproject/com.example.felipe.myappproject.NewProfileActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
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)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.content.ContextWrapper.getPackageName(ContextWrapper.java:133)
at android.content.ComponentName.<init>(ComponentName.java:128)
at android.content.Intent.<init>(Intent.java:4449)
at com.example.felipe.myappproject.NewProfileActivity.<init>(NewProfileActivity.java:14)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
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)
这是我的 NewProfileActivity.java
public class NewProfileActivity extends AppCompatActivity {
EditText profile, password, confirmpass;
Intent insert = new Intent(NewProfileActivity.this, MainActivity.class);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_profile);
}
public void backMain(View v){
startActivity(back);
}
public void insertData(View v) {
profile = (EditText) findViewById(R.id.profileName);
password = (EditText) findViewById(R.id.password);
confirmpass = (EditText) findViewById(R.id.confirmPassword);
insert.putExtra("profile", profile.getText().toString());
insert.putExtra("password", password.getText().toString());
insert.putExtra("profile", confirmpass.getText().toString());
compare(password.getText().toString(), confirmpass.getText().toString());
}
public void compare(String x, String y){
final AlertDialog alert = new AlertDialog.Builder(NewProfileActivity.this).create();
if(x.equals(y)){
alert.setTitle("New Profile!");
alert.setMessage("Profile "+profile+" registered");
alert.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}
);
startActivity(insert);
} else {
alert.setTitle("Ops...");
alert.setMessage("Passwords don't macth");
alert.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}
);
}
}
}
我找不到我做错了什么。我试图通过删除 "public void backMain(View v)" 以外的所有内容来简化代码,但我仍然遇到同样的问题。
根据@MikeM 的评论,您在一开始就初始化了 Intent
,即使您作为参数传递的 class 也没有完全初始化。
你应该像这样使用它之前简单地初始化它:
public class NewProfileActivity extends AppCompatActivity {
EditText profile, password, confirmpass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_profile);
}
public void backMain(View v) {
// startActivity(back);
}
public void insertData(View v) {
profile = (EditText) findViewById(R.id.profileName);
password = (EditText) findViewById(R.id.password);
confirmpass = (EditText) findViewById(R.id.confirmPassword);
compare(password.getText().toString(), confirmpass.getText().toString());
}
public void compare(String x, String y) {
final AlertDialog alert = new AlertDialog.Builder(NewProfileActivity.this).create();
if (x.equals(y)) {
alert.setTitle("New Profile!");
alert.setMessage("Profile " + profile + " registered");
alert.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}
);
// CODES Transferred here ====
Intent insert = new Intent(NewProfileActivity.this, MainActivity.class);
insert.putExtra("profile", profile.getText().toString());
insert.putExtra("password", password.getText().toString());
insert.putExtra("profile", confirmpass.getText().toString());
// CODES Transferred here ====
startActivity(insert);
} else {
alert.setTitle("Ops...");
alert.setMessage("Passwords don't macth");
alert.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}
);
}
}
}
干杯! :D