android应用程序在加载新意图时崩溃

时间:2019-03-23 13:34:40

标签: java android android-intent

我正在制作一个Android应用,您可以练习乘法。您从1个低谷10中选择一个数字,然后得到10个问题。之后,您将被重定向回菜单。但是在此之后,如果您再执行10组问题,则应用程序将在最后一个问题之后崩溃。

package com.example.rick2.rekentuin_native;

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

public class OefenenActivity extends AppCompatActivity {

int index = 0;
int intArray[] = new int[]{1,2,3,4,5,6,7,8,9,10};
TextView tv2;
String sessionId;

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

    sessionId = getIntent().getStringExtra("EXTRA_SESSION_ID");
    TextView tv1 = findViewById(R.id.textView6);
    tv1.setText(sessionId);



    tv2 = findViewById(R.id.textView5);
    tv2.setText(Integer.toString(intArray[index]));
}

public void CheckAns(View view) {
    EditText edit = findViewById(R.id.editText4);
    Integer result = Integer.parseInt(edit.getText().toString());
    int number1 = Integer.parseInt(sessionId);

    if (result == intArray[index] * number1) {
        if (index < 10) {
            index++;
            tv2.setText(Integer.toString(intArray[index]));
            edit.setText("");
        }
        else{
            Intent intent2 = new Intent(this, MenuOefenActivity.class);
            startActivity(intent2);
        }
    }
    else{
        edit.setText("");
    }
}

}

logcat确实给了我这个错误,但是我找不到任何不起作用的onclick。

2019-03-23 13:49:44.066 24510-24510/com.example.rick2.rekentuin_native E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.rick2.rekentuin_native, PID: 24510
java.lang.IllegalStateException: Could not execute method for android:onClick
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
    at android.view.View.performClick(View.java:6597)
    at android.view.View.performClickInternal(View.java:6574)
    at android.view.View.access$3100(View.java:778)
    at android.view.View$PerformClick.run(View.java:25885)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
 Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
    at android.view.View.performClick(View.java:6597) 
    at android.view.View.performClickInternal(View.java:6574) 
    at android.view.View.access$3100(View.java:778) 
    at android.view.View$PerformClick.run(View.java:25885) 
    at android.os.Handler.handleCallback(Handler.java:873) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:193) 
    at android.app.ActivityThread.main(ActivityThread.java:6669) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
 Caused by: java.lang.ArrayIndexOutOfBoundsException: length=10; index=10
    at com.example.rick2.rekentuin_native.OefenenActivity.CheckAns(OefenenActivity.java:40)
    at java.lang.reflect.Method.invoke(Native Method) 
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) 
    at android.view.View.performClick(View.java:6597) 
    at android.view.View.performClickInternal(View.java:6574) 
    at android.view.View.access$3100(View.java:778) 
    at android.view.View$PerformClick.run(View.java:25885) 
    at android.os.Handler.handleCallback(Handler.java:873) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:193) 
    at android.app.ActivityThread.main(ActivityThread.java:6669) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 

2 个答案:

答案 0 :(得分:1)

该错误位于您的活动的第40行:tv2.setText(Integer.toString(intArray[index]));

原因是您增加了元素索引以提取之前获得请求的数组。因此,您有两个选择:

a)用index初始化-1字段

OR

b)重新排列行:

  tv2.setText(Integer.toString(intArray[index]));
  index++;

代替

  index++;
  tv2.setText(Integer.toString(intArray[index]));

答案 1 :(得分:0)

您的代码:

Intent intent2 = new Intent(this, MenuOefenActivity.class);

尝试这个:

Intent intent2 = new Intent(OefenenActivity.this, MenuOefenActivity.class);

我希望它可以帮助:-)