应用程序崩溃在屏幕旋转

时间:2014-06-24 18:49:37

标签: android android-fragments screen-rotation

旋转屏幕时,我的应用不断崩溃。

以下是我遇到的错误:

06-24 14:43:29.531  25869-25869/com.rcd.mypr E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.rcd.mypr, PID: 25869
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rcd.mypr/com.rcd.mypr.Workouts.WorkoutsActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
            at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3819)
            at android.app.ActivityThread.access$900(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1216)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5102)
            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:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
            at android.app.Activity.setContentView(Activity.java:1937)
            at android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java:216)
            at android.support.v7.app.ActionBarActivityDelegateICS.setContentView(ActionBarActivityDelegateICS.java:110)
            at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:76)
            at com.rcd.mypr.Workouts.WorkoutsActivity.onCreate(WorkoutsActivity.java:30)
            at android.app.Activity.performCreate(Activity.java:5248)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2173)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
            at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3819)
            at android.app.ActivityThread.access$900(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1216)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5102)
            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:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.IllegalStateException: Fragment com.rcd.mypr.Workouts.WorkoutsFragment did not create a view.
            at android.app.Activity.onCreateView(Activity.java:4826)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
            at android.app.Activity.setContentView(Activity.java:1937)
            at android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java:216)
            at android.support.v7.app.ActionBarActivityDelegateICS.setContentView(ActionBarActivityDelegateICS.java:110)
            at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:76)
            at com.rcd.mypr.Workouts.WorkoutsActivity.onCreate(WorkoutsActivity.java:30)
            at android.app.Activity.performCreate(Activity.java:5248)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2173)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
            at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3819)
            at android.app.ActivityThread.access$900(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1216)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5102)
            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:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)

这是WorkoutsActivity onCreate

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

        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(false);

        overridePendingTransition(R.anim.right_slide_in, R.anim.right_slide_out);

        // fragment is bodyFragment
        fragment = new WorkoutsFragment();
        benchmarkFragment = new TheBenchmarkGirlsListFragment();

        if (fragment != null) {
            fragmentManager.beginTransaction()
                    .replace(R.id.workoutsListFragment, fragment, TAG_FRAGMENT_HEROES)
                    .commit();
        } else {
            // error in creating fragment
            Log.e("MainActivity", "Error in creating fragment");
        }
    }

这是我正在创建锻炼列表的WorkoutsFragment

package com.rcd.mypr.Workouts;

import android.app.Fragment;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.rcd.mypr.R;

public class WorkoutsFragment extends Fragment {

    WorkoutsDatabaseHelper db;
    SimpleCursorAdapter adapter;
    ListView listContent;
    Cursor cursor;
    private Context mContext;
    WorkoutsAdapter workoutsAdapter;

    public WorkoutsFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mContext = getActivity();
        db = new WorkoutsDatabaseHelper(mContext);

        View rootView = inflater.inflate(R.layout.fragment_workouts_list, container, false);

        listContent = (ListView) rootView.findViewById(R.id.list);

        // Create the Adapter
        workoutsAdapter = new WorkoutsAdapter(mContext, cursor);

        // Set the adapter to ListView
        listContent.setAdapter(workoutsAdapter);


        return rootView;
    }


    private void initViews() {
        //Typeface tf = Typeface.createFromAsset(mContext.getAssets(),"Roboto-Light.ttf");
    }

    private void addItemsToList() {
        Cursor cursor = db.getWorkoutNames();
        Log.d("history.java", "finished Cursor cursor = db.getAllLogs();");
        String[] from = {"workouts_name"};
        int[] to = {R.id.tv_label};
        adapter = new SimpleCursorAdapter(mContext, R.layout.fragment_workouts_list_single_item, cursor, from, to, 0);
        listContent.setAdapter(adapter);

    }

    /*public void onResume(){
        super.onResume();
        workoutsAdapter = new WorkoutsAdapter(mContext, cursor);
        listContent.setAdapter(workoutsAdapter);

    }*/

    public void onSaveInstanceState(Bundle savedState) {
        super.onSaveInstanceState(savedState);
    }


}

activity_workouts.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <fragment
        android:id="@+id/workoutsListFragment"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        class="com.rcd.mypr.Workouts.WorkoutsFragment" ></fragment>

</LinearLayout>

fragment_workouts_list.xml

<?xml version="1.0" encoding="utf-8"?><!--
Copyright (C) 2012 The Android Open Source Project

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/darkGray">

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_marginLeft="6dp"
        android:layout_marginRight="6dp"
        android:divider="@null"
        android:dividerHeight="0dp"
        android:clipToPadding="false"
        android:scrollbarStyle="outsideOverlay"
        android:layout_weight="1"
        android:orientation="vertical"></ListView>

</FrameLayout>

我认为它与activity_workouts.xml

有关

我尝试将其更改为仅没有片段的FrameLayout,它仍会加载列表,但仍会在旋转时崩溃。

有什么建议吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

我从:

更改了activity_workouts.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <fragment
        android:id="@+id/workoutsListFragment"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        class="com.rcd.mypr.Workouts.WorkoutsFragment" ></fragment>

</LinearLayout>

为:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/workoutsListFragment"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

</LinearLayout>