-3

this is my code for World_Heritage activity,please help me guys.it has three buttons(next,previous,first) and a image view.if i click next button the image should change

public class World_Heritage extends Activity implements View.OnClickListener {

ImageView draw = (ImageView)findViewById(R.id.imageView1);
int a=0;
Button butnnext;

/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.worldheritage);
    Button butnpre = (Button)findViewById(R.id.buttonprevious);
    butnpre.setOnClickListener(this);

    Button butnfir = (Button)findViewById(R.id.buttonfirst);
    butnfir.setOnClickListener(this);

    butnnext = (Button)findViewById(R.id.buttonnext);
    butnnext.setOnClickListener(this);


    // TODO Auto-generated method stub
}

public void onClick(View v)
{
    switch(v.getId())
    {
    case R.id.buttonfirst:
        draw.setImageResource(R.drawable.agra_fort);
        a=0;
        break;

    case R.id.buttonnext:
        if(a==0)
        {
        draw.setImageResource(R.drawable.ajantha_caves);
        a=1;
        }
        else if(a==1)
        {
            draw.setImageResource(R.drawable.bhimbetka);
            a=2;
        }
        else if(a==2)
        {
            draw.setImageResource(R.drawable.champaner);
            a=3;
        }
        else if(a==3)
        {
            draw.setImageResource(R.drawable.chttrabadhi);
            a=4;
        }
        else if(a==4)
        {
            draw.setImageResource(R.drawable.church);
            a=5;
        }
        else if(a==5)
        {
            draw.setImageResource(R.drawable.elephanta);
            a=6;
        }
        else if(a==6)
        {
            draw.setImageResource(R.drawable.ellora_caves);
            draw.setClickable(false);
        }
        break;
    case R.id.buttonprevious:
        a--;
        butnnext.performClick();
        break;
    }

}

}
arvindjd
  • 3
  • 2
  • as i told u in your prev question move findViewById inside onCreate method after setContentView – ρяσѕρєя K Mar 15 '13 at 20:05
  • 1
    Rather than post two incomplete questions, you should have clicked [edit] underneath your previous question to add this new information. This way you would have one _complete_ question. – Sam Mar 15 '13 at 20:09

1 Answers1

1

Change

ImageView draw = (ImageView)findViewById(R.id.imageView1);

to

ImageView draw;

and under

setContentView(R.layout.worldheritage);

put

draw = (ImageView)findViewById(R.id.imageView1);

The reason for this is because your Views such as Buttons, ImagViews, etc... are contained in your Layout. Your layout doesn't exist until you inflate it using setCOntentView() or inflater hence your Views will be null

Also, from now on, when commenters ask you to post code/logcat/anything else, please edit your OP instead of creating a new post. Use the "edit" button below the post

codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • now the button "next" and "first" is working but the button "previous" is not working – arvindjd Mar 15 '13 at 20:13
  • What do you mean "is not working"? Do you get erro/unexpected results/something else? Are you sure `R.id.buttonprevious` is declared in `R.layout.worldheritage` properly? – codeMagic Mar 15 '13 at 20:16
  • when i click next button the image changes,but when i click previous button the image view is not changing. – arvindjd Mar 15 '13 at 20:26
  • That I'm not sure about without debugging and seeing exactly what happens. You may not be able to call the `onClick()` from within itself. You haven't broken out of the `switch` yet – codeMagic Mar 15 '13 at 20:36