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

时间:2017-04-18 04:11:03

标签: android android-layout android-studio

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

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]);


}    

子问题:如果我在onCreate()方法之前的初始化和声明是正确的,我有点麻烦。

编辑:布局文件

<?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>

3 个答案:

答案 0 :(得分:0)

你说你点击然后就崩溃了。因此onclick会出现一些问题。我们需要关注onclick中的代码。上面的代码片段只是显示第一个问题显示,这些是正确的,除了测试标识符没有增加。

请发布该片段的完整课程代码。还要给出logcat错误

我能想到的是一些错误应该在注册活动以显示或错误durimg onclick访问数组的位置。也可能是阵列外围。

答案 1 :(得分:0)

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

注意:: 您的初始化和声明是完美的,您的onClick方法不是问题。这个AppTheme是唯一的问题。告诉我,如果你没有得到我。

答案 2 :(得分:0)

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

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