我有个问题:
android.view.inflateexception:二进制xml文件行31:膨胀类片段时出错
fragment com.myapp.mgm.queryfragment未创建视图。
只有在平板电脑模式下才会出现此问题。如果应用程序在手机上运行,它可以正常工作。
我有这个layout-largexml文件(fragments.xml):

<android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/lightgrey">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="@color/lightgrey"
              android:baselineAligned="false">

    <fragment
            android:name="com.myapp.mgm.QueryFragment"
            android:id="@+id/fragment1"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            tools:layout="@layout/nu"/>

    <fragment
            android:name="com.myapp.mgm.Infodata"
            android:id="@+id/fragment2"
            android:layout_weight="2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            tools:layout="@layout/maindata"/>

</LinearLayout>
        <ListView
                android:id="@+id/list_slidermenu"
                android:layout_width="240dp"
                android:layout_height="match_parent"
                android:layout_gravity="start"
                android:choiceMode="singleChoice"
                android:divider="@color/orange"
                android:dividerHeight="1dp"
                android:listSelector="@drawable/menulist_selector"
                android:background="@color/lblack"/>
</android.support.v4.widget.DrawerLayout>

如您所见,我的布局上有两个片段和一个导航抽屉。
将所有这些放在一起的类是:
public class Fragments extends ActionBarActivity implements QueryFragment.OnQuerySelectedListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragments);

        slidingMenu();

        if (findViewById(R.id.fragmentsboth) != null) {
            if (savedInstanceState != null) {
                return;
            }
            QueryFragment firstFragment = new QueryFragment();
            getSupportFragmentManager().beginTransaction().add(R.id.fragmentsboth, firstFragment).commit();
        }
    }
}

我的课是这样的:
public class QueryFragment extends ListFragment {
    public interface OnQuerySelectedListener {
        public void onItemSelected(Bundle itemSelectedData);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        operation = Fragments.operation;
        if (operation == 0) {
            layoutToShow = inflater.inflate(R.layout.querylist, container, false);
            ...
        } else if (operation == 1) {
            layoutToShow = inflater.inflate(R.layout.querylist, container, false);
            ...
        } else if (operation == 2) {
            layoutToShow = inflater.inflate(R.layout.search, container, false);
            ...
        } else if (operation == 3) {
            layoutToShow = inflater.inflate(R.layout.nu, container, false);
            ...
        }
        return layoutToShow;
    }

}
我的课是这样的:
public class Infodata extends Fragment implements OnSeekBarChangeListener, OnItemSelectedListener {

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

        View itemDataLayout = inflater.inflate(R.layout.maindata, container, false);
        ....

        if (savedInstanceState != null && savedInstanceState.size()>=2) {
            layoutType = savedInstanceState.getInt("layout");
            comesFromSavedState = true;
            itemData = savedInstanceState;
        }
        return itemDataLayout;
    }

    @Override
    public void onStart() {
        super.onStart();

        Bundle args = getArguments();
        if (args != null) {
            populateLayoutWithData(1, args);
        } else if(comesFromSavedState){
            populateLayoutWithData(2, itemData);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //Saves stuff (data) in a bundle to restore after screen rotation
    }
}

基本上,我在左边的片段(QueryFragment)上有一个InfoData,当用户选择其中一个项(带有数据的游标)之后,右边的片段(listview)将填充这个游标的提取数据。
如果我从左片段中选择一个intem,它的数据填充在右片段中,因为我在QueryFragment上保存数据,如果我旋转屏幕,一切正常,也就是说,数据在右片段上重新加载。
现在,我的导航抽屉有4个选择。每一个选项,在左边的片段上加载一个InfoData,然后,和以前一样,如果用户选择了其中一个项,它的数据将加载在右边的片段上。
下面是在MyonSaveInstanceState类中处理此问题的代码:
private void selectItem(int position) {
    int typeOfLayout;
    mDrawerList.setItemChecked(position, true);
    mDrawerLayout.closeDrawer(mDrawerList);
    QueryFragment newFragment;
    FragmentTransaction transaction;

    ViewGroup parentView = (ViewGroup) findViewById(R.id.fragmentsboth);

    if (parentView != null) {
        typeOfLayout = R.id.fragmentsboth;
    } else {
        typeOfLayout = R.id.fragment1;
    }

    switch (position) {
        case 0:
            Intent i = new Intent(this, NutriData.class);
            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(i);
            finish();
            break;
        case 1:
            newFragment = new QueryFragment();
            operation = 0;
            transaction = getSupportFragmentManager().beginTransaction();
            transaction.replace(typeOfLayout, newFragment).commit();
            break;
        case 2:
            newFragment = new QueryFragment();
            operation = 1;
            transaction = getSupportFragmentManager().beginTransaction();
            transaction.replace(typeOfLayout, newFragment).commit();
            break;
        case 3:
            newFragment = new QueryFragment();
            operation = 2;
            transaction = getSupportFragmentManager().beginTransaction();
            transaction.replace(typeOfLayout, newFragment).commit();
            break;
        case 4:
            newFragment = new QueryFragment();
            operation = 3;
            transaction = getSupportFragmentManager().beginTransaction();
            transaction.replace(typeOfLayout, newFragment).commit();
            break;
    }
}

想象一下塞纳里奥:
我得到一个listView项目在左边的片段,然后选择这些项目之一。然后,它的数据被传递到正确的片段,把它放在正确的位置。如果我旋转屏幕,数据将被保存,旋转后将重新加载到右侧片段上。现在,我打开导航抽屉,选择其中一个选项(1、2、3或4),相应的Fragments被加载到左边的片段中。右边的片段继续显示用户所选的第一个项目中的数据。现在,我按下左边片段上的一个新项,它的数据被加载到右边片段上。甜的。它按我的意愿工作。问题是如果我现在旋转我的屏幕。我得到这个错误:
01-01 15:51:16.356    2569-2569/com.myapp.mgm E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp.mgm/com.myapp.mgm.Fragments}: android.view.InflateException: Binary XML file line #31: Error inflating class fragment
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
            at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3740)
            at android.app.ActivityThread.access$700(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.view.InflateException: Binary XML file line #31: Error inflating class fragment
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
            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:267)
            at android.app.Activity.setContentView(Activity.java:1895)
            at android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java:208)
            at android.support.v7.app.ActionBarActivityDelegateICS.setContentView(ActionBarActivityDelegateICS.java:111)
            at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:76)
            at com.myapp.mgm.Fragments.onCreate(Fragments.java:27)
            at android.app.Activity.performCreate(Activity.java:5133)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
            at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3740)
            at android.app.ActivityThread.access$700(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.IllegalStateException: Fragment com.myapp.mgm.QueryFragment did not create a view.
            at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:308)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
            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:267)
            at android.app.Activity.setContentView(Activity.java:1895)
            at android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java:208)
            at android.support.v7.app.ActionBarActivityDelegateICS.setContentView(ActionBarActivityDelegateICS.java:111)
            at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:76)
            at com.myapp.mgm.Fragments.onCreate(Fragments.java:27)
            at android.app.Activity.performCreate(Activity.java:5133)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
            at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3740)
            at android.app.ActivityThread.access$700(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

有人能解释一下我做错了什么吗?

最佳答案

尝试删除

xmlns:android="http://schemas.android.com/apk/res/android

LinearLayout

关于android - 错误夸大类 fragment fragment 未创建 View ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20870610/

10-09 01:18