2012-05-23 31 views
0

我已經定義一個類XX這樣的:這個任務有什麼問題?

public class XX extends RelativeLayout { 
    protected static final boolean DEBUG = true; 

    public XX(Context context) { 
    super(context); 
    // TODO Auto-generated constructor stub 
    } 

    public XX(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    if (DEBUG) 
     Log.i(this.getClass().getSimpleName(), " ->2" 
      + Thread.currentThread().getStackTrace()[2].getMethodName()); 
    //getAttributes(context, attrs); 

    } 

    public XX(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    if (DEBUG) 
     Log.i(this.getClass().getSimpleName(), " ->3" 
      + Thread.currentThread().getStackTrace()[2].getMethodName()); 
    //getAttributes(context, attrs); 

    } 

} 

,並在我的代碼我寫:

RelativeLayout v = (RelativeLayout) this.findViewById(R.id.switch_TCMyTB); 
XX x = (XX) v; //<----- crashes here 

,但它與分配崩潰。我假設因爲XX擴展視圖我可以將視圖(RelativeLayout)分配給XX對象。

但它崩潰。這項任務有什麼問題?

編輯:

我改變extends Viewextends RelativeLayout。還將View v更改爲RelativeLayout v。 但我仍然得到classCastException .. ????爲什麼?

雖然

RelativeLayout r = (RelativeLayout) v; 

肯定能正常工作。

+0

什麼樣的碰撞? ClasscastException?你能否告訴我們發生了什麼? – Gomoku7

+0

你設置你的活動內容有何看法? 哪會崩潰嗎?等級豁免?你可以發佈你的佈局文件(excerp?) – Fabian

+0

我不確定這是否會工作,但你有沒有試過XX x =(XX)this.findViewById(...); ?我以前沒有做的自定義組件,因此它只是想沿着你的問題。 –

回答

0

由於我對定製組件不完全熟悉,我只是試着舉個例子。 我無法回答你的問題,爲什麼它不會工作。如果我的示例不能爲您運行,則需要提供一個logcat。

創建自定義類:

public class TestLayout extends RelativeLayout { 

    public TestLayout(Context context) { 
     super(context); 

    } 

    public TestLayout(Context context, AttributeSet attrs) { 
     super(context, attrs); 

    } 

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

這個類是在包com.test.cc

在XML佈局我使用

<com.test.cc.TestLayout 
    android:layout_width="10dip" 
    android:layout_height="10dip" 
    android:id="@+id/testLayout"  
    /> 

在此示例佈局。 。TestLayout是LinearLayout的子項。 如果其xml佈局中的最高級別組件添加xmlns:android="http://schemas.android.com/apk/res/android"

然後在活動:

//Called in onCreate mthod of an activity.  
setContentView(R.layout.test); //make sure you call this first 
TestLayout l = (TestLayout)findViewById(R.id.testLayout); 

這將運行對我罰款。

這是在2.2和3.2設備上測試的。

所以一定要調用的setContentView(...),然後再創建佈局的對象。 還要確保在xml定義中包是正確的。但如果這是錯誤的,你會得到一個類不能foudn例外

編輯||

我嘗試運行此:

RelativeLayout l = (RelativeLayout)findViewById(R.id.testLayout); 
TestLayout tl = (TestLayout)l; 

而且它也運行良好,沒有任何異常。所以我認爲問題在於別處。也許包名稱錯誤或東西。

+0

我只是重複了一切 - 按照您的步驟操作,現在它可以工作 - 真的不知道現在有什麼不同。 (知道有什麼不同:-)非常感謝! – user387184

+0

問題在XML或進口最有可能撒了謊。這是我能想到的唯一解釋。儘管你很歡迎。很高興我能幫上忙 :) –

0

下面的代碼運行沒有ClassCastExceptions
所以我的猜測是錯誤可能在於XML。

public class Test { 

    public static void main(String[] args) { 
    B b = new B(); 
    Object o = b; 
    A a = (A) o; 
    B b2 = (B) a; 
    System.out.println("done"); 
    } 

    public static class A /* extends Object */ { 
    } 

    public static class B extends A { 
    } 
}