anurag145 anurag145 - 1 month ago 18
Android Question

Using onclicklistener dynamic image display

This is my code for creating an app which displays next image when clicked on, I am having trouble creating a dynamic display, it crashes on clicking the image.

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
/>




java file :

public class MainActivity extends AppCompatActivity {

private ImageView im1;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
im1 = (ImageView) findViewById(R.id.imageView);
im1.setImageResource(R.drawable.l);
im1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
im1.setImageResource(R.drawable.m);
}
});
}

}


PS:originally in my XML file I was using
android:background="@drawable/l"

to set my image at start


log: 01-10 19:07:09.912
5465-5465/experiment.com.example.anurag.imageswap2 E/AndroidRuntime:
FATAL EXCEPTION: main
Process: experiment.com.example.anurag.imageswap2, PID: 5465
java.lang.OutOfMemoryError: Failed to allocate a 74649612 byte
allocation with 4194304 free bytes and 28MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
at
android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
at
android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1080)
at
android.content.res.Resources.loadDrawableForCookie(Resources.java:2635)
at android.content.res.Resources.loadDrawable(Resources.java:2540)
at android.content.res.Resources.getDrawable(Resources.java:806)
at android.content.Context.getDrawable(Context.java:458)
at
android.support.v4.content.ContextCompatApi21.getDrawable(ContextCompatApi21.java:26)
at
android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:321)
at
android.support.v7.widget.TintManager.getDrawable(TintManager.java:175)
at
android.support.v7.widget.TintManager.getDrawable(TintManager.java:168)
at
android.support.v7.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:51)
at
android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:72)
at
experiment.com.example.anurag.imageswap2.MainActivity$1.onClick(MainActivity.java:24)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Answer

Try moving your drawables from lower MPI to HDPI or to even higher dimension folders like XHDPI depending on your resolution.

Comments