8

Hello I'm writing a little Android app (Version 2.3.3). Now i get this strange NullPointer Exception in this very basic code:

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.mainmenu); newDeck = (Button) findViewById(R.id.newDeckB); loadDeck = (Button) findViewById(R.id.loadDeckB); viewEdition = (Button) findViewById(R.id.viewEditionB); newDeck.setOnClickListener(this); loadDeck.setOnClickListener(this); viewEdition.setOnClickListener(this); } 

Im using this simple layout at the moment in main menu.xml:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:id="@+id/newDeckB" android:text="New Deck" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <Button android:id="@+id/loadDeckB" android:text="Load Deck" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <Button android:id="@+id/viewEditionB" android:text="View Edition" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <TextView android:id="@+id/currentDeckTextView" android:text="Default Deck" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> 

Now my problem is a nullpointexception in line 25, which is the line where i set the first clickListener

newDeck.setOnClickListener(this); 

Using the debugger i figured out that the button newDeck is null. I searched a lot in the web but the only answer to such kind of problem was to check that the setContentView is set before the findViewById. This is obviously the case here.

I would be very glad for any kind of advice.

Thx in Before!

3
  • Does R.id.newDeckB actually exist in R.layout.mainmenu? Commented Aug 21, 2011 at 10:27
  • I was having the exact same problem (frustrating!), and it was the comment below (@manelizzard) that solved it for me: 'Sometimes you need to "clean" and rebuild the project to get the correct resurces compiled, it's something from Eclipse'. (i.e. you don't need to use onPostCreate() !) Commented Aug 1, 2012 at 14:55
  • Clean & rebuild works... how strange. Commented May 10, 2013 at 4:45

2 Answers 2

10

Get your views and set the listeners in onPostCreate() method.

Sign up to request clarification or add additional context in comments.

7 Comments

Thx a lot for your posts. I don't really understand why but now it works. I just changed the id-Names in both the java code and the xml (from newDeckB to newDeckButton) and now it works. I really don't get it because i tried that already yesterday and it did not solve the problem.
Sometimes you need to "clean" and rebuild the project to get the correct resurces compiled, it's something from Eclipse. The onPostCreate() method is executed when the content is fully loaded. Accept the answer, plz =)
Soory i searched the faq but still don't see a button to accept an answer. Or to set this topic to solved... Sorry i am zoo nooby.
Just to be pedantic, onPostCreate is really meant for framework use, and typically is not expected to be used in applications. Any logic should be contained at the end of onCreate instead.
@manelizzard but im changing the content view after onCreate()!. Getting null with findViewById().
|
-2

There are two events that the App expects, onCreate(), and onStart()

Which one you put this function in, matters.

I had to move "findViewByID" from onCreate() to onStart()

 @Override protected void onStart() { // use findViewById() here instead of in onCreate() } 

2 Comments

Include more info please.
There are two events that the App expects, onCreate(), and onStart() Which one you put this function in, matters.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.