2
Button btnEditor;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    btnEditor = (Button) findViewById(R.id.btnEditor);

    //some code

    btnEditor.setOnClickListener(new View.OnClickListener(){
        public void onClick(View arg0) {

        }
    });
}

btnEditor.setOnClickListener(new View.OnClickListener() gives me a Null Pointer Exception. btnEditor is earlier connecter to XML Button by: btnEditor = (Button) findViewById(R.id.btnEditor);

In my main.xml file:

<Button
        android:id="@+id/btnEditor"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="16dp"
        android:text="Editor"
        android:textSize="48dp"
        android:textStyle="bold"
        android:typeface="normal" android:layout_gravity="bottom"/>

Seriously, I have no idea what to do...

RESOLVED

I forgot that I had two main.xml files:

  • /res/layout
  • /res/layout-large

One of them (in large dir) didn't contain a Button inside, so I got an error while running application on device using large layout.

5
  • 1
    what is the result of (Button) findViewById(R.id.btnEditor);. Is it null? Commented Nov 2, 2013 at 23:07
  • Where is the line btnEditor = (Button) findViewById(R.id.btnEditor); located? Can you post more code? Commented Nov 2, 2013 at 23:07
  • @Szymon I edited main post to show how it is located. Commented Nov 2, 2013 at 23:15
  • Do you have any other @+id/btnEditor in your xml? Commented Nov 2, 2013 at 23:18
  • No, I don't. It's the only one. Commented Nov 2, 2013 at 23:21

3 Answers 3

1

Most likely you haven't called setContentView() with the layout that this Button is in or else you haven't called setContentView() before this line

btnEditor = (Button) findViewById(R.id.btnEditor);

either of these situations would give a NPE at that line and would be the only reason for it. If you think you are then please post how you are doing this.

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

7 Comments

I have called setContentView() to main.xml where my button is. I edited first post to show it.
Try cleaning your project ("Project --> Clean...") and see if this line returns null btnEditor = (Button) findViewById(R.id.btnEditor);
Project cleaned: Caused by: java.lang.NullPointerException at com.piotr.mygame.Main.onCreate(Main.java:54) 54 is btnEditor.setOnClickListener(new View.OnClickListener()
Have you set a breakpoint there to see if btnEditor is null? It must be and the only reason is that this Button is not actually in the xml in setContentView(), you call setContentView() again before setting the listener, or you re-define the Button.
I set a breakpoint after this line and it's how you say: btnEditor is null.
|
0

try to do next:

  1. btn.setOnclickListener(this);
  2. Your class name implemented OnclickListener
  3. Add Method OnClick
  4. @Override public void onClick(View v) { switch (v.getId()) { case R.id.R.id.btnEditor: your code break; }}

1 Comment

This does nothing to address the issue at hand
0

write this as global variable

Button btnEditor ;

in your on create() write

btnEditor = (Button) findViewById(R.id.btnEditor);
btnEditor.setOnClickListener(new View.OnClickListener(){
        public void onClick(View arg0) {

        }
    });

1 Comment

That's exactly how I'm doing it. I forget to include in first post global variable, but I use it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.