2012-05-24 93 views
0

我是新到Android編程......所以請耐心等待...錯誤充氣XML佈局文件

我想自定義視圖(ArticleView)加載到活動中,被定義爲一個在我的自定義視圖活動課的子類。我創建了包含自定義視圖中的活性下面的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> 

會發生什麼事是我保持在得到運行時錯誤(應用程序死亡),看在logcat中我得到:

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; 
} 
} 
+0

你不能在佈局堅持一個活動。我不確定你想要完成什麼,但你現在正在做的事情是行不通的。 – dymmeh

+0

您應該繼承View或其現有子類之一。 – Simon

回答

0

正確的答案是錯的。您可以在項目中定義不同的活動。 錯誤是簡單
aarddict.android.ArticleView 必須 yourpackagename.ArticleView