我的Android应用程序在模拟器中运行良好但是当我在手机中安装该应用程序并使用它时。单击切换按钮时,它在第二个活动时崩溃。 代码如下。 dob是一个切换按钮,start是我从那里开始下一个活动的按钮。
dob.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Toggle clicked", Toast.LENGTH_SHORT);
if (dob.isChecked()) {
dob_player=1;
name2.setVisibility(0);
name3.setVisibility(1);
} else {
name2.setVisibility(1);
name3.setVisibility(0);
dob_player=0;
}
}
});
start_game.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(dob_player==1){
username1=name1.getText().toString();
username2=name3.getText().toString();
if(username1.equals("") || username2.equals("")){
Toast.makeText(start.this, "Enter the name of the Players", Toast.LENGTH_SHORT);
AlertDialog.Builder alertbox = new AlertDialog.Builder(start.this);
alertbox.setMessage("Enter the name of the Players");
alertbox.setNeutralButton("Ok",
new DialogInterface.OnClickListener() {
// Click listener on the neutral button of alert box
public void onClick(DialogInterface arg0, int arg1) {
// mp1.stop();
//mp1.release();
}
});
alertbox.show();
}
else{
Bundle bundle=new Bundle();
bundle.putInt("qualifying",right);
Intent start=new Intent(start.this,game_double.class);
start.putExtras(bundle);
startActivityForResult(start, 0);
}
}
else{
username1=name1.getText().toString();
Bundle bundle=new Bundle();
bundle.putInt("qualifying",right);
Intent start=new Intent(start.this,game.class);
start.putExtras(bundle);
startActivityForResult(start, 0);
}
}
});
我的logcat如下。
1.按下切换按钮
时出现05-10 10:52:49.453: E/ERROR(17394): ERROR
05-10 10:52:49.453: E/ERROR(17394): java.lang.NullPointerException
05-10 10:52:49.453: E/ERROR(17394): at com.ktrivia.in.start$5.onClick(start.java:129)
05-10 10:52:49.453: E/ERROR(17394): at android.view.View.performClick(View.java:2485)
05-10 10:52:49.453: E/ERROR(17394): at android.widget.CompoundButton.performClick(CompoundButton.java:99)
05-10 10:52:49.453: E/ERROR(17394): at android.view.View$PerformClick.run(View.java:9080)
05-10 10:52:49.453: E/ERROR(17394): at android.os.Handler.handleCallback(Handler.java:587)
05-10 10:52:49.453: E/ERROR(17394): at android.os.Handler.dispatchMessage(Handler.java:92)
05-10 10:52:49.453: E/ERROR(17394): at android.os.Looper.loop(Looper.java:130)
05-10 10:52:49.453: E/ERROR(17394): at android.app.ActivityThread.main(ActivityThread.java:3687)
05-10 10:52:49.453: E/ERROR(17394): at java.lang.reflect.Method.invokeNative(Native Method)
05-10 10:52:49.453: E/ERROR(17394): at java.lang.reflect.Method.invoke(Method.java:507)
05-10 10:52:49.453: E/ERROR(17394): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
05-10 10:52:49.453: E/ERROR(17394): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
05-10 10:52:49.453: E/ERROR(17394): at dalvik.system.NativeStart.main(Native Method)
2.单击开始按钮
时出现05-10 10:54:24.710: D/AndroidRuntime(17394): Shutting down VM
05-10 10:54:24.710: W/dalvikvm(17394): threadid=1: thread exiting with uncaught exception (group=0x40018578)
05-10 10:54:24.710: E/AndroidRuntime(17394): FATAL EXCEPTION: main
05-10 10:54:24.710: E/AndroidRuntime(17394): java.lang.NullPointerException
05-10 10:54:24.710: E/AndroidRuntime(17394): at com.ktrivia.in.start$6.onClick(start.java:150)
05-10 10:54:24.710: E/AndroidRuntime(17394): at android.view.View.performClick(View.java:2485)
05-10 10:54:24.710: E/AndroidRuntime(17394): at android.view.View$PerformClick.run(View.java:9080)
05-10 10:54:24.710: E/AndroidRuntime(17394): at android.os.Handler.handleCallback(Handler.java:587)
05-10 10:54:24.710: E/AndroidRuntime(17394): at android.os.Handler.dispatchMessage(Handler.java:92)
05-10 10:54:24.710: E/AndroidRuntime(17394): at android.os.Looper.loop(Looper.java:130)
05-10 10:54:24.710: E/AndroidRuntime(17394): at android.app.ActivityThread.main(ActivityThread.java:3687)
05-10 10:54:24.710: E/AndroidRuntime(17394): at java.lang.reflect.Method.invokeNative(Native Method)
05-10 10:54:24.710: E/AndroidRuntime(17394): at java.lang.reflect.Method.invoke(Method.java:507)
05-10 10:54:24.710: E/AndroidRuntime(17394): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
05-10 10:54:24.710: E/AndroidRuntime(17394): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
05-10 10:54:24.710: E/AndroidRuntime(17394): at dalvik.system.NativeStart.main(Native Method)
单击这两个按钮会导致应用程序崩溃。这对我来说是个新概率。如果有人对此有任何想法,请帮助我。
答案 0 :(得分:2)
尝试使用此
Intent intent=new Intent(CurrentActivity.this,NextActivity.class);
用CurrentActivity.this替换start.this
答案 1 :(得分:2)
也许你正在使用Activity和intent name,所以可能导致问题。所以试试这个:
AlertDialog alert =alertbox .create();
alert.show();
else{
Intent intent=new Intent(start.this,game_double.class);
Bundle bundle=new Bundle();
bundle.putInt("qualifying",right);
intent.putExtras(bundle);
startActivityForResult(intent, 0);
}
}
else{
username1=name1.getText().toString();
Intent intent=new Intent(start.this,game.class);
Bundle bundle=new Bundle();
bundle.putInt("qualifying",right);
intent.putExtras(bundle);
startActivityForResult(intent, 0);
}
答案 2 :(得分:0)
试
Intent start = new Intent(getApplicationContext(),game_double.class)
而不是
Intent start = new Intent(start.this,game.class);