2015-10-08 43 views
2

我在引用另一個類的子類時遇到了困難,該類是我的主佈局XML文件中的佈局。我在我的Android應用程序下面的類結構:在Android佈局XML中引用內部類

 public class MainClass { 
     .... 
     .... 
      public class SubClassView extends LinearLayout { 
       public SubClassView(Context context, AttributeSet attrs) { 
        super(context, attrs); 
        ..... 
       } 
     .... 
     .... 
      } 
     } 

和我喜歡引用這在我的佈局,使:

<view class = ".MainClass$SubClassView" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:id="@+id/button"/> 

但我不斷收到錯誤:

android.view.InflateException: Binary XML file line #35: Error inflating class 
.... 
.... 
Caused by: android.view.InflateException: Binary XML file line #35: Error inflating class MainClass$SubClassView 
.... 
.... 
Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]   

當這個類不是一個子類時,我沒有遇到任何問題。任何人有什麼想法,我要去哪裏錯了?

+0

一直試圖解決這個問題幾個小時,但你的代碼讓我意識到,我必須使用小寫的「視圖」和正確的語法來寫在XML中。萬分感謝! – karenms

回答

6

您的SubClassView需要爲static,才能在您的佈局xml中使用它。

... 
public static class SubClassView extends LinearLayout { 
...