Question

I want to create a little app which shows several pictures and if you click on of it a toast pops and says the name. I cant really figure out why it crashes immediately. LogCat says something like Nullpointerexpection?! Thank you for very much in advance for your help.

  package com.example.housesgot;
  import android.os.Bundle;
  import android.annotation.SuppressLint;
  import android.app.Activity;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.ImageButton;
  import android.widget.Toast;

  @SuppressLint("ShowToast")
  public class MainActivity extends Activity implements OnClickListener 
  {

      ImageButton  imageButton1,imageButton2,imageButton3,imageButton4,imageButton5,imageButton6;
      @Override
      protected void onCreate(Bundle savedInstanceState) 
      {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            imageButton1=(ImageButton)findViewById(R.drawable.house_baratheon);
            imageButton2=(ImageButton)findViewById(R.drawable.house_frey);
            imageButton3=(ImageButton)findViewById(R.drawable.house_greyjoy);
            imageButton4=(ImageButton)findViewById(R.drawable.house_lannister);
            imageButton5=(ImageButton)findViewById(R.drawable.house_stark);
            imageButton6=(ImageButton)findViewById(R.drawable.house_targaryen);
            imageButton1.setOnClickListener(this);
            imageButton2.setOnClickListener(this);
            imageButton3.setOnClickListener(this);
            imageButton4.setOnClickListener(this);
            imageButton5.setOnClickListener(this);
            imageButton6.setOnClickListener(this);
      }


    @SuppressLint("ShowToast")
    @Override
    public void onClick(View v) 
    {
        if(v==imageButton1){
            Toast.makeText(MainActivity.this, R.string.baratheon, Toast.LENGTH_LONG);}
        if(v==imageButton2){
            Toast.makeText(MainActivity.this, R.string.frey, Toast.LENGTH_LONG);}
        if(v==imageButton3){
            Toast.makeText(MainActivity.this, R.string.greyjoy, Toast.LENGTH_LONG);}
        if(v==imageButton4){
            Toast.makeText(MainActivity.this, R.string.lannister, Toast.LENGTH_LONG);}
        if(v==imageButton5){
            Toast.makeText(MainActivity.this, R.string.stark, Toast.LENGTH_LONG);}
        if(v==imageButton6){
            Toast.makeText(MainActivity.this, R.string.targaryen, Toast.LENGTH_LONG);}

     }
}
Was it helpful?

Solution

imageButton1 = (ImageButton) findViewById(R.drawable.house_baretheon);

Notice how the method is called findViewById, so you should ACTUALLY be supplying it the ID that you registered to the image button

You have to first set the content view of your activity

setContentView(R.layout.activity_main);

This will set the screen, and put all those buttons on there, AFTER you have made this call you need to grab a reference to all the imagebuttons.

layout/activity_main.xml should have an imagebutton like this for all the items

<ImageButton
    android:id="@+id/button_house_baretheon"
    android:src="@drawable/house_baretheon"
    ... />

Then grab the reference by calling

imageButton1 = (ImageButton) findViewById(R.id.button_house_baretheon)

The rest of your code is fine.

Edit

Oh wait one more thing, you need to call show() after the makeText() method call, so like this

Toast.makeText(MainActivity.this, R.string.baratheon, Toast.LENGTH_LONG).show();

OTHER TIPS

Since you've posted no logcat, I can't correct your code, but I can give you a working example:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.belfast_map);


      ImageButton ib1,ib2,ib3,ib4,ib5;

       //change your image ID's here
       ib1= (ImageButton) findViewById(R.id.go_to_lagan_screen);
       ib2= (ImageButton) findViewById(R.id.go_to_city);
       ib3= (ImageButton) findViewById(R.id.go_to_university);
       ib4= (ImageButton) findViewById(R.id.go_to_icon_screen);
       ib5= (ImageButton) findViewById(R.id.map_to_home_screen);


       ib1.setOnClickListener(new View.OnClickListener()
       {
          @Override
          public void onClick(View v)
          {
             //change the action here, a toast in your example
             Toast.makeText(MainActivity.this, MainActivity.this.getResources().getString(R.string.my_resource_string), Toast.LENGTH_LONG);

          }
       } );

       ib2.setOnClickListener((new View.OnClickListener()
       {
          @Override
          public void onClick(View v)
          {
             Intent intent1= new Intent (MapScreen.this, CityCentre.class);
             startActivity(intent1);
             //To change body of implemented methods use File | Settings | File Templates.
          }
       }));
       ib3.setOnClickListener((new View.OnClickListener()
       {
          @Override
          public void onClick(View v)
          {
             Intent intent2= new Intent (MapScreen.this, UniversityArea.class);
             startActivity(intent2);
             //To change body of implemented methods use File | Settings | File Templates.
          }
       }));
       ib4.setOnClickListener((new View.OnClickListener()
       {
          @Override
          public void onClick(View v)
          {
             Intent intent3= new Intent (MapScreen.this, TheIcons.class);
             startActivity(intent3);

             //To change body of implemented methods use File | Settings | File Templates.
          }
       }));
       ib5.setOnClickListener((new View.OnClickListener()
       {
          @Override
          public void onClick(View v)
          {
             Intent intent4= new Intent (MapScreen.this, MyActivity.class);
             startActivity(intent4);
             //To change body of implemented methods use File | Settings | File Templates.
          }
       }));
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top