Hi I am trying to create an android application to display images so that you can click on a 'next' button and then the next image shows. So far I have this method
public void buttonClick(){
imgView = (ImageView)findViewById(R.id.imgView);
buttonS = (Button)findViewById(R.id.button);
buttonS.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
current_image_index++;
current_image_index = current_image_index % images.length;
imgView.setImageResource(images[current_image_index]);
}
}
);
}
However when I try to run this, I get a java.lang.OutOfMemoryError when I click the 'next' button twice. Am I doing something wrong? My image array looks like this:
int[] images = {R.drawable.img};
I have tried changing the Manifest file to include
android:largeHeap="true"
This is where I set the variables and call the method above
public class MainActivity extends AppCompatActivity {
private static ImageView imgView;
private static Button buttonS;
private int current_image_index;
int[] images = {R.drawable.dog,R.drawable.img};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonClick();
}
The stack trace is:
08-29 23:17:34.781 15450-15450/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.daniel.firstapp, PID: 15450
java.lang.OutOfMemoryError: Failed to allocate a 2070252108 byte allocation with 16777216 free bytes and 482MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:816)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:637)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1019)
at android.content.res.Resources.loadDrawableForCookie(Resources.java:3778)
at android.content.res.Resources.loadDrawable(Resources.java:3651)
at android.content.res.Resources.getDrawable(Resources.java:1865)
at android.content.Context.getDrawable(Context.java:408)
at android.widget.ImageView.resolveUri(ImageView.java:752)
at android.widget.ImageView.setImageResource(ImageView.java:408)
at com.example.daniel.firstapp.MainActivity$1.onClick(MainActivity.java:40)
at android.view.View.performClick(View.java:5217)
at android.view.View$PerformClick.run(View.java:20983)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6141)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
current_image_indexspecified?