首页 > 解决方案 > 我在 android studio 中的应用程序在模拟器和实际设备中都经常崩溃

问题描述

我的应用程序是一个多项选择测验,它在模拟器和设备上运行,但它经常崩溃。代码似乎没有问题,因为构建很干净。我应该采取什么行动?

这是来自 logcat 错误引用的类...

package com.example.mequizapp;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.Random;

public class mequizapp extends AppCompatActivity {

Button answer1,answer2,answer3,answer4;

TextView score,question;

private Questions mQuestions = new Questions();

private String mAnswer;
private int mScore = 0;
private int mQuestionsLength = mQuestions.mQuestions.length;

Random r;

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

    r = new Random();

    answer1 = (Button) findViewById(R.id.answer1);
    answer2 = (Button) findViewById(R.id.answer2);
    answer3 = (Button) findViewById(R.id.answer3);
    answer4 = (Button) findViewById(R.id.answer4);

    answer1 = (Button) findViewById(R.id.answer1);
    answer1 = (Button) findViewById(R.id.answer1);
    answer1 = (Button) findViewById(R.id.answer1);
    answer1 = (Button) findViewById(R.id.answer1);

    score = (TextView) findViewById(R.id.score);
    question = (TextView) findViewById(R.id.question);

    score.setText("Score: " + mScore);

    updateQuestion(r.nextInt(mQuestionsLength));


    answer1.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view){
            if(answer1.getText() == mAnswer){
                mScore++;
                score.setText("Score: " + mScore);
                updateQuestion(r.nextInt(mQuestionsLength));
            } else {
                gameOver();
            }

        }

    });

    answer2.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view){
            if(answer2.getText() == mAnswer){
                mScore++;
                score.setText("Score: " + mScore);
                updateQuestion(r.nextInt(mQuestionsLength));
            } else {
                gameOver();
            }
        }


    });

    answer3.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view){
            if(answer3.getText() == mAnswer){
                mScore++;
                score.setText("Score: " + mScore);
                updateQuestion(r.nextInt(mQuestionsLength));
            } else {
                gameOver();
            }
        }


    });

    answer4.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view){
            if(answer4.getText() == mAnswer){
                mScore++;
                score.setText("Score: " + mScore);
                updateQuestion(r.nextInt(mQuestionsLength));
            } else {
                gameOver();
            }
        }


    });

    }

    private void updateQuestion(int num){
    question.setText(mQuestions.getQuestion(num));
    answer1.setText(mQuestions.getChoice1(num));
    answer2.setText(mQuestions.getChoice2(num));
    answer3.setText(mQuestions.getChoice3(num));
    answer4.setText(mQuestions.getChoice4(num));

    mAnswer =mQuestions.getCorrectAnswer(num);
    }

    private void gameOver(){
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder     (mequizapp.this);
    alertDialogBuilder
            .setMessage("Wrong! The correct answer is (" + mAnswer + ") ... Your score is " + mScore + " points.")
            .setCancelable(true)
            .setPositiveButton("ok",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            return;
                        }
                    })
            .setNegativeButton("Exit",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            finish();
                        }
                    });
    AlertDialog alertDialog = alertDialogBuilder.create();
    alertDialog.show();

}


}

这是日志猫....

2019-05-25 14:42:06.347 1426-1426/? D/gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 8298496
2019-05-25 14:42:06.353 8724-8741/com.example.mequizapp E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf
2019-05-25 14:42:06.354 8724-8741/com.example.mequizapp E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf
2019-05-25 14:42:06.354 8724-8741/com.example.mequizapp E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824
2019-05-25 14:42:06.355 8724-8741/com.example.mequizapp E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824
2019-05-25 14:42:06.410 8724-8741/com.example.mequizapp D/EGL_emulation: eglMakeCurrent: 0xa8ab17a0: ver 3 0 (tinfo 0xa8a8b8b0)
2019-05-25 14:42:06.786 1664-1685/? I/ActivityManager: Displayed com.example.mequizapp/.MainActivity: +1s622ms (total +4s99ms)
2019-05-25 14:42:06.887 1420-2481/? W/audio_hw_generic: Hardware backing HAL too slow, could only write 0 of 720 frames
2019-05-25 14:42:08.083 1420-1612/? W/audio_hw_generic: Not supplying enough data to HAL, expected position 34910679 , only wrote 34650720
2019-05-25 14:42:09.677 1664-1681/? E/BatteryStatsService: modem info is invalid: ModemActivityInfo{ mTimestamp=0 mSleepTimeMs=0 mIdleTimeMs=0 mTxTimeMs[]=[0, 0, 0, 0, 0] mRxTimeMs=0 mEnergyUsed=0}
2019-05-25 14:42:23.367 1664-1677/? E/memtrack: Couldn't load memtrack module
2019-05-25 14:42:23.367 1664-1677/? W/android.os.Debug: failed to get memory consumption info: -1
2019-05-25 14:42:29.049 6666-8271/? E/WakeLock: GCM_HB_ALARM release without a matched acquire!
2019-05-25 14:42:39.049 1664-1677/? E/memtrack: Couldn't load memtrack module
2019-05-25 14:42:39.049 1664-1677/? W/android.os.Debug: failed to get memory consumption info: -1

标签: android

解决方案


也发布您的 Questions.java 类..

并检查以下内容。

mQuestions.mQuestions.length;

推荐阅读