开发者

Custom ViewFlipper not working?

Here is a simplified code example of what I'm trying to do:

public class main extends Activity {

    public myFlipper flipper;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        flipper = (myFlipper) findViewById(R.id.viewFlipper1);
    }

    class myFlipper extends ViewFlipper
    {

        public myFlipper(Context context)
        {
            super(context);
            // TODO Auto-generated co开发者_运维技巧nstructor stub
        }

    }

}

Java throws a classCastException when I try to do this, what am I doing wrong?


You must declare your custom view on the XML too:

<view class="your.package.main$myFlipper"
    android:layout_width="fill_parent"
    etc.
/>

Extra advice: classes should be declared with the first letter in upper case.


I figured it out to those of you who are interested.

You need to add a constructor that allows for Layout attributes in your custom view class. Like so:

    public myFlipper(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

And you need to follow Cristian´s example as well. Then it should work :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜