Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

findViewById returns null

Tags:

android

I have code:

public class HelloWorld extends Activity {

private Button buttonOk;
private Button buttonCancel;

private OnClickListener buttonOkListener = new OnClickListener() {
    public void onClick(View v){
        EditText editText = (EditText)findViewById(R.id.input);

        CharSequence textFromInput = (CharSequence) editText.getText();
        Context context = getApplicationContext();
        int duration = Toast.LENGTH_SHORT;

        Toast toast = Toast.makeText(context,textFromInput,duration);
        toast.show();
    }
};

private OnClickListener buttonCancelListener = new OnClickListener() {
    public void onClick(View v){
        EditText editText = (EditText)findViewById(R.id.input);

        CharSequence textFromInput = (CharSequence) editText.getText();
        Context context = getApplicationContext();
        int duration = Toast.LENGTH_SHORT;

        Toast toast = Toast.makeText(context,textFromInput,duration);
        toast.show();
    }
};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    // ToDo add your GUI initialization code here

    setContentView(R.layout.main);

    buttonOk = (Button)findViewById(R.id.buttonOk);
    buttonCancel = (Button)findViewById(R.id.buttonCancel);

    buttonOk.setOnClickListener(buttonOkListener);
    buttonCancel.setOnClickListener(buttonCancelListener);
}
}

and the layout file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
    android:id="@+id/label"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Wpisz swoje imię:"/>
<EditText
    android:id="@+id/input"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@android:drawable/editbox_background"
    android:layout_below="@id/label"/>
<Button
    android:id="@+id/buttonOk"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/input"
    android:layout_alignParentRight="true"
    android:layout_marginLeft="10dip"
    android:text="OK" />
<Button
    android:id="@+id/buttonCancel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@id/buttonOk"
    android:layout_alignTop="@id/buttonOk"
    android:text="Anuluj" />
</RelativeLayout>

Line with buttonCancel.setOnClickListener(buttonCancelListener); throws an Exception because buttonCancel is null. What i am doing wrong?

like image 939
baniczek Avatar asked May 09 '11 14:05

baniczek


3 Answers

there is no problem with your codes, by right everything should work as per normal.

The most common mistake of encountering null via findViewById() method is when you forgot to call setContentView() or called it for the wrong layout.

I suggest cleaning your project and try again!!!!

like image 98
Rejinderi Avatar answered Nov 15 '22 09:11

Rejinderi


I was having the same trouble, but after cleaning my project and running it again it works perfectly.

like image 21
Anderson Avatar answered Nov 15 '22 07:11

Anderson


I'm not 100% sure but you are calling the findviewbyid's in the class initialisation. I think this code is called before the onCreate method so the view's cannot be found. Initializing the listeners in the oncreate method should work.

like image 21
matsjoe Avatar answered Nov 15 '22 07:11

matsjoe