【发布时间】:2019-01-30 13:48:38
【问题描述】:
我正在尝试创建一个测验应用程序(尽管我不太擅长使用 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
虽然我还没有完成应用程序的构建,但我想在遇到这个问题之前对其进行测试并看看结果可能是什么。
【问题讨论】:
-
这很可能是因为您在实际创建任何视图之前尝试
findViewById。您应该只在至少调用一次(或类似的)setContextView之后搜索视图。现在这不是发生的事情。如果您真的想拥有与现在类似的语法,请查看 Butterknife 库。 -
我确实尝试在
public class MainActivity extends AppCompatActivity { ...}中声明视图变量,并将findViewById放在setContentView(R.layout.activity_main)旁边,当我单击下一个按钮时应用程序运行但停止工作。 -
这完全是另一个问题,为此我们需要一个单独的问题并进行适当的描述。我强烈建议您在提问之前搜索类似的已回答问题,但您的问题不太可能是一个独特的问题。
-
我搞错了;我忘记删除变量 inside
setContentView(R.layout.activity_main);中的数据类型,因为包含视图的变量已经使用public class MainActivity extends AppCompatActivity中的数据类型声明,当我运行应用程序时,它运行时没有崩溃。 @KotlinIsland