0

我的 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)

单击这两个按钮时应用程序崩溃。这对我来说是个新问题。如果有人对此有任何想法,请帮助我。

4

3 回答 3

2

尝试使用这个

Intent intent=new Intent(CurrentActivity.this,NextActivity.class);

用 CurrentActivity.this 替换 start.this

于 2012-05-10T04:09:56.840 回答
2

也许您使用的活动和意图名称相同,所以可能会导致问题。所以试试这个:

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);
        }
于 2012-05-10T04:35:51.723 回答
0

尝试

    Intent start = new Intent(getApplicationContext(),game_double.class)

代替

    Intent start = new Intent(start.this,game.class);
于 2012-05-10T04:41:12.567 回答