为什么即使没有错误(HELP),我的应用也会崩溃

时间:2019-01-30 13:48:38

标签: java android crash

我试图创建一个测验应用程序(尽管我对使用android studio不太满意),我对代码进行了一些更改,并尝试在android模拟器上运行该应用程序,但我注意到该应用程序不断崩溃并拒绝即使构建应用程序时也没有错误,仍然可以运行。

可能是问题所在,如何使应用程序运行而不会崩溃?

这是我正在从事的第一个项目,并且对Java和android studio的了解有限。

package com.myafrica.africaquiz;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RadioButton;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {

//Getting the ID of the TextView of the question text
TextView TestQuestionID = (TextView) findViewById(R.id.TestQuestion);

//Getting the ID of the TextView of the question answer
RadioButton AnswerA_ID = (RadioButton) findViewById(R.id.AnswerA);
RadioButton AnswerB_ID = (RadioButton) findViewById(R.id.AnswerB);
RadioButton AnswerC_ID = (RadioButton) findViewById(R.id.AnswerC);
RadioButton AnswerD_ID = (RadioButton) findViewById(R.id.AnswerD);

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

public void GetNextQuestion(View view) {
    Question1();

}


    /*
    This part of the code will contain the answer part of this quiz app.
    Each question is a method and that method will be called when the Next 
    button is clicked in the app.
    When the button is clicked the setText() will automatically (re)set the 
    text in the question layout in the XML
     */

public void Question1() {
    TestQuestionID.setText("Which part of Africa can Ghana be located");
    AnswerA_ID.setText(" A: West Africa");
    AnswerB_ID.setText(" B: East Africa");
    AnswerC_ID.setText(" C: Central Africa");
    AnswerD_ID.setText(" D: North Africa");
}
}

这是myXML代码

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ScrollViewLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<RelativeLayout
    android:id="@+id/QuestionViewLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="@string/quiz_progress"
            android:textSize="15sp" />

        <ProgressBar
            style="@style/Widget.AppCompat.ProgressBar.Horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="10dp"
            android:layout_marginTop="12dp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="10dp"
            android:text="Question 1 / 50" />

        <TextView
            android:id="@+id/TestQuestion"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="10dp"
            android:text="How old is james Mother?"
            android:textSize="20sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="125dp"
        android:orientation="vertical">

        <RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <RadioButton
                android:id="@+id/AnswerA"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="10dp"
                android:layout_marginTop="10dp"
                android:padding="5dp"
                android:text=" A: 20" />

            <RadioButton
                android:id="@+id/AnswerB"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="10dp"
                android:layout_marginTop="5dp"
                android:padding="5dp"
                android:text=" B: 50" />

            <RadioButton
                android:id="@+id/AnswerC"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="10dp"
                android:layout_marginTop="5dp"
                android:padding="5dp"
                android:text=" B: 100" />

            <RadioButton
                android:id="@+id/AnswerD"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="10dp"
                android:layout_marginTop="5dp"
                android:padding="5dp"
                android:text=" D: 58" />
        </RadioGroup>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="300dp"
        android:gravity="center"
        android:orientation="horizontal">

        <Button
            android:id="@+id/PreviousButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="20dp"
            android:padding="10dp"
            android:text="@string/Previous"
            android:textSize="15sp" />

        <Button
            android:id="@+id/NextButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="20dp"
            android:onClick="GetNextQuestion"
            android:padding="10dp"
            android:text="@string/Next"
            android:textSize="15sp" />
    </LinearLayout>
    </RelativeLayout> 
 </ScrollView>

这是Logcat中的详细信息

01-30 14:10:28.606 16454-16454/? I/rica.africaqui: Not late-enabling - 
Xcheck:jni (already on)
01-30 14:10:28.648 16454-16454/? W/rica.africaqui: Unexpected CPU variant 
for X86 using defaults: x86
01-30 14:10:28.832 16454-16454/com.myafrica.africaquiz I/rica.africaqui: 
The ClassLoaderContext is a special shared library.
01-30 14:10:28.992 16454-16454/com.myafrica.africaquiz W/rica.africaqui: 
JIT profile information will not be recorded: profile file does not exits.
01-30 14:10:28.995 16454-16454/com.myafrica.africaquiz I/chatty: 
uid=10086(com.myafrica.africaquiz) identical 10 lines
01-30 14:10:28.995 16454-16454/com.myafrica.africaquiz W/rica.africaqui: 
JIT profile information will not be recorded: profile file does not exits.
01-30 14:10:29.026 16454-16454/com.myafrica.africaquiz I/InstantRun: 
starting instant run server: is main process
01-30 14:10:29.252 16454-16454/com.myafrica.africaquiz D/AndroidRuntime: 
Shutting down VM
01-30 14:10:29.271 16454-16454/com.myafrica.africaquiz E/AndroidRuntime: 
FATAL EXCEPTION: main
Process: com.myafrica.africaquiz, PID: 16454
java.lang.RuntimeException: Unable to instantiate activity 

ComponentInfo{com.myafrica.africaquiz/
com.myafrica.africaquiz.MainActivity}: 
java.lang.NullPointerException: Attempt to invoke virtual method 
'android.view.Window$Callback android.view.Window.getCallback()' on a null 
object reference
    at 
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2843)
    at 
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
    at 

android.app.servertransaction.LaunchActivityItem.execute
(LaunchActivityItem.java:78)
    at 
android.app.servertransaction.TransactionExecutor.executeCallbacks
(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute
(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    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.NullPointerException: Attempt to invoke virtual 
method 'android.view.Window$Callback android.view.Window.getCallback()' 
on a null object reference
    at android.support.v7.app.AppCompatDelegateImplBase.<init> 
(AppCompatDelegateImplBase.java:117)
    at android.support.v7.app.AppCompatDelegateImplV9.<init> 
(AppCompatDelegateImplV9.java:149)
    at android.support.v7.app.AppCompatDelegateImplV14.<init> 
(AppCompatDelegateImplV14.java:56)
    at android.support.v7.app.AppCompatDelegateImplV23.<init> 
(AppCompatDelegateImplV23.java:31)
    at android.support.v7.app.AppCompatDelegateImplN.<init> 
(AppCompatDelegateImplN.java:31)
    at 
android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:198)
    at 
android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:183)
    at 
android.support.v7.app.AppCompatActivity.getDelegate
(AppCompatActivity.java:519)
    at android.support.v7.app.AppCompatActivity.findViewById
(AppCompatActivity.java:190)
    at com.myafrica.africaquiz.MainActivity.<init>(MainActivity.java:13)
    at java.lang.Class.newInstance(Native Method)
    at 
 android.app.AppComponentFactory.instantiateActivity
 (AppComponentFactory.java:69)
    at android.app.Instrumentation.newActivity(Instrumentation.java:1215)
    at 
 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2831)
    at 
 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
    at 

 android.app.servertransaction.LaunchActivityItem.execute
 (LaunchActivityItem.java:78) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks
 (TransactionExecutor.java:108) 
    at android.app.servertransaction.TransactionExecutor.execute
 (TransactionExecutor.java:68) 
    at 
 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    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) 
 01-30 14:10:29.450 16454-16465/com.myafrica.africaquiz I/rica.africaqui: 
 Background concurrent copying GC freed 20027(6MB) AllocSpace objects, 
0(0B) 
LOS 
objects, 58% free, 1102KB/2MB, paused 757us total 159.205ms

尽管我还没有完成构建该应用程序的工作,但我还是想对其进行测试,看看在遇到此问题之前会得到什么结果。

1 个答案:

答案 0 :(得分:1)

第一步,您应该将RadioButtons声明为null:

RadioButton AnswerA_ID = null;
RadioButton AnswerB_ID = null;
RadioButton AnswerC_ID = null;
RadioButton AnswerD_ID = null;

然后在OnCreate中应该执行以下操作:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    AnswerA_ID = (RadioButton) findViewById(R.id.AnswerA);
    AnswerB_ID = (RadioButton) findViewById(R.id.AnswerB);
    AnswerC_ID = (RadioButton) findViewById(R.id.AnswerC);
    AnswerD_ID = (RadioButton) findViewById(R.id.AnswerD);
}