0

Consider:

package com.example.practicealpha;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity implements OnClickListener {
    Button next,previous;
    ImageView image;
    Integer[] id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        id[0] = R.drawable.aa;
        id[1] = R.drawable.bb;
        id[2] = R.drawable.cc;
        id[3] = R.drawable.dd;

        next=(Button)findViewById(R.id.buttonNext);
        previous=(Button)findViewById(R.id.buttonPrevious);
        image=(ImageView)findViewById(R.id.imageView1);
        // image.setImageDrawable(id[0]);
        // image.setImageResource(id[i]);
        /*
            next.setOnClickListener(new Button.OnClickListener(){
                public void onClick(View v)
                {
                    if (i<=3)
                    {
                        i++;
                        image.setImageResource(id[i]);
                        if(i==4)
                            next.setEnabled(false);
                    }
                }
            });
            previous.setOnClickListener(new Button.OnClickListener(){
                public void onClick(View v)
                {
                    if(i>=1)
                    {
                        i--;
                        image.setImageResource(id[i]);
                        if(i==0)
                            next.setEnabled(false);
                    }
                }
            });
       */
    }

    @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);
        return true;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }
}

id[0] = R.drawable.aa;

This line is causing my app to stop forcefully. How can I fix this problem?

I am trying to intialise an integer array with images id and then trying to go through the image listed in drawable folder.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pardeep Singh
  • 412
  • 3
  • 14
  • http://stackoverflow.com/questions/3355220/android-how-can-i-make-a-drawable-array and another way http://stackoverflow.com/questions/6945678/android-storing-r-drawable-ids-in-xml-array – Raghunandan Dec 29 '13 at 14:07

3 Answers3

3

Change

Integer[] id;

to

int[] id;
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nitin Misra
  • 4,472
  • 3
  • 34
  • 52
0

You have to initialize the ids for the size:

int[] id = new int[size];
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
balaji koduri
  • 1,321
  • 9
  • 25
0

You cannot use the id array directly. Initialise it first:

id[0] = R.drawable.aa;
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sush
  • 3,864
  • 2
  • 17
  • 35