我正在尝试在平板电脑视图中制作2片段屏幕,但在旋转设备后它会崩溃。
这是layout-sw600dp/
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:divider="?android:attr/dividerHorizontal"
android:id="@+id/main_activity"
android:orientation="horizontal"
tools:context="com.morxander.popularmovies.MainActivity">
<fragment
android:id="@+id/fragment_main"
android:name="com.morxander.popularmovies.MainActivity$PlaceholderFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="4"
tools:layout="@layout/fragment_main" />
<FrameLayout
android:id="@+id/movie_detail_container"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3" />
</LinearLayout>
这是代码的相关部分:
private static boolean mTwoPane;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (findViewById(R.id.movie_detail_container) != null) {
mTwoPane = true;
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_main, new PlaceholderFragment())
.commit();
}else{
mTwoPane = false;
}
}
}
.....
.....
.....
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
setHasOptionsMenu(true);
// Method to set the views controls
initComponents(rootView);
return rootView;
当我旋转设备时,应用程序崩溃并给我以下错误:
08-10 21:22:34.842: E/AndroidRuntime(4338): FATAL EXCEPTION: main
08-10 21:22:34.842: E/AndroidRuntime(4338): Process: com.morxander.popularmovies, PID: 4338
08-10 21:22:34.842: E/AndroidRuntime(4338): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.morxander.popularmovies/com.morxander.popularmovies.MainActivity}: android.view.InflateException: Binary XML file line #15: Error inflating class fragment
08-10 21:22:34.842: E/AndroidRuntime(4338): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
08-10 21:22:34.842: E/AndroidRuntime(4338): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
08-10 21:22:34.842: E/AndroidRuntime(4338): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3912)
08-10 21:22:34.842: E/AndroidRuntime(4338): at android.app.ActivityThread.access$900(ActivityThread.java:144)
08-10 21:22:34.842: E/AndroidRuntime(4338): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284)
08-10 21:22:34.842: E/AndroidRuntime(4338): at android.os.Handler.dispatchMessage(Handler.java:102)
08-10 21:22:34.842: E/AndroidRuntime(4338): at android.os.Looper.loop(Looper.java:135)
08-10 21:22:34.842: E/AndroidRuntime(4338): at android.app.ActivityThread.main(ActivityThread.java:5221)
08-10 21:22:34.842: E/AndroidRuntime(4338): at java.lang.reflect.Method.invoke(Native Method)
08-10 21:22:34.842: E/AndroidRuntime(4338): at java.lang.reflect.Method.invoke(Method.java:372)
08-10 21:22:34.842: E/AndroidRuntime(4338): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
08-10 21:22:34.842: E/AndroidRuntime(4338): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
08-10 21:22:34.842: E/AndroidRuntime(4338): Caused by: android.view.InflateException: Binary XML file line #15: Error inflating class fragment
08-10 21:22:34.842: E/AndroidRuntime(4338): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
08-10 21:22:34.842: E/AndroidRuntime(4338): at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
08-10 21:22:34.842: E/AndroidRuntime(4338): at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
08-10 21:22:34.842: E/AndroidRuntime(4338): at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
08-10 21:22:34.842: E/AndroidRuntime(4338): at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
08-10 21:22:34.842: E/AndroidRuntime(4338): at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:240)
08-10 21:22:34.842: E/AndroidRuntime(4338): at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
08-10 21:22:34.842: E/AndroidRuntime(4338): at com.morxander.popularmovies.MainActivity.onCreate(MainActivity.java:38)
08-10 21:22:34.842: E/AndroidRuntime(4338): at android.app.Activity.performCreate(Activity.java:5937)
08-10 21:22:34.842: E/AndroidRuntime(4338): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
08-10 21:22:34.842: E/AndroidRuntime(4338): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
08-10 21:22:34.842: E/AndroidRuntime(4338): ... 11 more
08-10 21:22:34.842: E/AndroidRuntime(4338): Caused by: java.lang.IllegalStateException: Fragment com.morxander.popularmovies.MainActivity$PlaceholderFragment did not create a view.
08-10 21:22:34.842: E/AndroidRuntime(4338): at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2199)
08-10 21:22:34.842: E/AndroidRuntime(4338): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:297)
08-10 21:22:34.842: E/AndroidRuntime(4338): at android.support.v7.app.ActionBarActivity.onCreateView(ActionBarActivity.java:543)
08-10 21:22:34.842: E/AndroidRuntime(4338): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
08-10 21:22:34.842: E/AndroidRuntime(4338): ... 21 more
}
我尝试了很多解决方案,但没有任何对我有用。
答案 0 :(得分:0)
尝试检查语句
中传递的布局View rootView = inflater.inflate(R.layout.fragment_main, container, false);