我正在开发针对2.x和3.0设备的Android应用程序,因此我正在使用兼容性API。我正在Android 2.0上进行测试。
我试图将显示的片段替换为另一个片段(带有搜索结果的搜索表单),并且在显示第二个(结果)片段时发生方向更改崩溃。
基本上,我有一个 Activity ,其中包括在布局xml中定义的片段,如下所示:
<fragment class="org.prevoz.android.search.SearchFormFragment"
android:id = "@+id/search_form_fragment"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent" />
默认显示第一个片段(SearchFormFragment)。当用户点击“搜索”按钮时,我将SearchFormFragment替换为SearchResultsFragment,后者将运行AsyncTask(这就是为什么我要保留它)
// Show the search results fragment
SearchResultsFragment newSearch = new SearchResultsFragment(from, to, when);
newSearch.setRetainInstance(true);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
transaction.replace(R.id.search_form_fragment, newSearch);
transaction.addToBackStack(null);
transaction.commit();
但是,当显示SearchFormFragment并更改方向时,我的应用程序崩溃
ERROR/AndroidRuntime(334): FATAL EXCEPTION: main
ERROR/AndroidRuntime(334): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.prevoz.android/org.prevoz.android.search.MainActivity}: android.view.InflateException: Binary XML file line #13: Error inflating class fragment
ERROR/AndroidRuntime(334): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
ERROR/AndroidRuntime(334): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
ERROR/AndroidRuntime(334): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3815)
ERROR/AndroidRuntime(334): at android.app.ActivityThread.access$2400(ActivityThread.java:125)
ERROR/AndroidRuntime(334): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2037)
ERROR/AndroidRuntime(334): at android.os.Handler.dispatchMessage(Handler.java:99)
ERROR/AndroidRuntime(334): at android.os.Looper.loop(Looper.java:123)
ERROR/AndroidRuntime(334): at android.app.ActivityThread.main(ActivityThread.java:4627)
ERROR/AndroidRuntime(334): at java.lang.reflect.Method.invokeNative(Native Method)
ERROR/AndroidRuntime(334): at java.lang.reflect.Method.invoke(Method.java:521)
ERROR/AndroidRuntime(334): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
ERROR/AndroidRuntime(334): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
ERROR/AndroidRuntime(334): at dalvik.system.NativeStart.main(Native Method)
ERROR/AndroidRuntime(334): Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class fragment
ERROR/AndroidRuntime(334): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:582)
ERROR/AndroidRuntime(334): at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
ERROR/AndroidRuntime(334): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
ERROR/AndroidRuntime(334): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
ERROR/AndroidRuntime(334): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
ERROR/AndroidRuntime(334): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
ERROR/AndroidRuntime(334): at android.app.Activity.setContentView(Activity.java:1647)
ERROR/AndroidRuntime(334): at org.prevoz.android.search.MainActivity.onCreate(MainActivity.java:40)
ERROR/AndroidRuntime(334): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
ERROR/AndroidRuntime(334): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
ERROR/AndroidRuntime(334): ... 12 more
ERROR/AndroidRuntime(334): Caused by: java.lang.IllegalStateException: Fragment org.prevoz.android.search.SearchFormFragment did not create a view.
ERROR/AndroidRuntime(334): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:281)
ERROR/AndroidRuntime(334): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:558)
ERROR/AndroidRuntime(334): ... 21 more
WARN/ActivityManager(59): Force finishing activity org.prevoz.android/.search.MainActivity
确实调用了SearchFormFragment的相关onCreateView代码,并且我返回了一个有效的 View :
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState)
{
View newView = inflater.inflate(R.layout.search_form_frag, container, false);
return newView;
}
仅当显示SearchResultsFragment时发生崩溃,如果显示默认的SearchFormFragment,则更改可以正常进行。
那么,如何通过方向更改来保留第二个片段状态?确实缺少有关API和状态更改的文档。
最佳答案
不要在XML中创建SearchFormFragment
。而是有一个空的FrameLayout
,您将其填充在Activity.onCreate()
中而不将其添加到后堆栈中。这样,Activity
将保留当前的Fragment
,而不是尝试添加XML中指定的AsyncLoader
。
另外,使用ojit_code可能是一种更好的方法,请参见http://code.google.com/p/android/issues/detail?id=14944