0

我正在使用 Master-Detail 流的类和 xml 布局(在创建新项目时可用),以并排显示列表和详细视图。但是,我收到此错误:

E/AndroidRuntime(  724): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.TrueFalse/com.TrueFalse.news.NewsModule}: android.view.InflateException: Binary XML file line #14: Error inflating class fragment
E/AndroidRuntime(  724):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1736)
E/AndroidRuntime(  724):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1752)
E/AndroidRuntime(  724):    at android.app.ActivityThread.access$1500(ActivityThread.java:123)
E/AndroidRuntime(  724):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993)
E/AndroidRuntime(  724):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(  724):    at android.os.Looper.loop(Looper.java:126)
E/AndroidRuntime(  724):    at android.app.ActivityThread.main(ActivityThread.java:3997)
E/AndroidRuntime(  724):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(  724):    at java.lang.reflect.Method.invoke(Method.java:491)
E/AndroidRuntime(  724):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
E/AndroidRuntime(  724):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
E/AndroidRuntime(  724):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(  724): Caused by: android.view.InflateException: Binary XML file line #14: Error inflating class fragment
E/AndroidRuntime(  724):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:688)
E/AndroidRuntime(  724):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:724)
E/AndroidRuntime(  724):    at android.view.LayoutInflater.inflate(LayoutInflater.java:479)
E/AndroidRuntime(  724):    at android.view.LayoutInflater.inflate(LayoutInflater.java:391)
E/AndroidRuntime(  724):    at android.view.LayoutInflater.inflate(LayoutInflater.java:347)
E/AndroidRuntime(  724):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:224)
E/AndroidRuntime(  724):    at android.app.Activity.setContentView(Activity.java:1777)
E/AndroidRuntime(  724):    at com.TrueFalse.news.NewsModule.onCreate(NewsModule.java:19)
E/AndroidRuntime(  724):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
E/AndroidRuntime(  724):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1700)
E/AndroidRuntime(  724):    ... 11 more
E/AndroidRuntime(  724): Caused by: java.lang.NullPointerException
E/AndroidRuntime(  724):    at java.lang.VMClassLoader.findLoadedClass(Native Method)
E/AndroidRuntime(  724):    at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:390)
E/AndroidRuntime(  724):    at java.lang.ClassLoader.loadClass(ClassLoader.java:538)
E/AndroidRuntime(  724):    at java.lang.ClassLoader.loadClass(ClassLoader.java:508)
E/AndroidRuntime(  724):    at android.support.v4.app.Fragment.instantiate(Fragment.java:391)
E/AndroidRuntime(  724):    at android.support.v4.app.Fragment.instantiate(Fragment.java:369)
E/AndroidRuntime(  724):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
E/AndroidRuntime(  724):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:660)
E/AndroidRuntime(  724):    ... 20 more

我怀疑这与我没有设置 android:name="" 或 class="" 的事实有关,这可以解释堆栈跟踪,但是上面提到的模板具有相同的 xml 文件改变一些名称。

我的代码跟踪中的行号是

setContentView(R.layout.news_list_container);

我的 NewsModule 类扩展了 android.support.v4.app.FragmentActivity。


/res/values-large/refs.xml(将 news_list_container.xml 映射到 news_twopane_container.xml,我很确定会发生这种情况,因为它在不满足“大:约束”的设备上工作正常。)

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <!-- List of layout overrides for screens that can support iPad style views.
         This should mirror the other refs.xml file-->
    <item name="news_list_container" type="layout">@layout/news_twopane_container</item>
</resources>

/res/layout/news_twopane_container.xml(第 14 行是第二个片段的开头)

<?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/news_list_container"
        android:name="com.TrueFalse.news.NewsList"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1" />

    <fragment
        android:id="@+id/news_detail_container"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1" />

</LinearLayout>
4

2 回答 2

3

当我使用模板时,我忽略了一个事实,即第二个片段实际上应该是片段的占位符,在模板中是一个 FrameLayout。我最初的怀疑是问题所在,因为片段必须有一个与之关联的类。所以 /res/news_twopane_container.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/news_list_container"
        android:name="com.TrueFalse.news.NewsList"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1" />
    <!-- NOT fragment, because we don't have data to put here yet -->
    <FrameLayout
        android:id="@+id/news_detail_container"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1" />

</LinearLayout>
于 2013-01-23T05:27:30.367 回答
0

您可能想通过将 onCreate 方法替换为 onCreateView 来尝试此操作。我想这可能对你有帮助。

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(LOG_TAG, "Loading Container with news_container.xml");
        setContentView(R.layout.news_list_container);
--------------------------------------------------------------------------

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.news_list_container, container, false);
}
于 2013-01-23T04:34:48.637 回答