3

I havee an android activity with two buttons, and a ViewFlipper. The two buttons simply flips to next and previous on the viewflipper.

I want to have exactly 38 Images to switch between, and so I have added them as ImageViews inside the ViewFlipper in the activity's xml file.

For some reason, it seems like the device has a memory limit, that is exceeded when i add more than 20-something imageviews. It works perfectly with 21, but not with 25. The images are only between 50-150 kb, so I don't really understand why a ASUS Transformer Pad TF300T shouldn't be able to handle it. Here is the java-code

import dk.fourway.divepool.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ViewFlipper;

public class FlipperFourActivity extends Activity {


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Set main.XML as the layout for this Activity
    setContentView(R.layout.activity_flipper_four);

    // Set the listener for Button_Next, a quick and dirty way to create a listener
    Button buttonNext = (Button) findViewById(R.id.Button_next);
    buttonNext.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
        flipNext(view);
        }
    });

    // Set the listener for Button_Previous, a quick and dirty way to create a listener
    Button buttonPrevious = (Button) findViewById(R.id.Button_previous);
    buttonPrevious.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            flipPrevious(view);
        }
    });        
}

private void flipNext(View view){
    // Get the ViewFlipper from the layout
    ViewFlipper vf = (ViewFlipper) findViewById(R.id.flipper);

    // Set an animation from res/anim: I pick push left in
    vf.setOutAnimation(AnimationUtils.loadAnimation(view.getContext(), R.anim.push_left_out));
    vf.setInAnimation(AnimationUtils.loadAnimation(view.getContext(), R.anim.slide_left));
    vf.showNext();
}

private void flipPrevious(View view){
    // Get the ViewFlipper from the layout
    ViewFlipper vf = (ViewFlipper) findViewById(R.id.flipper);
    // Set an animation from res/anim: I pick push left out
    vf.setOutAnimation(AnimationUtils.loadAnimation(view.getContext(), R.anim.push_right_out));
    vf.setInAnimation(AnimationUtils.loadAnimation(view.getContext(), R.anim.slide_right));
    vf.showPrevious();
}

}

and here the xml, simplyfied, as there are many almost identical ImageViews

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff"
    android:orientation="vertical" >

    <ViewFlipper
        android:id="@+id/flipper"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/CockpitLayout"
        android:layout_weight="3" >

        <ImageView
            android:id="@+id/foura"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:contentDescription="@string/fourway"
            android:src="@drawable/a" />

        <ImageView
            android:id="@+id/fourb"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:contentDescription="@string/fourway"
            android:src="@drawable/b" />

         <ImageView
            android:id="@+id/fourc"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:contentDescription="@string/fourway"
            android:src="@drawable/c" />

    ...//I want 35 more of these
     </ViewFlipper>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#CCCCCC"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/Button_previous"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="prev" />

        <Button
            android:id="@+id/Button_next"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="next" />
    </LinearLayout>

</LinearLayout>

finally the error message:

05-23 22:04:03.830: E/AndroidRuntime(16152): FATAL EXCEPTION: main
05-23 22:04:03.830: E/AndroidRuntime(16152): java.lang.RuntimeException: Unable to start activity ComponentInfo{dk.fourway.divepool/dk.fourway.divepool.notebook.FlipperFourActivity}: android.view.InflateException: Binary XML file line #173: Error inflating class <unknown>
05-23 22:04:03.830: E/AndroidRuntime(16152):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
05-23 22:04:03.830: E/AndroidRuntime(16152):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
05-23 22:04:03.830: E/AndroidRuntime(16152):    at android.app.ActivityThread.access$600(ActivityThread.java:130)
05-23 22:04:03.830: E/AndroidRuntime(16152):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
05-23 22:04:03.830: E/AndroidRuntime(16152):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-23 22:04:03.830: E/AndroidRuntime(16152):    at android.os.Looper.loop(Looper.java:137)
05-23 22:04:03.830: E/AndroidRuntime(16152):    at android.app.ActivityThread.main(ActivityThread.java:4745)
05-23 22:04:03.830: E/AndroidRuntime(16152):    at java.lang.reflect.Method.invokeNative(Native Method)
05-23 22:04:03.830: E/AndroidRuntime(16152):    at java.lang.reflect.Method.invoke(Method.java:511)
05-23 22:04:03.830: E/AndroidRuntime(16152):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
05-23 22:04:03.830: E/AndroidRuntime(16152):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-23 22:04:03.830: E/AndroidRuntime(16152):    at dalvik.system.NativeStart.main(Native Method)
05-23 22:04:03.830: E/AndroidRuntime(16152): Caused by: android.view.InflateException: Binary XML file line #173: Error inflating class <unknown>
05-23 22:04:03.830: E/AndroidRuntime(16152):    at android.view.LayoutInflater.createView(LayoutInflater.java:613)
05-23 22:04:03.830: E/AndroidRuntime(16152):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
05-23 22:04:03.830: E/AndroidRuntime(16152):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
05-23 22:04:03.830: E/AndroidRuntime(16152):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
05-23 22:04:03.830: E/AndroidRuntime(16152):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
05-23 22:04:03.830: E/AndroidRuntime(16152):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
05-23 22:04:03.830: E/AndroidRuntime(16152):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
05-23 22:04:03.830: E/AndroidRuntime(16152):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
05-23 22:04:03.830: E/AndroidRuntime(16152):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
05-23 22:04:03.830: E/AndroidRuntime(16152):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
05-23 22:04:03.830: E/AndroidRuntime(16152):    at android.app.Activity.setContentView(Activity.java:1867)
05-23 22:04:03.830: E/AndroidRuntime(16152):    at dk.fourway.divepool.notebook.FlipperFourActivity.onCreate(FlipperFourActivity.java:18)
05-23 22:04:03.830: E/AndroidRuntime(16152):    at android.app.Activity.performCreate(Activity.java:5008)
05-23 22:04:03.830: E/AndroidRuntime(16152):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
05-23 22:04:03.830: E/AndroidRuntime(16152):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
05-23 22:04:03.830: E/AndroidRuntime(16152):    ... 11 more
05-23 22:04:03.830: E/AndroidRuntime(16152): Caused by: java.lang.reflect.InvocationTargetException
05-23 22:04:03.830: E/AndroidRuntime(16152):    at java.lang.reflect.Constructor.constructNative(Native Method)
05-23 22:04:03.830: E/AndroidRuntime(16152):    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
05-23 22:04:03.830: E/AndroidRuntime(16152):    at android.view.LayoutInflater.createView(LayoutInflater.java:587)
05-23 22:04:03.830: E/AndroidRuntime(16152):    ... 25 more
05-23 22:04:03.830: E/AndroidRuntime(16152): Caused by: java.lang.OutOfMemoryError
05-23 22:04:03.830: E/AndroidRuntime(16152):    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
05-23 22:04:03.830: E/AndroidRuntime(16152):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:500)
05-23 22:04:03.830: E/AndroidRuntime(16152):    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:353)
05-23 22:04:03.830: E/AndroidRuntime(16152):    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:781)
05-23 22:04:03.830: E/AndroidRuntime(16152):    at android.content.res.Resources.loadDrawable(Resources.java:1930)
05-23 22:04:03.830: E/AndroidRuntime(16152):    at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
05-23 22:04:03.830: E/AndroidRuntime(16152):    at android.widget.ImageView.<init>(ImageView.java:120)
05-23 22:04:03.830: E/AndroidRuntime(16152):    at android.widget.ImageView.<init>(ImageView.java:110)
05-23 22:04:03.830: E/AndroidRuntime(16152):    ... 28 more
jumps4fun
  • 3,994
  • 10
  • 50
  • 96
  • Rather old post, but @KjetilNordin, did you find a solution? – leMale Oct 21 '16 at 13:47
  • @leMale I have to admit, I only ever did android programming once. As a stand alone project when I was in school. So far it has been the programming area I have enjoyed the least, because I had a very hard time debugging my errors. I have not maintained any skills in android programming after this project. I now do mostly web programming. – jumps4fun Oct 24 '16 at 10:38

0 Answers0