您好我在使用Android 2.3.3时遇到此异常:
android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
此方法发生异常: (创建片段时)
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
// We have different layouts, and in one of them this
// fragment's containing frame doesn't exist. The fragment
// may still be created from its saved state, but there is
// no reason to try to create its view hierarchy because it
// won't be displayed. Note this is not needed -- we could
// just run the code below, where we would create and return
// the view hierarchy; it would just never be used.
return null;
}
try {
//exception here
theLayout = (LinearLayout) inflater.inflate(R.layout.fragment1, container, false);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return theLayout;
}
当我转到Activity并再次返回FragmentActivity并切换标签时会发生这种情况。
我应该为这个做些什么?
感谢。
答案 0 :(得分:0)
我得到了同样的错误(但我使用的是MonoDroid和C#)。在我的情况下,我试图给包含<fragment>
标签的布局充气,但操作系统(Android 2.3.3)不知道片段。
我的解决方案是使用Android Support Package v4(android-support-v4.jar,可以在<android-sdk>/extras/android/support/v4
文件夹中找到)。然后我使用SupportingFragmentManager创建了片段。
可以在此处找到带有示例代码(可轻松转换为Java)的MonoDroid说明:http://docs.xamarin.com/android/tutorials/Fragments/Part_4_-_Providing_Backwards_Compatibility_with_the_Android_Support_Package