我想将LinearLayout
充气到" onCreateView"方法。但我总是犯同样的错误:
"android.view.InflateException: Binary XML file line #7: Error inflating class fragment"
我在android.support.v4.app.FragmentActivity
文件夹中使用"libs"
。
主要活动:
public class GeofranceDeptActivity extends FragmentActivity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dept);
}
}
主xml文件dept
:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment class="dauran.geofrance.DeptFragment"
android:id="@+id/depts" android:layout_weight="1"
android:layout_width="0px"
android:layout_height="match_parent">
</fragment>
<FrameLayout android:id="@+id/detailsdept" android:layout_weight="1"
android:layout_width="0px"
android:layout_height="match_parent" />
</LinearLayout>
片段DeptFragment
的类:
public class DeptFragment extends Fragment implements OnTouchListener{
Context mContext = getActivity();
boolean mDualPane;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
return (LinearLayout)inflater.inflate(R.layout.dept_fragment, container, false);
}
@Override
public void onActivityCreated(Bundle savedState) {
................etc
我要膨胀的xml文件dept_fragment
:
<?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="vertical" >
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/targetDeptImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/region_france_grand" />
<ImageView
android:id="@+id/cacheDeptImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/region_france_grand" />
<Spinner
android:id="@+id/sDepartement"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignRight="@+id/targetImage" />
</RelativeLayout>
</LinearLayout>
Logcat
:
06-26 19:02:27.940: E/AndroidRuntime(11993): FATAL EXCEPTION: main
06-26 19:02:27.940: E/AndroidRuntime(11993): java.lang.RuntimeException: Unable to start activity ComponentInfo{dauran.geofrance/dauran.geofrance.GeofranceDeptActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
06-26 19:02:27.940: E/AndroidRuntime(11993): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
06-26 19:02:27.940: E/AndroidRuntime(11993): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
06-26 19:02:27.940: E/AndroidRuntime(11993): at android.app.ActivityThread.access$600(ActivityThread.java:123)
06-26 19:02:27.940: E/AndroidRuntime(11993): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
06-26 19:02:27.940: E/AndroidRuntime(11993): at android.os.Handler.dispatchMessage(Handler.java:99)
06-26 19:02:27.940: E/AndroidRuntime(11993): at android.os.Looper.loop(Looper.java:137)
06-26 19:02:27.940: E/AndroidRuntime(11993): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-26 19:02:27.940: E/AndroidRuntime(11993): at java.lang.reflect.Method.invokeNative(Native Method)
06-26 19:02:27.940: E/AndroidRuntime(11993): at java.lang.reflect.Method.invoke(Method.java:511)
06-26 19:02:27.940: E/AndroidRuntime(11993): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-26 19:02:27.940: E/AndroidRuntime(11993): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-26 19:02:27.940: E/AndroidRuntime(11993): at dalvik.system.NativeStart.main(Native Method)
06-26 19:02:27.940: E/AndroidRuntime(11993): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
06-26 19:02:27.940: E/AndroidRuntime(11993): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
06-26 19:02:27.940: E/AndroidRuntime(11993): at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
06-26 19:02:27.940: E/AndroidRuntime(11993): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
06-26 19:02:27.940: E/AndroidRuntime(11993): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
06-26 19:02:27.940: E/AndroidRuntime(11993): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
06-26 19:02:27.940: E/AndroidRuntime(11993): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
06-26 19:02:27.940: E/AndroidRuntime(11993): at android.app.Activity.setContentView(Activity.java:1835)
06-26 19:02:27.940: E/AndroidRuntime(11993): at dauran.geofrance.GeofranceDeptActivity.onCreate(GeofranceDeptActivity.java:10)
06-26 19:02:27.940: E/AndroidRuntime(11993): at android.app.Activity.performCreate(Activity.java:4465)
06-26 19:02:27.940: E/AndroidRuntime(11993): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-26 19:02:27.940: E/AndroidRuntime(11993): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
06-26 19:02:27.940: E/AndroidRuntime(11993): ... 11 more
06-26 19:02:27.940: E/AndroidRuntime(11993): Caused by: java.lang.IllegalStateException: Fragment dauran.geofrance.DeptFragment did not create a view.
06-26 19:02:27.940: E/AndroidRuntime(11993): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:293)
06-26 19:02:27.940: E/AndroidRuntime(11993): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:669)
答案 0 :(得分:2)
Logcat
可能更多地说 android.view.InflateException:二进制XML文件行#7:错误膨胀类片段(检查下面是否有实例化例外,也许你可以添加完整的异常?)。在查看您的代码之后,我会假设您的DeptFragment
不在dauran.geofrance
包中,因为这个android无法找到它。
此外,无需测试null
container
参数:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.dept_fragment, container, false);
}
此外,我认为不需要在RelativeLayout
中使用LinearLayout
包裹R.layout.deptfragment
:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/targetDeptImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/region_france_grand" />
<ImageView
android:id="@+id/cacheDeptImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/region_france_grand" />
<Spinner
android:id="@+id/sDepartement"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignRight="@+id/targetImage" />
</RelativeLayout>