Android 膨胀xml布局文件时出错

Android 膨胀xml布局文件时出错,android,layout,Android,Layout,我是android编程新手…所以请耐心点 我试图将自定义视图(ArticleView)加载到活动中,其中我的自定义视图定义为activty类的子类。我为包含自定义视图的活动创建了以下xml布局文件: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/MainLa

我是android编程新手…所以请耐心点

我试图将自定义视图(ArticleView)加载到活动中,其中我的自定义视图定义为activty类的子类。我为包含自定义视图的活动创建了以下xml布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/MainLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/ArticleLayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <aarddict.android.ArticleView // here's the line # 14
            android:id="@+id/ArticleView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/ButtonLayout"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="5dp"
        android:layout_marginRight="5dp"
        android:gravity="bottom" >

        <Button
            android:id="@+id/NextButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableRight="@drawable/ic_next"
            android:visibility="gone" />
    </LinearLayout>

</RelativeLayout>
代码:


您不能将
活动
放在
活动
中,如果要创建自定义视图,您将希望
扩展视图
或其子视图(
线性布局
/
相对视图
/
文本视图


正确答案是错误的。您可以在项目上定义不同的活动。 错误很简单
aarddict.android.ArticleView 必须是
yourpackagename.ArticleView

您不能在版面中粘贴活动。我不确定您试图完成什么,但您当前所做的将不起作用。您应该对视图进行子类化,或者对其现有子类之一进行子类化。
    05-24 23:41:06.685: E/AndroidRuntime(600): java.lang.RuntimeException: Unable to start activity ComponentInfo{aarddict.alidev.android/aarddict.alidev.android.ArticleViewActivity}: android.view.InflateException: Binary XML file line #14: Error inflating class aarddict.android.ArticleView
05-24 23:41:06.685: E/AndroidRuntime(600):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
05-24 23:41:06.685: E/AndroidRuntime(600):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-24 23:41:06.685: E/AndroidRuntime(600):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-24 23:41:06.685: E/AndroidRuntime(600):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-24 23:41:06.685: E/AndroidRuntime(600):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-24 23:41:06.685: E/AndroidRuntime(600):  at android.os.Looper.loop(Looper.java:137)
05-24 23:41:06.685: E/AndroidRuntime(600):  at android.app.ActivityThread.main(ActivityThread.java:4424)
05-24 23:41:06.685: E/AndroidRuntime(600):  at java.lang.reflect.Method.invokeNative(Native Method)
interface ScrollListener {
    void onScroll(int l, int t, int oldl, int oldt);
}

private ScrollListener scrollListener;

public ArticleView(Context context) {
    super(context);
}

public ArticleView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public ArticleView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
    super.onScrollChanged(l, t, oldl, oldt);
    if (scrollListener != null) {
        scrollListener.onScroll(l, t, oldl, oldt);          
    }
}

public void setOnScrollListener(ScrollListener l) {
    this.scrollListener = l;
}
}