使用片段创建简单的双窗格布局时出现异常

时间:2011-07-08 00:50:18

标签: android android-fragments

我正在尝试使用片段创建一个简单的两个面板布局。这最终会被构建成更复杂的东西,但是现在我只是想让两个空面板显示出不同的背景颜色。我尝试这个时会遇到异常而且我不确定为什么。我使用了Google的示例作为我的指南,可在此处找到:http://developer.android.com/guide/topics/fundamentals/fragments.html#Example

这是我的XML文件fragment_signon.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
    <fragment android:id="@+id/fragmentOptionsPanel" 
        android:layout_width="12dp"
        android:layout_height="match_parent" 
        android:background="@color/black_shadow" />
    <fragment 
        android:id="@+id/fragmentSignOn" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"
        android:background="@color/blue_text" />
</LinearLayout>

这是我的Activity加载片段布局。

public class SignOnFragmentLayout extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.fragment_signon);
    }
}

我在运行时收到以下异常:

07-08 00:06:02.310: ERROR/AndroidRuntime(1486): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mfoundry.boa.ui/com.mfoundry.boa.ui.SignOnFragmentLayout}: android.view.InflateException: Binary XML file line #8: Error inflating class fragment

...

07-08 00:06:02.310: ERROR/AndroidRuntime(1486): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class fragment

我不确定我做错了什么。我不想在布局中加载任何东西,只是为了在左边显示一个特定宽度的面板,然后屏幕的其余部分是另一种颜色。

1 个答案:

答案 0 :(得分:1)

看起来您需要为片段元素指定class属性。 如果你不想加载任何东西,那么如果从构造函数中调用它,就不要在片段中加载任何东西,就像在这里一样。