-1

Hi I am create some imagebuttons and then adding some on click listeners but I am getting this null pointer exception error. I have included the logcat as well if that is more helpful.Any advice guys ? Many thanks!

     import android.app.Activity;
     import android.content.Intent;
     import android.os.Bundle;
     import android.util.Log;
     import android.view.Menu;
     import android.view.MenuItem;
     import android.view.View;
     import android.view.View.OnClickListener;
     import android.widget.ImageButton;

    public class menuloader extends Activity {
private final String TAG = "Main Activity";
ImageButton imgButton1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.menulayout);
    addButtonListener1();
    addButtonListener2() ;
    addButtonListener3() ;
    addButtonListener4() ;
}
@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);
    Log.i(TAG,"OnCreate");
    return true;
}

public void addButtonListener1() {

    imgButton1 = (ImageButton) findViewById(R.id.imageBtn1);
    imgButton1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getApplicationContext(), 
          bars.class);
            startActivity(intent);
        }
    });
}
    public void addButtonListener2() {

        imgButton1 = (ImageButton) findViewById(R.id.imageButton2);
        imgButton1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getApplicationContext(), 
              ListViews.class);
                startActivity(intent);
            }
        });
}
    public void addButtonListener3() {

        imgButton1 = (ImageButton) findViewById(R.id.imageButton3);
        imgButton1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getApplicationContext(),
     barsandrest.class);
                startActivity(intent);
            }
        });
}
    public void addButtonListener4() {

        imgButton1 = (ImageButton) findViewById(R.id.imageButton4);
        imgButton1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getApplicationContext(),    
           events.class);
                startActivity(intent);
            }
        });
   }
         }

LOGCAT

 03-09 22:20:23.173: E/AndroidRuntime(8267): Caused by: java.lang.NullPointerException
 03-09 22:20:23.173: E/AndroidRuntime(8267):    at 
 com.example.demomap.menuloader.addButtonListener1(menuloader.java:54)
 03-09 22:20:23.173: E/AndroidRuntime(8267):    at com.example.demomap.menuloader.onCreate  
 (menuloader.java:23)
 03-09 22:20:23.173: E/AndroidRuntime(8267):    at android.app.Activity.performCreate       
 (Activity.java:5133)
 03-09 22:20:23.173: E/AndroidRuntime(8267):    at  
 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
 03-09 22:20:23.173: E/AndroidRuntime(8267):    at 
 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
Nikolas
  • 51
  • 1
  • 2
  • 8

2 Answers2

0

It can't find R.id.imageBtn1, so findViewById is returning null. Then when you try to set the onClickListener it throws an exception. You can avoid the exception by adding in a null check, but to fix the actual problem you need to make sure the id is in the activity's view.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • Hey thank your for your reply. I made sure that imageBtn1 is in the activity. That was my first thought as well but it seems that something else is wrong. – Nikolas Mar 09 '14 at 22:58
  • I changed the name of the image button and it gives me an error saying that the (new name of the button) cannot be resolved or is not a field. any advice ? – Nikolas Mar 09 '14 at 23:01
0

If you have done everything right and still throws the exception, try Project -> Clean...

Similar post

Community
  • 1
  • 1
IvanRF
  • 7,115
  • 5
  • 47
  • 71
  • all of the files do not recognise the R.layout .... you just ruined my whole app!!! – Nikolas Mar 09 '14 at 23:12
  • Read this: [post](http://stackoverflow.com/questions/3896301/r-java-disappears-after-project-clean). Cleaning a project removes all auto-generated files. Building the project it should automatically create them. When you Clean a project, there's an option to start a build immediately after clean up. – IvanRF Mar 09 '14 at 23:18
  • If you have the option "Build Automatically" checked under the "Project" menu, the project should auto-generate the files in the gen folder after the clean up. In that folder is located the R.java file. [Here](http://stackoverflow.com/questions/885009/r-cannot-be-resolved-android-error) there is a lot of info. – IvanRF Mar 09 '14 at 23:31
  • I build it and It is still the same any advice ? – Nikolas Mar 09 '14 at 23:32
  • 1
    Sometimes Eclipse complains that a file, e.g. R.layout.main cannot be found. Check in your source code that you did not import android.R. An android.R import will prevent Eclipse from finding your R file. – IvanRF Mar 09 '14 at 23:33