应用程序已停止Error Logcat错误

时间:2014-02-14 18:40:10

标签: java android indexoutofboundsexception

我是adt和开发java应用程序的新手。但我正在尝试制作一个问答游戏。但是出于某些原因,当我点击某个按钮时,它会随机显示不幸的是你的应用已经停止了。

但问题是,它不会发生在同一个地方。我发生在实际的测验部分)甚至有时在主屏幕上。

我注意到有一些日志猫错误,但由于我是新手,我不知道如何修复它。

对于第一张图片,这是日志cat错误:

02-14 18:22:27.146: E/AndroidRuntime(7346): FATAL EXCEPTION: main
02-14 18:22:27.146: E/AndroidRuntime(7346): java.lang.ArrayIndexOutOfBoundsException: length=5; index=5
02-14 18:22:27.146: E/AndroidRuntime(7346):     at vApp.theperiodictablequiz.QuizActivity.ansGen(QuizActivity.java:131)
02-14 18:22:27.146: E/AndroidRuntime(7346):     at vApp.theperiodictablequiz.QuizActivity$4.onClick(QuizActivity.java:204)
02-14 18:22:27.146: E/AndroidRuntime(7346):     at android.view.View.performClick(View.java:4206)
02-14 18:22:27.146: E/AndroidRuntime(7346):     at android.view.View$PerformClick.run(View.java:17357)
02-14 18:22:27.146: E/AndroidRuntime(7346):     at android.os.Handler.handleCallback(Handler.java:725)
02-14 18:22:27.146: E/AndroidRuntime(7346):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-14 18:22:27.146: E/AndroidRuntime(7346):     at android.os.Looper.loop(Looper.java:137)
02-14 18:22:27.146: E/AndroidRuntime(7346):     at android.app.ActivityThread.main(ActivityThread.java:5041)
02-14 18:22:27.146: E/AndroidRuntime(7346):     at java.lang.reflect.Method.invokeNative(Native Method)
02-14 18:22:27.146: E/AndroidRuntime(7346):     at java.lang.reflect.Method.invoke(Method.java:511)
02-14 18:22:27.146: E/AndroidRuntime(7346):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-14 18:22:27.146: E/AndroidRuntime(7346):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-14 18:22:27.146: E/AndroidRuntime(7346):     at dalvik.system.NativeStart.main(Native Method)

对于第二张图片,这是log cat错误:

02-14 18:22:13.146: E/AndroidRuntime(7138): FATAL EXCEPTION: main
02-14 18:22:13.146: E/AndroidRuntime(7138): java.lang.RuntimeException: Unable to start activity ComponentInfo{vApp.theperiodictablequiz/vApp.theperiodictablequiz.QuizActivity}: java.lang.ArrayIndexOutOfBoundsException: length=5; index=7
02-14 18:22:13.146: E/AndroidRuntime(7138):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
02-14 18:22:13.146: E/AndroidRuntime(7138):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-14 18:22:13.146: E/AndroidRuntime(7138):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-14 18:22:13.146: E/AndroidRuntime(7138):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-14 18:22:13.146: E/AndroidRuntime(7138):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-14 18:22:13.146: E/AndroidRuntime(7138):     at android.os.Looper.loop(Looper.java:137)
02-14 18:22:13.146: E/AndroidRuntime(7138):     at android.app.ActivityThread.main(ActivityThread.java:5041)
02-14 18:22:13.146: E/AndroidRuntime(7138):     at java.lang.reflect.Method.invokeNative(Native Method)
02-14 18:22:13.146: E/AndroidRuntime(7138):     at java.lang.reflect.Method.invoke(Method.java:511)
02-14 18:22:13.146: E/AndroidRuntime(7138):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-14 18:22:13.146: E/AndroidRuntime(7138):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-14 18:22:13.146: E/AndroidRuntime(7138):     at dalvik.system.NativeStart.main(Native Method)
02-14 18:22:13.146: E/AndroidRuntime(7138): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=5; index=7
02-14 18:22:13.146: E/AndroidRuntime(7138):     at vApp.theperiodictablequiz.QuizActivity.ansGen(QuizActivity.java:131)
02-14 18:22:13.146: E/AndroidRuntime(7138):     at vApp.theperiodictablequiz.QuizActivity.onCreate(QuizActivity.java:56)
02-14 18:22:13.146: E/AndroidRuntime(7138):     at android.app.Activity.performCreate(Activity.java:5104)
02-14 18:22:13.146: E/AndroidRuntime(7138):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-14 18:22:13.146: E/AndroidRuntime(7138):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
02-14 18:22:13.146: E/AndroidRuntime(7138):     ... 11 more

如果需要任何代码,请告诉我,我会把它提出来。谢谢你的帮助!

编辑:

这是代码

    package vApp.theperiodictablequiz;

    import java.util.Random;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;

public class QuizActivity extends Activity
{
    Button Answer1;
    Button Answer2;
    Button Answer3;
    Button Answer4;
    TextView Question;
    String correctAnswer="";
    int qindex=0;



    String[] QuestionArray =
    {"HelloBye", "What element has atomic number 1?",
            "What is the second most abundant element on Earth?",
            "Element with symbol Li ?", "Has 4 protons?" };
    String[] AnswerArray =
    { "Enter the Lion","Hydrogen", "Helium", "Lithium", "Beryllium", "Boron", "Carbon",
            "Nitrogen" };




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




        Answer1 = (Button) findViewById(R.id.Answer1);
        Answer2 = (Button) findViewById(R.id.Answer2);
        Answer3 = (Button) findViewById(R.id.Answer3);
        Answer4 = (Button) findViewById(R.id.Answer4);
        Question= (TextView)findViewById(R.id.textView1);
        Answer1.setOnClickListener(Achecker1);
        Answer2.setOnClickListener(Achecker2);
        Answer3.setOnClickListener(Achecker3);
        Answer4.setOnClickListener(Achecker4);



        ansGen();

    }


    public void ansGen()
    {
        int AnsArraylen=AnswerArray.length;     
        Random randomizer=new Random();
        int[] AnsVal=new int[4];
        int numchecker=0;

        for (int x=0;x<4;x+=1)
        {
            int tempVal=randomizer.nextInt((AnsArraylen));

            if (tempVal==AnsVal[0])
            {
                numchecker=1;
            }       
            if (tempVal==AnsVal[1])
            {
                numchecker=1;
            }
            if (tempVal==AnsVal[2])
            {
                numchecker=1;
            }
            if (tempVal==AnsVal[3])
            {
                numchecker=1;
            }

            if (numchecker==1)
            {
                x-=1;
            }

            if (numchecker==0)
            {
                AnsVal[x]=tempVal;
                numchecker=0;
            }
            numchecker=0;
        }
        Answer1.setText(AnswerArray[AnsVal[0]]);
        Answer2.setText(AnswerArray[AnsVal[1]]);
        Answer3.setText(AnswerArray[AnsVal[2]]);
        Answer4.setText(AnswerArray[AnsVal[3]]);
        int qpick=randomizer.nextInt(4);


        if(qpick==0)
        {
            qindex=AnsVal[0];
            correctAnswer=AnswerArray[AnsVal[0]];
        }
        if(qpick==1)
        {
            qindex=AnsVal[1];
            correctAnswer=AnswerArray[AnsVal[1]];
        }
        if(qpick==2)
        {
            qindex=AnsVal[2];
            correctAnswer=AnswerArray[AnsVal[2]];
        }
        if(qpick==3)
        {
            qindex=AnsVal[3];
            correctAnswer=AnswerArray[AnsVal[3]];
        }

        Question.setText(QuestionArray[qindex]);

        //Quesgen(qindex);
    }

    View.OnClickListener Achecker1 = new View.OnClickListener()
    {

        @Override
        public void onClick(View v)
        {
            System.out.println("Button1");
            if(Answer1.getText()==correctAnswer)
            {
                System.out.println("YESYESYESYESYESYESYE1");
                ansGen();
            }
            else
            {
                System.out.println("Fail1");
            }
        }
    };


    View.OnClickListener Achecker2 = new View.OnClickListener()
    {

        @Override
        public void onClick(View v)
        {
            System.out.println("Button2");
            if(Answer2.getText()==correctAnswer)
            {
                System.out.println("YESYESYESYESYESYESYE2");
                ansGen();
            }
            else
            {
                System.out.println("Fail2");
            }
        }
    };

    View.OnClickListener Achecker3 = new View.OnClickListener()
    {

        @Override
        public void onClick(View v)
        {
            System.out.println("Button3");
            if(Answer3.getText()==correctAnswer)
            {
                System.out.println("YESYESYESYESYESYESYE3");
                ansGen();
            }
            else
            {
                System.out.println("Fail3");
            }
        }
    };

    View.OnClickListener Achecker4 = new View.OnClickListener()
    {

        @Override
        public void onClick(View v)
        {
            System.out.println("Button4");
            if(Answer4.getText()==correctAnswer)
            {
                System.out.println("YESYESYESYESYESYESYE4");
                ansGen();
            }
            else
            {
                System.out.println("Fail4");
            }
        }
    };


    public void Quesgen(int qindex)
    {
        Question.setText(QuestionArray[qindex]);
    }




    public void delay()
    {
        for (long x = 1; x < 90000000; x += 1)
        {

        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.quiz, menu);
        return true;
    }

}

1 个答案:

答案 0 :(得分:0)

ArrayIndexOutOfBoundsException是一个异常,当您想从arrat中获取超出数组范围的元素时调用该异常。

例如(获取数组的最后一个元素的正确代码:

int []array = {1,2,3,4,5,6,7,8,9};
int lastElement = array[8];

代码不正确:

int []array = {1,2,3,4,5,6,7,8,9};
int lastElement = array[9];

数组中的迭代从0开始(不像现实生活中那样)

更新1

at vApp.theperiodictablequiz.QuizActivity.ansGen(QuizActivity.java:131)

在131行中调用问题异常