Unfortunately, Geoquiz has stopped

#1

Hello.

Every time I run the Geoquiz on the emulator, an error message pops up saying “Unfortunately, Geoquiz has stopped”.

I think it’s got to do something with this error:

Caused by: java.lang.NullPointerException at com.jixtrix.geoquiz.QuizActivity.onCreate(QuizActivity.java:28)

Which says: mTrueButton.setOnClickListener(new View.OnClickListener() {

I don’t get why I get it though.What is the cause for it? Here is all the code:

QuizActivity.java

[code]package com.jixtrix.geoquiz;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.Button;
import android.widget.Toast;

public class QuizActivity extends ActionBarActivity {

private Button mTrueButton;
private Button mFalseButton;

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


    mTrueButton = (Button)findViewById(R.id.true_button);
    mTrueButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(QuizActivity.this,
                    R.string.correct_toast,
                    Toast.LENGTH_SHORT).show();

        }
    });



    mFalseButton = (Button)findViewById(R.id.false_button);
    mFalseButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(QuizActivity.this,
                    R.string.incorrect_toast,
                    Toast.LENGTH_SHORT).show();

        }
    });


    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.quiz, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_quiz, container, false);
        return rootView;
    }
}

}
[/code]

fragment_quiz.xml:

[code]

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="24dp"
    android:text="@string/question_text" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/true_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/true_button"/>

    <Button
        android:id="@+id/false_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/false_button" />



</LinearLayout>
[/code]

activity_quiz.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.jixtrix.geoquiz.QuizActivity" tools:ignore="MergeRootFrame" />

strings.xml:

[code]<?xml version="1.0" encoding="utf-8"?>

GeoQuiz
Constantinople is the largest city in Turkey.
True
False
Correct!
Incorrect!
Settings

[/code]

Can it be due to the fact that I wrote the code in fragment_quiz.xml instead of activity_quiz.xml? Fragments and activities have gotten me mixed up… :question:

Thanks.
-George

#2

This is precisely why you’re getting the error. It was happening to me, too.

So now, I’m trying to figure out how to do this with Fragments, as it seems to be best-practices, now?

#3

I agree, fragments seem to be way better than activities. The thing is I don’t know where exactly to move the code I wrote in Quizactivity.java… that’s what’s confusing me. I can’t read on if I get exceptions every time I compile.

What do I do?

#4

Bump: does anyone know what I can do?

:frowning:

#5

I was having exactly the same problem when attempting to run the Geoquiz app.

By default, Android Studio (I assume Eclipse behaves the same, though I haven’t tried it) sets the build to 4.4 Kit Kat and installs only an emulator at the Kit Kat level. As a workaround to the fragment problem, I went to the SDK Manager and installed a 4.0 Ice Cream Sandwich emulator (which doesn’t support fragments), and started the project over building for 4.0 Ice Cream Sandwich. After completing the project, doing the layout in activity_quiz.xml, GeoQuiz ran perfectly on the 4.0 emulator.

#6

To use this sample with fragments need to change code like this in generated PlaceholderFragment class:

[code]public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_quiz, container, false);

        Button mTrueButton = (Button)rootView.findViewById(R.id.true_button);
        mTrueButton.setOnClickListener(new  View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(), getString(R.string.correct_toast), Toast.LENGTH_SHORT).show();
            }
        });

        Button mFalseButton = (Button)rootView.findViewById(R.id.false_button);
        mFalseButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
               Toast.makeText(v.getContext(), getString(R.string.incorrect_toast), Toast.LENGTH_SHORT).show();
            }
        });

        return rootView;
    }
}

[/code]

#7

Drasius,
A big thank you and +1 for your reply!

#8

Thank you so much, Drasius, for your sample code! Now it works like charm for me! I’m so new to Android programming that I just wanted to get it running on the emulator running KitKat to make sure I’m following correctly so far.

However, the logic is different from the example code given in the book. The buttons are supposed to be private member variables. Besides, when I continue to the next chapter, we are supposed to add more questions in an array, so this won’t work anymore, will it?

Thanks! Any suggestion or help is greatly appreciated!

#9

Presto, you are going to run into problem after problem if you continue down that path. If you are beginner, the one true way is to download the book’s code and change yours until it looks exactly like it – every line in every file. There is nothing wrong with the book’s code. it builds and runs perfectly with the latest build tools. The problem is the newest wizards now are making that fragment and you really need to get rid of it – completely.

Once you do it once, you’ll know how to do it again as needed when the book starts a new project.

#10

Hi all,

I am having the same problem as the OP. I tried the code from Drasius but the problem is still there. I’ve tried created new AVD and creating a new project based on Android 4.0 instead of 4.4 but no good. My code is exactly the same as in the book. Googling this problem for the most part brought me to this thread and another thread. But so far nothing works. Any help would be appreciated!

#11

Never mind. Followed the instructions here and got it to work.

stackoverflow.com/questions/2228 … t-activity

#12

Hi all, I found many beginners troubling with new ADT, so I would like to tell some changes so that it will look exactly the same as book…

  1. Let wizard generate the fragment_quiz.xml and all that stuff.
  2. Now go to res/layout and delete fragment_quiz.xml and activity_quiz.xml
  3. Right click on res/layout and add android xml file and name it activity_quiz.xml and make its root view LinearLayout, now you have layout look like book
  4. Go to src/QuizActivity.java and delete all the code after setContentView(R.layout.activity_quiz)
  5. Validate the syntax. You are done. :wink:
#13

[quote=“samsidx”]Hi all, I found many beginners troubling with new ADT, so I would like to tell some changes so that it will look exactly the same as book…

  1. Let wizard generate the fragment_quiz.xml and all that stuff.
  2. Now go to res/layout and delete fragment_quiz.xml and activity_quiz.xml
  3. Right click on res/layout and add android xml file and name it activity_quiz.xml and make its root view LinearLayout, now you have layout look like book
  4. Go to src/QuizActivity.java and delete all the code after setContentView(R.layout.activity_quiz)
  5. Validate the syntax. You are done. :wink:[/quote]

Thanks samsidx, your method worked like a charm. :smiley:

#14

I ran into this problem just as the original poster did, and I moved the implementation from the book’s location to the fragment method as described, and it worked on the first try.

Thanks for the updated information.

Bob

#15

uncomment mQuestionTextView = (TextView)findViewById(R.id.question_text_view); on function onCreate. on source code sample of 03_ActivityLifecycle