从LANDSCAPE旋转到PORTRAIT后,Android应用程序崩溃

时间:2015-04-12 22:02:00

标签: java android-view

我正在构建我的第一个Android应用程序,我已经查阅了一些教程和stackoverflow帖子,但我似乎无法找到适合此问题的修复程序。 我一直在关注TheNewBoston和Derek Banas的YouTube教程,以及一些书面教程。

我的应用程序在启动应用程序时处于运行状态(处于纵向模式)。当我把它换成风景时,它仍然可以正常工作。但是,当我将其旋转回肖像时,应用程序崩溃时出现以下错误:

04-12 23:42:12.331  28004-28004/nl.avans.mbd2.android.gideon.van.david.multipanefragment E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: nl.avans.mbd2.android.gideon.van.david.multipanefragment, PID: 28004
    java.lang.RuntimeException: Unable to start activity ComponentInfo{nl.avans.mbd2.android.gideon.van.david.multipanefragment/nl.avans.mbd2.android.gideon.van.david.multipanefragment.MainActivity}: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2413)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
            at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4054)
            at android.app.ActivityThread.access$1000(ActivityThread.java:175)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1314)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5602)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
            at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:240)
            at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
            at nl.avans.mbd2.android.gideon.van.david.multipanefragment.MainActivity.onCreate(MainActivity.java:18)
            at android.app.Activity.performCreate(Activity.java:5451)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
            at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4054)
            at android.app.ActivityThread.access$1000(ActivityThread.java:175)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1314)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5602)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.IllegalStateException: Fragment nl.avans.mbd2.android.gideon.van.david.multipanefragment.MainPanelFragment did not create a view.
            at android.app.Activity.onCreateView(Activity.java:5020)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:695)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
            at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:240)
            at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
            at nl.avans.mbd2.android.gideon.van.david.multipanefragment.MainActivity.onCreate(MainActivity.java:18)
            at android.app.Activity.performCreate(Activity.java:5451)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
            at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4054)
            at android.app.ActivityThread.access$1000(ActivityThread.java:175)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1314)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5602)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            at dalvik.system.NativeStart.main(Native Method)

实际错误是:

java.lang.RuntimeException: Unable to start activity ComponentInfo{nl.avans.mbd2.android.gideon.van.david.multipanefragment/nl.avans.mbd2.android.gideon.van.david.multipanefragment.MainActivity}: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class fragment

Caused by: java.lang.IllegalStateException: Fragment nl.avans.mbd2.android.gideon.van.david.multipanefragment.MainPanelFragment did not create a view.

我的MainActivity看起来像这样:

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        int screenOrientation = getResources().getConfiguration().orientation;

        if (screenOrientation == Configuration.ORIENTATION_PORTRAIT) {
            hideSidePanel();
        }
    }

    private void hideSidePanel() {
        View detailPane = findViewById(R.id.detail_panel);
        if(detailPane.getVisibility() == View.VISIBLE) {
            detailPane.setVisibility(View.GONE);
        }
    }
}

MainPanelFragment:

public class MainPanelFragment extends ListFragment {

    boolean orientation;
    int currentListViewItem;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        ArrayAdapter<String> connectArrayToListView = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_activated_1, API.dummyData);
        setListAdapter(connectArrayToListView);

        orientation = getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;

        if (savedInstanceState != null) {
            currentListViewItem = savedInstanceState.getInt("curChoice", 0);
        }

        if (orientation) {
            getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
            showDetails(currentListViewItem);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);

        return inflater.inflate(R.layout.fragment_main_panel, container, false);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        outState.putInt("curChoice", currentListViewItem);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        showDetails(position);
        // super.onListItemClick(l, v, position, id);
    }

    void showDetails(int kaart) {

        currentListViewItem = kaart;

        if (orientation) {
            getListView().setItemChecked(kaart, true);

            DetailPanelFragment details = (DetailPanelFragment) getFragmentManager().findFragmentById(R.id.detail_panel);

            if (details == null || details.getShowIndex() == -1) {
                details = DetailPanelFragment.newInstance(kaart);
                FragmentTransaction ft = getFragmentManager().beginTransaction();
                ft.replace(R.id.main_panel, details);
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                ft.commit();
            }
        } else {
            Intent intent = new Intent();
            intent.setClass(getActivity(), DetailsActivity.class);
            intent.putExtra("index", kaart);
            startActivity(intent);
        }
    }
}

activity_main.xml中:

<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:animateLayoutChanges="true"
    tools:context=".MainActivity">

    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:id="@+id/main_panel"
        android:layout_alignParentRight="true"
        class="nl.avans.mbd2.android.gideon.van.david.multipanefragment.MainPanelFragment"/>

    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:id="@+id/detail_panel"
        android:layout_alignParentLeft="true"
        class="nl.avans.mbd2.android.gideon.van.david.multipanefragment.DetailPanelFragment"/>

</LinearLayout>

fragment_detail_panel.xml:

<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:background="@android:color/holo_blue_bright"
    android:layout_gravity="top|center"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Detail panel"
        android:textColor="@android:color/white"
        android:textStyle="bold"
        android:id="@+id/kaartenListViewDetail"
        />

</LinearLayout>

fragment_main_panel.xml:

<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:background="@android:color/holo_red_dark"
    android:layout_gravity="top|center"
    android:orientation="vertical">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@android:id/list">
    </ListView>

</LinearLayout>

我还在/res中有一个名为layout-land的文件夹。它包含一个activity_main.xml文件,该文件与上面发布的文件完全相同。 我已经看过类似这样的类似问题了,但似乎这种情况并不(足够)找到一个有效的答案。

0 个答案:

没有答案