填充布局会动态崩溃应用程序而不会出错



当我尝试通过上一个布局上的一个按钮打开此新活动时,应用程序崩溃了,没有明显的原因。我的代码是:

public class Main3Activity extends AppCompatActivity {

private   int limitsImages[] = {R.drawable.onelimits, R.drawable.twolimits, R.drawable.threelimits, R.drawable.fourlimits, R.drawable.fivelimits, R.drawable.sixlimits,
        R.drawable.sevenlimits, R.drawable.eightlimits, R.drawable.ninelimits, R.drawable.tenlimits};
private   String answersLimits[] = {"-9", "2", "1/10", "1/6", "1/2", "12", "1", "2/3", "5/6", "1/4"};
private String choicesLimits[][] = {{"-19", "19", "-9", "2"},{"0", "-2", "Does not exist", "2"}, {"Does not exist","1/5","0","1/10"},{"1/6","1/5","1/4","1"}
,{"1","1/2","0","Does not exist"},{"0","4","12","10"},{"0","1","1/2","2/3"},{"2/3","1","3/2","0"},{"1","2/6","5/6","4"},{"1/4","1"
        ,"Does not exist","4"}};

private  TextView questionNum;
private  TextView questionTxt;
private  ImageView questionImage;
private  CheckBox choiceOne;
private  CheckBox choiceTwo;
private  CheckBox choiceThree;
private  CheckBox choiceFour;
private static int testIdentifier=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main3);
    questionNum = (TextView) findViewById(R.id.questionNumber);
    questionTxt = (TextView) findViewById(R.id.questionText);
    questionImage = (ImageView) findViewById(R.id.questionImage);
    choiceOne = (CheckBox) findViewById(R.id.choice1);
    choiceTwo = (CheckBox) findViewById(R.id.choice2);
    choiceThree = (CheckBox) findViewById(R.id.choice3);
    choiceFour = (CheckBox) findViewById(R.id.choice4);
    //choicesLimits[4][3]=getString(R.string.doesntexist);
    //choicesLimits[9][2]=getString(R.string.doesntexist);

    startLimitsQuestion(testIdentifier);


}
public void startLimitsQuestion( int questionNum) {
    this.questionNum.setText(questionNum+1+"/10");
    questionTxt.setText(getString(R.string.limitsQ));
    questionImage.setImageResource(limitsImages[questionNum]);
    choiceOne.setText(choicesLimits[questionNum][1]);
    choiceTwo.setText(choicesLimits[questionNum][2]);
    choiceThree.setText(choicesLimits[questionNum][3]);
    choiceFour.setText(choicesLimits[questionNum][4]);

}    

sub问题:如果我的初始化和声明正确((方法正确。

编辑:布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@color/colorPrimaryDark"
    tools:context="com.example.learnmath.Main2Activity"
    >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button4"
        android:background="@color/colorPrimary"
        android:textColor="@android:color/black"
        android:layout_marginTop="27dp"
        android:layout_below="@+id/textView3"
        android:layout_alignLeft="@+id/button5"
        android:layout_alignStart="@+id/button5"
        android:text="@string/quadratics" />
    <Button
        android:text="@string/derivatives"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button6"
        android:background="@color/colorPrimary"
        android:textColor="@android:color/black"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="18dp"
        android:layout_marginEnd="18dp" />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/timetostart"
        android:id="@+id/imageView"
        android:layout_alignTop="@+id/button4"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="22dp"
        android:layout_above="@+id/button6" />
    <Button
        android:text="@string/limits"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button5"
        android:background="@color/colorPrimary"
        android:textColor="@android:color/black"
        android:layout_marginTop="35dp"
        android:layout_below="@+id/button4"
        android:layout_alignLeft="@+id/button6"
        android:layout_alignStart="@+id/button6"
        android:elevation="0dp"
        android:onClick="limitsQuiz" />
    <ImageView
        android:layout_width="200dp"
        android:layout_height="200dp"
        app:srcCompat="@drawable/mathtest"
        android:id="@+id/imageView2"
        android:layout_marginRight="46dp"
        android:layout_marginEnd="46dp"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/button6"
        android:layout_alignEnd="@+id/button6" />
    <TextView
        android:text="@string/tests"
        android:typeface="monospace"
        android:textStyle="italic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/holo_orange_light"
        android:id="@+id/textView3"
        android:textSize="35sp"
        android:shadowColor="@android:color/background_light"
        android:layout_alignParentTop="true"
        android:layout_alignLeft="@+id/imageView2"
        android:layout_alignStart="@+id/imageView2" />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/index"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/imageView3"
        android:onClick="homePage" />
</RelativeLayout>

您说单击然后崩溃。因此,在OnClick中会出现一些问题。我们需要从OnClick遵循您的代码。上面的片段只是显示第一个问题显示,除了测试标识符没有递增外,它们是正确的。

请发布该片段的完整班级代码。也会给出logcat错误

我可以猜到的是,在注册活动中应出现错误以表现出或错误的durimg onclick在您的访问数组中。也可能是阵列。

我已经解决了您的问题。您在 androidManifest.xml 中声明的应用主题不合适。当您在活动中扩展 AppCompatactivity 时,您需要为您的特定活动或应用程序使用 AppCompat 主题。
将当前的应用主题更改为 android:theme="@style/Theme.AppCompat.Light" in androidManifest.xml 文件将解决您的问题。



注意:: 您的初始化和声明非常完美,并且您的onclick方法不是问题。这个apptheme是唯一的问题。告诉我你是否没有得到我。

使用Firebase崩溃库报告崩溃,以检测崩溃,如果您无法在logcate中崩溃

https://firebase.google.com/docs/crash/

最新更新