0

I have just started my first android app. I'm using that Big Nerd Ranch Guide book and doing the first example in the book. Not sure what I am doing wrong. Here is my code and the error details. If anyone can help it would be appreciated.

package com.example.geoquiz2;

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.incorrect_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.correct_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;
    }
}

}

Here are the errors that show up.

07-13 13:42:34.875: D/AndroidRuntime(1107): Shutting down VM
07-13 13:42:34.875: W/dalvikvm(1107): threadid=1: thread exiting with uncaught exception (group=0x41465700)
07-13 13:42:34.895: E/AndroidRuntime(1107): FATAL EXCEPTION: main
07-13 13:42:34.895: E/AndroidRuntime(1107): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.geoquiz2/com.example.geoquiz2.QuizActivity}: java.lang.NullPointerException
07-13 13:42:34.895: E/AndroidRuntime(1107):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
07-13 13:42:34.895: E/AndroidRuntime(1107):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
07-13 13:42:34.895: E/AndroidRuntime(1107):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-13 13:42:34.895: E/AndroidRuntime(1107):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
07-13 13:42:34.895: E/AndroidRuntime(1107):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-13 13:42:34.895: E/AndroidRuntime(1107):     at android.os.Looper.loop(Looper.java:137)
07-13 13:42:34.895: E/AndroidRuntime(1107):     at android.app.ActivityThread.main(ActivityThread.java:5103)
07-13 13:42:34.895: E/AndroidRuntime(1107):     at java.lang.reflect.Method.invokeNative(Native Method)
07-13 13:42:34.895: E/AndroidRuntime(1107):     at java.lang.reflect.Method.invoke(Method.java:525)
07-13 13:42:34.895: E/AndroidRuntime(1107):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
07-13 13:42:34.895: E/AndroidRuntime(1107):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-13 13:42:34.895: E/AndroidRuntime(1107):     at dalvik.system.NativeStart.main(Native Method)
07-13 13:42:34.895: E/AndroidRuntime(1107): Caused by: java.lang.NullPointerException
07-13 13:42:34.895: E/AndroidRuntime(1107):     at com.example.geoquiz2.QuizActivity.onCreate(QuizActivity.java:27)
07-13 13:42:34.895: E/AndroidRuntime(1107):     at android.app.Activity.performCreate(Activity.java:5133)
07-13 13:42:34.895: E/AndroidRuntime(1107):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-13 13:42:34.895: E/AndroidRuntime(1107):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
07-13 13:42:34.895: E/AndroidRuntime(1107):     ... 11 more
  • Which line is line 27? "_Caused by: java.lang.NullPointerException ... at com.example.geoquiz2.QuizActivity.onCreate(QuizActivity.java:27)_" – takendarkk Jul 13 '14 at 18:29

1 Answers1

0

You received a NullPointerException at line 27. If I am right, line 27 is

mTrueButton.setOnClickListener(new View.OnClickListener() {

Please check if mTrueButton is null. I have done not much with Android, but if i remember correctly findViewById can return null. If so in your case, you could look [here].(findViewByID returns null)

Community
  • 1
  • 1
Stefan Freitag
  • 3,578
  • 3
  • 26
  • 33