无法在Android上的两个活动之间切换

时间:2016-04-02 18:44:22

标签: android android-studio

我的应用程序在我的设备上启动它时会出现“应用程序不幸停止”的错误。

主要活动:

package com.example.shah.stone_paper_scissor;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;  
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    ((Button) findViewById(R.id.easy_button)).setOnClickListener(new         View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent intent = new Intent(MainActivity.this, Easy.class);
            startActivity(intent);

        }
    });

}
}

次要活动:

package com.example.shah.stone_paper_scissor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.Random;

public class Easy extends AppCompatActivity {

private Random random = new Random();
private int user_score = 0;
private int computer_score = 0;
private TextView user_score_text;
private TextView comp_score_text;
private TextView winner;


public void winner(String win){
    winner = ((TextView) findViewById(R.id.winner));
    winner.setText(win + " WINS");
    findViewById(R.id.stone_button).setEnabled(false);
    findViewById(R.id.paper_button).setEnabled(false);
    findViewById(R.id.scissors_button).setEnabled(false);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    findViewById(R.id.stone_button).setEnabled(false);
    findViewById(R.id.paper_button).setEnabled(false);
    findViewById(R.id.scissors_button).setEnabled(false);
    findViewById(R.id.reset).setEnabled(false);

    ((Button) findViewById(R.id.stone_button)).setOnClickListener(new  View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int r = (random.nextInt(9)%3 + 1+4);
            int r_id = getResources().getIdentifier("stone" + 1, "drawable", getApplication().getPackageName());
            ((ImageView) findViewById(R.id.user_choice)).setImageResource(r_id);
            int p_id = getResources().getIdentifier("stone" + r, "drawable", getApplication().getPackageName());
            ((ImageView) findViewById(R.id.comp_choice)).setImageResource(p_id);
            findViewById(R.id.start_button).setEnabled(false);
             }
        }
    });


    ((Button) findViewById(R.id.reset)).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            user_score = 0;
            user_score_text = ((TextView) findViewById(R.id.user_score_text));
            user_score_text.setText(""+user_score);
            comp_score_text = ((TextView) findViewById(R.id.comp_score_text));
            computer_score=0;
            comp_score_text.setText(""+0);
            findViewById(R.id.paper_button).setEnabled(true);
            findViewById(R.id.scissors_button).setEnabled(true);
            findViewById(R.id.stone_button).setEnabled(true);
            findViewById(R.id.start_button).setEnabled(true);
            winner = ((TextView) findViewById(R.id.winner));

            winner.setText("");
        }
    });

    ((Button) findViewById(R.id.start_button)).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            user_score = 0;
            user_score_text = ((TextView) findViewById(R.id.user_score_text));
            user_score_text.setText(""+user_score);
            comp_score_text = ((TextView) findViewById(R.id.comp_score_text));
            computer_score=0;
            comp_score_text.setText(""+0);
            findViewById(R.id.paper_button).setEnabled(true);
            findViewById(R.id.scissors_button).setEnabled(true);
            findViewById(R.id.reset).setEnabled(true);
            findViewById(R.id.stone_button).setEnabled(true);
            winner = ((TextView) findViewById(R.id.winner));
            winner.setText("");
        }
    });

 }
 }

logcat的

     04-03 00:23:14.263 27552-27570/? I/Adreno-EGL:      <qeglDrvAPI_eglInitialize:379>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_LA.BR.1.1.3_RB1.05.01.00.032.017_msm8916_64_LA.BR.1.1.3_RB1__release_AU (Iac7c2e2837)
                                           OpenGL ES Shader Compiler     Version: E031.25.03.04
                                           Build Date: 06/11/15 Thu
                                           Local Branch: mybranch10882158
                                           Remote Branch: quic/LA.BR.1.1.3_rb1.16
                                           Local Patches: NONE
                                           Reconstruct Branch: AU_LINUX_ANDROID_LA.BR.1.1.3_RB1.05.01.00.032.017 + 26a3cba + 6f69ea6 + 8bc2bc8 + 649fcde + a52cccf + dbf281f + 15f0bf8 + 8d02f76 + 9b2cb1a + 25f3b04 + 7cd8c84 + b54906e + 675fd74 + 7c22ef4 +  NOTHING
04-03 00:23:14.264 27552-27570/? I/OpenGLRenderer: Initialized EGL, version 1.4
04-03 00:23:14.275 27552-27570/? D/OpenGLRenderer: Enabling debug mode 0
04-03 00:23:14.347 27552-27552/? I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@21c17ad9 time:13226974
04-03 00:23:19.210 27552-27552/com.example.shah.stone_paper_scissor I/Timeline: Timeline: Activity_launch_request id:com.example.shah.stone_paper_scissor time:13231837
04-03 00:23:19.255 27552-27552/com.example.shah.stone_paper_scissor D/AndroidRuntime: Shutting down VM
04-03 00:23:19.256 27552-27552/com.example.shah.stone_paper_scissor E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                       Process: com.example.shah.stone_paper_scissor, PID: 27552
                                                                                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.shah.stone_paper_scissor/com.example.shah.stone_paper_scissor.Easy}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setEnabled(boolean)' on a null object reference
                                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2358)
                                                                                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420)
                                                                                      at android.app.ActivityThread.access$900(ActivityThread.java:154)
                                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                      at android.os.Looper.loop(Looper.java:135)
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:5292)
                                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                                      at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
                                                                                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setEnabled(boolean)' on a null object reference
                                                                                      at com.example.shah.stone_paper_scissor.Easy.onCreate(Easy.java:35)
                                                                                      at android.app.Activity.performCreate(Activity.java:5990)
                                                                                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
                                                                                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420) 
                                                                                      at android.app.ActivityThread.access$900(ActivityThread.java:154) 
                                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) 
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                      at android.os.Looper.loop(Looper.java:135) 
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:5292) 
                                                                                      at java.lang.reflect.Method.invoke(Native Method) 
                                                                                      at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) 
                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699) 

2 个答案:

答案 0 :(得分:0)

您在R.layout.activity_main活动中使用Easy作为布局,其中不包含观看次数R.id.stone_buttonR.id.paper_buttonR.id.scissors_button和{{1} }。这导致错误。将R.id.reset更改为您为“简单”活动设计的相应布局。这将解决您的错误

答案 1 :(得分:0)

此错误是由于此部分:

    findViewById(R.id.stone_button).setEnabled(false);
findViewById(R.id.paper_button).setEnabled(false);
findViewById(R.id.scissors_button).setEnabled(false);

也许在你的第二个活动中,小部件不存在