我正在编写一个包含单个片段的活动。此片段显示一组带有“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();
}
}
看来第二次fragment view parent不好...为什么?