2012-09-30 55 views
0

我在寫一個包含單個分段的活動。 此分段顯示一組帶有「AdapterViewFlipper」和「SimpleCursorAdapter」的照片。Android設備配置更改和setRetainInstance,第二次崩潰

要對設備的方向更改做出快速響應,我使用片段「setRetainInstance」功能。

它在第一個方向改變時可以快速且很好地工作,無需從網絡重新加載圖像。

的問題出現在第二取向變化,以在片段 「onDetach」 一 「拋出:IllegalArgumentException」:

09-30 13:50:36.374: W/System.err(8310): java.lang.IllegalArgumentException: Receiver not registered: [email protected] 
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(); 
    } 
} 

看來,片段視圖父母第二次不好...爲什麼?

回答

0

通過將「AdapterViewFlipper」替換爲簡單的「ImageView」和「光標」即可解決問題。