Android设备配置更改和setRetainInstance,第二次崩溃

时间:2012-09-30 15:53:51

标签: android

我正在撰写一个包含单个细分的活动。 此分段显示一组带有“AdapterViewFlipper”和“SimpleCursorAdapter”的照片。

要快速响应设备的方向更改,我使用片段“setRetainInstance”功能。

它可以在第一次方向更改时快速而精确地工作,无需从网络重新加载图像。

第二个方向更改出现问题,片段“onDetach”中出现“IllegalArgumentException”:

09-30 13:50:36.374: W/System.err(8310): java.lang.IllegalArgumentException: Receiver not   registered: android.widget.AdapterViewFlipper$1@424512f8
09-30 13:50:36.384: W/System.err(8310):     at android.app.LoadedApk.forgetReceiverDispatcher(LoadedApk.java:654)
09-30 13:50:36.384: W/System.err(8310):     at android.app.ContextImpl.unregisterReceiver(ContextImpl.java:1134)
09-30 13:50:36.384: W/System.err(8310):     at android.content.ContextWrapper.unregisterReceiver(ContextWrapper.java:378)
09-30 13:50:36.384: W/System.err(8310):     at android.widget.AdapterViewFlipper.onDetachedFromWindow(AdapterViewFlipper.java:112)
09-30 13:50:36.384: W/System.err(8310):     at android.view.View.dispatchDetachedFromWindow(View.java:11789)
09-30 13:50:36.384: W/System.err(8310):     at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:2534)
09-30 13:50:36.384: W/System.err(8310):     at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:2532)
09-30 13:50:36.384: W/System.err(8310):     at android.view.ViewGroup.removeAllViewsInLayout(ViewGroup.java:3819)
09-30 13:50:36.384: W/System.err(8310):     at android.view.ViewGroup.removeAllViews(ViewGroup.java:3765)
09-30 13:50:36.384: W/System.err(8310):     at debut.telebox.FragPhoto.onDetach(FragPhoto.java:117)
09-30 13:50:36.384: W/System.err(8310):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:991)
09-30 13:50:36.384: W/System.err(8310):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
09-30 13:50:36.384: W/System.err(8310):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1017)
09-30 13:50:36.384: W/System.err(8310):     at android.app.FragmentManagerImpl.dispatchDestroy(FragmentManager.java:1826)
09-30 13:50:36.384: W/System.err(8310):     at android.app.Activity.performDestroy(Activity.java:5171)
09-30 13:50:36.384: W/System.err(8310):     at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1109)
09-30 13:50:36.394: W/System.err(8310):     at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3260)
09-30 13:50:36.394: W/System.err(8310):     at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3291)
09-30 13:50:36.394: W/System.err(8310):     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3489)
09-30 13:50:36.394: W/System.err(8310):     at android.app.ActivityThread.access$700(ActivityThread.java:130)
09-30 13:50:36.394: W/System.err(8310):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201)
09-30 13:50:36.394: W/System.err(8310):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-30 13:50:36.394: W/System.err(8310):     at android.os.Looper.loop(Looper.java:137)
09-30 13:50:36.394: W/System.err(8310):     at android.app.ActivityThread.main(ActivityThread.java:4745)
09-30 13:50:36.394: W/System.err(8310):     at java.lang.reflect.Method.invokeNative(Native Method)
09-30 13:50:36.394: W/System.err(8310):     at java.lang.reflect.Method.invoke(Method.java:511)
09-30 13:50:36.394: W/System.err(8310):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-30 13:50:36.394: W/System.err(8310):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-30 13:50:36.394: W/System.err(8310):     at dalvik.system.NativeStart.main(Native Method)

FragPhoto第117行在“((ViewGroup)principale.getParent())。removeAllViews();”

以下是我的活动代码:

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle etatSauve) {
    super.onCreate(etatSauve);
    setContentView(R.layout.mainphoto);
    }

@Override
public void onDestroy () {
    super.onDestroy();
}

活动布局:

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

<fragment
     android:id="@+id/activitePhotoFrag"
     android:name="debut.telebox.FragPhoto"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     />

</LinearLayout>

片段的代码包含:

private View principale;
private SimpleCursorAdapter monAdapter;
private AdapterViewFlipper vuele;

@Override
public void onCreate(Bundle etatSauve) {
    super.onCreate(etatSauve);
    this.setHasOptionsMenu(true);
    this.setRetainInstance(true);
    }

@Override
public View onCreateView(LayoutInflater inflateur, ViewGroup container,
        final Bundle etatSauve) {

    if (principale == null) {
        principale = inflateur.inflate(R.layout.fragmentphoto, container, false);
        vuele = (AdapterViewFlipper) principale.findViewById(R.id.photoflipper);

    }

    return principale;
}

public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    if (getLoaderManager().getLoader(0) == null)
        getLoaderManager().initLoader(0, getArguments(), this);
}

public void onAttach (Activity activity) {
    super.onAttach(activity);
}

public void onDetach() {
    super.onDetach();
    try {
        ((ViewGroup) principale.getParent()).removeAllViews();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    }
}

看来片段视图父级第二次不好......为什么?

1 个答案:

答案 0 :(得分:0)

通过简单的“ImageView”和“Cursor”替换“AdapterViewFlipper”解决了问题