Android app and java activities, issues with onCreate() method and XML

I am trying use the onClick() function in an activity for an android app. So far I have:

public class Activity2 extends Activity implements OnClickListener {

    private ImageButton closeButton;

    public void onCreate(Bundle savedInstanceState){

        Button myButton = (Button) findViewById(;

        this.closeButton = (ImageButton)this.findViewById(;
        this.closeButton.setOnClickListener(new OnClickListener(){
            public void onClick(View v){

    public void onClick(View v) {
        // TODO Auto-generated method stub
        TextView lowerText = (TextView) findViewById(;
        EditText boxText = (EditText) findViewById(;


This is just a stripped down version of my project. When I enter my app as usual in the emualtor, everything works fine. When I click the button to open up this particular activity, everything crashes. I assume the issue lies within the onClick() method. This code was sent to me by my project partner without the XML files so I think thats where the problem lies.

here is my basic XML file that I thought should work:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:layout_width="match_parent" android:layout_height="match_parent"

    <Button android:id="@+id/wowButton" android:layout_width="fill_parent"
        android:layout_height="200dp" android:text="WoW" android:typeface="sans"
        android:background="@drawable/btn_default_normal_red" />




While very basic, I though it should work. Thanks for your help.


you instantiate an ImageButton through this.closeButton = (ImageButton)this.findViewById(; But you don't declare it in the xml file. That's why it's null and throws a NullPointer Exception.

Need Your Help

Set specific error pages exception-handling

In my MVC project i set a Default error page in the web.config