2011-11-26 30 views
1

是否有可能在活動類中擴展SurfaceView作爲內部類的類?事情是這樣的:帆布在內部類中,android

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 
<com.android.iiProject.Presentation.DrawCanvas 
android:id="@+id/SurfaceView" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
/> 
</LinearLayout> 

Presentation是活動類,而DrawCanvas是擴展SurfaceView類和DrawCanvas對象從Presentation活動中調用。

我想要這樣,因爲我想要訪問Presentation活動中可用的方法和變量,我不想聲明它們是靜態的以便從其他類到達它們,因爲這會導致我很多額外的問題...! 這樣,它給了我classNotFoundException在XML文件

感謝很多提前

回答

4

是的,這是可能的,你只需要ADRESS你從XML不同的方式內部類,像這樣:

<view 
    class="com.yourpackage.YourOuterClass$YourInnerClass" 
    ... 
    /> 

引用:

自定義組件被創建爲在XML的通用視圖,以及使用所述指定了 類完整的包裝。 另請注意,我們定義的內部類是使用NoteEditor $ MyEditText 表示法引用的,該表示法是引用Java 編程語言中的內部類的標準方式。如果您的自定義View組件未定義爲內部類 ,則可以替代地使用XML元素名稱聲明組件,並排除class屬性。

爲一個確切的答案見modifying an existing view type,點4

+0

+1。 –

+1

謝謝你的回覆,它的工作! 但是現在當我引用DrawCanvas(內部類)時,我得到一個classCastException,因爲它被聲明爲XML中的通用對象「View」,不應該被轉換爲DrawCanvas?! 有問題的句子: DrawCanvas mycanvas =(DrawCanvas)findViewById(R.id.SurfaceView); – NZal