简体   繁体   中英

ViewFlipper crashes after orientation change

I'm trying to use inflate while using ViewFlipper to access to the data inside my view. I've done sample project that crashes.

main.xml

<?xml version="1.0" encoding="utf-8"?>  
<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"  
    android:id="@+id/RelativeLayout"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content" />

FlipViewBug.java

package android.FlipViewBug;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
public class FlipViewBug extends Activity {

    private static LayoutInflater inflater = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
}

When I rotate emulator from horizontal to vertical orientation CTRL+F11 app crashes with stopped unexpectedly .

If I remove line

inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

app works fine. Am I trying to do something wrong? In my app I have more complex LinnarView that ViewFlipper is nested and the results are the same.

I was checking this on android 1.5, 2.2 and galaxy tab. There is no problem while rotating form vertical view to horizontal.

As a rule of thumb if you're storing UI objectrefs in static data you're probably doing something wrong. :) Even when things appear to work, you are likely to be leaking memory until Android decides to kill your process. See Romain Guy's article on this for more details.

So basically you answered your own question... don't do that! If you want to delay inflation of flipped-out views until they are flipped-in (ie as a performance improvement) I'd suggest you look into ViewStub .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM