Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Simple Android App (retired 2014) Getting Started with Android Adding an OnClickListener to a Button

Marcus Arvidsson
Marcus Arvidsson
5,731 Points

Developing apps on device

Hi, I'm running these tutorials with my phone hooked up to Eclipse.

I am on the step where we click on the quick-fix on "answerLabel" where we change the modifier to final - doing this makes my app crash onCreate.

03-16 18:01:08.045: E/AndroidRuntime(9293): FATAL EXCEPTION: main (10+ more errors).

Any ideas?

6 Answers

Theodore Kruczek
Theodore Kruczek
8,800 Points

Far from an expert on this stuff, but it looks like you are getting a Null Pointer Exception because you are referencing a text view that is in your fragment.

Try moving the code down to the fragment's onCreateView method, like below, and see if it fixes it for you. With the new SDK, as I understand it, you now have two XML files activity_main and fragment_main. Your activity_main loads you fragment_main inside of it. Your textView should be in the fragment_main and your java code that normally goes in the onCreate now goes inside the onCreateView.

If someone more knowledgable could confirm that it would be helpful, but that is how I made my apps start working again.

public PlaceholderFragment() {
    }

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

    final TextView answerLabel = (TextView) rootView.findViewById(R.id.textView1);

    Button getAnswerButton = (Button) rootView.findViewById(R.id.button1);

    getAnswerButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String answer = "Yes";
            answerLabel.setText(answer);
        }
    });

        return rootView;
    }

(Getting code to display is a nightmare for some reason. Edit: Was using ' not `).

The main change is specifying the view the fragment is acting on, hence the inclusion of "rootView" before findViewById(). Obviously delete the old code from onCreate() and let me know if that fixes it for you.

Theodore Kruczek
Theodore Kruczek
8,800 Points

Would you mind posting your code from inside the onCreate method? It is most likely some minor typo.

Marcus Arvidsson
Marcus Arvidsson
5,731 Points

Hi Theodore, Thank you for your reply!

Here's my code: (For some reason, when I installed Eclipse, tried reinstalling to remove this "issue" - it creates a project called appcompat_v7 just over my newly created project . So for every new project there is this folder. Also as you can see - it generates a whole lot more code upon creation).

<p> package com.example.mynewandroid;

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

public class MainActivity extends ActionBarActivity {

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

    // Declare our View variables and assign them the views from the layout file
    final TextView answerLabel = (TextView) findViewById(R.id.textView1);
    Button getAnswerButton = (Button) findViewById(R.id.button1);

    getAnswerButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String answer = "Yes";
            answerLabel.setText(answer);
        }
    });

    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.main, 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_main, container, false);
        return rootView;
    }
}

}

</p>

Theodore Kruczek
Theodore Kruczek
8,800 Points

I don't see any obvious typos, but the main difference I see is that your are using a newer SDK and it is implementing fragments (I believe Ben is redoing these videos because of that). I am still knew to this, but those fragments caused me a ton of problems.

Can you post those errors you are getting and that should narrow it down (if not to me, for someone more experienced). I have been moving my code that the videos say goes inside onCreate() to the fragment's onCreateView() method, but again I know the newest SDK is causing problems.

Marcus Arvidsson
Marcus Arvidsson
5,731 Points

Hello again, Theodore.

Here's the output from error log:

03-18 13:08:26.175: D/AndroidRuntime(19573): Shutting down VM

03-18 13:08:26.175: W/dalvikvm(19573): threadid=1: thread exiting with uncaught exception (group=0x41ed8ac8)

03-18 13:08:26.175: E/AndroidRuntime(19573): FATAL EXCEPTION: main

03-18 13:08:26.175: E/AndroidRuntime(19573): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.MainActivity}: java.lang.NullPointerException

03-18 13:08:26.175: E/AndroidRuntime(19573):

03-18 13:08:26.175: E/AndroidRuntime(19573):

03-18 13:08:26.175: E/AndroidRuntime(19573):

03-18 13:08:26.175: E/AndroidRuntime(19573):

03-18 13:08:26.175: E/AndroidRuntime(19573):

03-18 13:08:26.175: E/AndroidRuntime(19573):

03-18 13:08:26.175: E/AndroidRuntime(19573):

03-18 13:08:26.175: E/AndroidRuntime(19573):

03-18 13:08:26.175: E/AndroidRuntime(19573):

03-18 13:08:26.175: E/AndroidRuntime(19573):

03-18 13:08:26.175: E/AndroidRuntime(19573):

03-18 13:08:26.175: E/AndroidRuntime(19573):

03-18 13:08:26.175: E/AndroidRuntime(19573): Caused by: java.lang.NullPointerException 03-18 13:08:26.175: E/AndroidRuntime(19573):

03-18 13:08:26.175: E/AndroidRuntime(19573):

03-18 13:08:26.175: E/AndroidRuntime(19573):

03-18 13:08:26.175: E/AndroidRuntime(19573):

03-18 13:08:26.175: E/AndroidRuntime(19573):

Marcus Arvidsson
Marcus Arvidsson
5,731 Points

Hi Theodore,

That worked perfectly! Thank you so much :)

I guess I have a lot of reading about these fragments.