我一直收到错误:当我从Main2Activity.java转到Main3Activity.java时,“testapp已停止”[复制]

loading...


-2

这个问题在这里已有答案:

我对编码很陌生,而且我对调试过程并不是很了解 .

至于现在,这是我能够提供的所有信息,我希望有人遇到并有解决这类问题的方法 .

Main2Activity 的代码:

MediaPlayer bmusic;

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

    bmusic = MediaPlayer.create(Main2Activity.this,R.raw.bmapp);
    bmusic.setLooping(true);
    bmusic.start();

    button = findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Main2Activity.this, Main3Activity.class);
            startActivity(intent);

        }
    });
}

@Override
protected void onPause() {
    super.onPause();
    bmusic.release();
    finish();
}

Main2Activity非常好,但是一旦我点击开始游戏按钮,这将导致我进入Main3Activity,弹出一个错误,说“testapp(我的应用程序名称)已经停止 .

Main3Acitivty 代码:

Button b_continue;

TextView tv_question;

EditText et_answer1;

List<Item> questions;

int curQuestion = 0;

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

    b_continue = findViewById(R.id.b_continue);
    tv_question =  findViewById(R.id.tv_question);
    et_answer1 = findViewById(R.id.et_answer1);

    b_continue.setVisibility(View.INVISIBLE);

    questions = new ArrayList<>();
    //add all questions and answers to the game
    for (int i  = 0; 1 < Database.questions.length; i++){
        questions.add(new Item(Database.questions[i], Database.answers[i]));
    }

    //shuffle the questions
    Collections.shuffle(questions);

    tv_question.setText(questions.get(curQuestion).getQuestion());

    et_answer1.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //check if the answer is right
            if(et_answer1.getText().toString().equalsIgnoreCase(questions.get(curQuestion).getAnswer())){
                b_continue.setVisibility(View.VISIBLE);
            } else {
                b_continue.setVisibility(View.INVISIBLE);
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

    b_continue.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (curQuestion < (Database.questions.length - 1)) {
                //next question
                curQuestion++;
                tv_question.setText(questions.get(curQuestion).getQuestion());
                b_continue.setVisibility(View.INVISIBLE);
                et_answer1.setText("");
            } else {
                //no more questions - game over
                Toast.makeText(Main3Activity.this, "You won the game!", Toast.LENGTH_SHORT).show();
                finish();
            }
        }
    });
}

loading...

1回答

  • 0

    AndroidManifest.xml 中声明活动

    像那样

    <activity android:name=".Main3Activity" />
    

    注意:如果隐含错误 .

评论

暂时没有评论!