我遇到了这个问题:
android.view.InflateException:二进制XML文件行#31:错误膨胀类片段 片段com.myapp.mgm.QueryFragment没有创建视图。
此问题仅在平板电脑模式下发生。如果应用程序在手机上运行,则可以正常运行。
我有这个layout-large
xml文件(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();
}
}
}
我的QueryFragment
课就是这样:
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;
}
}
我的InfoData
课就是这个东西:
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
}
}
基本上我在左边的片段(listview
)上有一个QueryFragment
,在用户选择其中一个项目(带有数据的光标)之后,右边的片段(InfoData
)是填充了此游标的提取数据。
如果我从左侧片段中选择一个intem并且它的数据填充在右侧片段上,因为我在onSaveInstanceState
上保存数据,如果我旋转屏幕,一切正常,即数据重新加载正确的片段。
现在,我的Navigadion Drawer为4个选项。这些选项中的每一个都在左侧片段上加载listView
,然后,如前所述,如果用户选择其中一个项目,则将其数据加载到右侧片段上。
以下是我Fragments
课程中处理此问题的代码:
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),并在左侧片段中加载相应的listView
。右侧片段始终显示用户选择的第一个项目的数据。现在,我按下左侧片段上的新项目,并将其数据加载到右侧片段上。甜。它正如我想的那样工作。问题是如果我现在旋转我的屏幕。我收到了这个错误:
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)
有人可以解释一下我做错了吗?
答案 0 :(得分:1)
尝试删除
xmlns:android="http://schemas.android.com/apk/res/android
来自LinearLayout
。
答案 1 :(得分:0)
您可以尝试将布局作为片段的容器放置,该容器将被新片段替换。而不是直接在QueryFragment
中嵌入fragments.xml
。
1)fragments.xml:
<!-- change fragment1 to a layout -->
<FrameLayout
android:id="@+id/fragment1_container"
.../>
2)碎片onCreate():
setContentView(R.layout.fragments);
// add QueryFragment to the container layout
getSupportFragmentManager()
.beginTransaction()
.add(R.id.fragment1_container, new QueryFragment())
.commit();
3)片段selectItem():
if (parentView != null) {
typeOfLayout = R.id.fragmentsboth;
} else {
// change to the container layout id
typeOfLayout = R.id.fragment1_container;
}
Error inflating class fragment | fragment did not create a view
答案 2 :(得分:0)
Android中常见的挑战是可能频繁破坏和重建活动。发生这种情况的最常见时间是用户在横向和纵向方向之间旋转设备。这种崩溃通常会发生,因为设备方向更改会导致Android框架在任何包含的视图中拆除显示的Activity,然后完全重建Activity / View层次结构。对活动或视图的任何引用都会突然变为无效。
注意:此问题可以通过包含: -
来解决 android:configChanges="orientation"
android属性在android清单中的所有活动中。这可以防止由方向变化引起的拆卸/重建过程
此外,在Fragment的构造函数中不要忘记这样做
public MyFragment()
{
setRetainInstance(true);
}
使用值为true调用setRetainInstance()
会导致Android在活动的拆除/重建周期中保留片段。与片段或视图中包含的视图或其他对象引用一起保留。
将此设置为true,Android: -
在尝试了所有这些疯狂的解决方案之后,这给了我很多,这是唯一有效的解决方案。