2012-05-23 30 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 { 
    } 
}