-1

I was trying to create a quiz app (though i am not too good with using android studio) i made some changes to the code and tried to run the app on the android emulator and i noticed that the app keep crashing and refuses to run despite there is no error when building the app.

What could be the problem and how can i get the app to run without crashing?

This is one of the first project i am working on and have have a limited knowledge of java and 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");
}
}

This is myXML code

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

This is the details from the 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

Though i am not through with building the app, i wanted to test it and see what might be the result before i encountered this problem.

Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
Wami ikechukwu
  • 57
  • 2
  • 10
  • 1
    Most likely this happens because you are trying to `findViewById` before any view is actually created. You should only search for views after you called `setContextView` at least once (or similar). Right now this is not what happens. If you really want to have similar syntax to what you have right now, look at Butterknife library. – M. Prokhorov Jan 30 '19 at 13:55
  • i did try to declare the views variables inside `public class MainActivity extends AppCompatActivity { ...}` and put the `findViewById` next to the `setContentView(R.layout.activity_main)` and the app ran but stop working when i clicked on the next button. – Wami ikechukwu Jan 30 '19 at 14:11
  • That would be another problem entirely, for which we'd need a separate question with proper description. I strongly advise you to search for similar answered questions before asking though, your problem is very unlikely a unique one. – M. Prokhorov Jan 30 '19 at 14:21
  • i made a mistake; i forgot to remove the data type in the variable inside`setContentView(R.layout.activity_main);` since the variable containing the views are already declared with the data type in the `public class MainActivity extends AppCompatActivity` and when i ran the app, it ran without crashing. @KotlinIsland – Wami ikechukwu Jan 30 '19 at 14:27

1 Answers1

1

You should declare the RadioButtons as null in a first step :

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

Then in the OnCreate you should do :

@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);
}
KotlinIsland
  • 799
  • 1
  • 6
  • 25
  • Ok, so issue resolved ? – KotlinIsland Jan 30 '19 at 14:31
  • it did work with or without using the `null` – Wami ikechukwu Jan 30 '19 at 14:33
  • Ok, I thought it was the cause of the issue :) I don't understand why it crashed, can you explain a little more ? Thanks – KotlinIsland Jan 30 '19 at 14:38
  • Actually i believed that, me not declaring a variable outside `protected void onCreate(Bundle savedInstanceState)` made it crash because as i did just as you said and it worked perfectly well, i believe another factor was my limited knowledge in java and android – Wami ikechukwu Jan 30 '19 at 15:00
  • Ok ! Keep up the work on your project :) It seems interesting :) And consider switching to Kotlin language as it is more and more used and simplifies the number of lines of code, and it has a lot of benefits also... – KotlinIsland Jan 30 '19 at 15:28