我的地图一直有效,直到我从本教程"Handling The Change"添加了RetainedFragment。
在使用类 com.google.android.gms.maps.SupportMapFragment 包含片段的布局进行膨胀时发生错误:
SplashScreenActivity.java中的PlaceholderFragment
View rootView = inflater.inflate(R.layout.fragment_splash_screen, container, false);
RES /布局/ fragment_splash_screen.xml
<fragment
android:id="@+id/app_search_map_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
错误:
android.view.InflateException: Binary XML file line #18: Error inflating class fragment
完整筹码:
03-25 10:16:51.681: I/Process(31941): Sending signal. PID: 31941 SIG: 9
03-25 10:17:00.141: E/Trace(32468): error opening trace file: No such file or directory (2)
03-25 10:17:00.381: D/AndroidRuntime(32468): Shutting down VM
03-25 10:17:00.381: W/dalvikvm(32468): threadid=1: thread exiting with uncaught exception (group=0x4184c7d0)
03-25 10:17:00.401: E/AndroidRuntime(32468): FATAL EXCEPTION: main
03-25 10:17:00.401: E/AndroidRuntime(32468): java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.ericksprengel.piz/br.com.ericksprengel.supportmapfragmentvsretainedfragment.SplashScreenActivity}: android.view.InflateException: Binary XML file line #18: Error inflating class fragment
03-25 10:17:00.401: E/AndroidRuntime(32468): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2115)
03-25 10:17:00.401: E/AndroidRuntime(32468): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2153)
03-25 10:17:00.401: E/AndroidRuntime(32468): at android.app.ActivityThread.access$700(ActivityThread.java:137)
03-25 10:17:00.401: E/AndroidRuntime(32468): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
03-25 10:17:00.401: E/AndroidRuntime(32468): at android.os.Handler.dispatchMessage(Handler.java:99)
03-25 10:17:00.401: E/AndroidRuntime(32468): at android.os.Looper.loop(Looper.java:137)
03-25 10:17:00.401: E/AndroidRuntime(32468): at android.app.ActivityThread.main(ActivityThread.java:5031)
03-25 10:17:00.401: E/AndroidRuntime(32468): at java.lang.reflect.Method.invokeNative(Native Method)
03-25 10:17:00.401: E/AndroidRuntime(32468): at java.lang.reflect.Method.invoke(Method.java:511)
03-25 10:17:00.401: E/AndroidRuntime(32468): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
03-25 10:17:00.401: E/AndroidRuntime(32468): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
03-25 10:17:00.401: E/AndroidRuntime(32468): at dalvik.system.NativeStart.main(Native Method)
03-25 10:17:00.401: E/AndroidRuntime(32468): Caused by: android.view.InflateException: Binary XML file line #18: Error inflating class fragment
03-25 10:17:00.401: E/AndroidRuntime(32468): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
03-25 10:17:00.401: E/AndroidRuntime(32468): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
03-25 10:17:00.401: E/AndroidRuntime(32468): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
03-25 10:17:00.401: E/AndroidRuntime(32468): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
03-25 10:17:00.401: E/AndroidRuntime(32468): at br.com.ericksprengel.supportmapfragmentvsretainedfragment.SplashScreenActivity$PlaceholderFragment.onCreateView(SplashScreenActivity.java:72)
03-25 10:17:00.401: E/AndroidRuntime(32468): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
03-25 10:17:00.401: E/AndroidRuntime(32468): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
03-25 10:17:00.401: E/AndroidRuntime(32468): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
03-25 10:17:00.401: E/AndroidRuntime(32468): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
03-25 10:17:00.401: E/AndroidRuntime(32468): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
03-25 10:17:00.401: E/AndroidRuntime(32468): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
03-25 10:17:00.401: E/AndroidRuntime(32468): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1163)
03-25 10:17:00.401: E/AndroidRuntime(32468): at android.app.Activity.performStart(Activity.java:5068)
03-25 10:17:00.401: E/AndroidRuntime(32468): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2088)
03-25 10:17:00.401: E/AndroidRuntime(32468): ... 11 more
03-25 10:17:00.401: E/AndroidRuntime(32468): Caused by: java.lang.IllegalStateException: Fragment com.google.android.gms.maps.SupportMapFragment did not create a view.
03-25 10:17:00.401: E/AndroidRuntime(32468): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:314)
03-25 10:17:00.401: E/AndroidRuntime(32468): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
03-25 10:17:00.401: E/AndroidRuntime(32468): ... 24 more
我没有想法RetainedFragment与SupportMapFragment之间的关系是什么,但只有在我使用RetainedFragment时才会出现此错误。
// find the retained fragment on activity restarts
FragmentManager fm = getSupportFragmentManager();
dataFragment = (RetainedFragment) fm.findFragmentByTag("data");
// create the fragment and data the first time
if (dataFragment == null) {
// add the fragment
dataFragment = new RetainedFragment();
fm.beginTransaction().add(dataFragment, "data").commit();
}
如果删除此行,应用程序不会崩溃
fm.beginTransaction().add(dataFragment, "data").commit();
我创建了一个简单的项目,其中包含一个带有SupportMapFragment和RetainedFragment的简单布局的简单Activity。
简单项目: https://github.com/ericksprengel/support-map-fragment-vs-retained-fragment
答案 0 :(得分:1)
我不确定这与你的RetainedFragment有什么关系,这很奇怪。
但看起来你的问题与Having an issue with GoogleMap v2 crashing app on orientation change有关。
您正在创建占位符片段,然后从xml加载子片段。看起来这不起作用,并且子片段只能动态添加。试试看,看看它是否解决了你的问题。