0

关于此的文档确实令人困惑。我只需要将矩形添加到我在 main.xml 布局文件中定义的视图中。这将是布局的一小部分。

我想要实现的是,我想在房间中添加架子,但由于房间形状和架子发生了变化,我需要以编程方式添加它们。

下面是我的 main.xml 文件的一小部分,你可以看到我定义的视图:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/relativeLayout2"
    android:layout_width="600dp"
    android:layout_height="650dp"
    android:layout_marginTop="70dp"
    android:layout_marginLeft="30dp"
    android:layout_toRightOf="@+id/relativeLayout1" >

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="@string/getDirections"
        android:textSize="20dp"
        android:textStyle="bold" />

    <View
        android:id="@+id/roomplan"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
            android:layout_below="@+id/textView3"
        android:layout_marginTop="40dp"
        android:background="@android:color/white" />

</RelativeLayout>

这是我为处理动态更改而创建的自定义 View 类:

public class CustomView extends View {
    ShapeDrawable roomFrame;
    ArrayList<ShapeDrawable> shelfFrames;

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

    @Override
    protected void onDraw(Canvas canvas){
        super.onDraw(canvas);
        roomFrame.draw(canvas);
        for (ShapeDrawable shelfFrame : shelfFrames){
            shelfFrame.draw(canvas);
        }
    }

    public void setRoom(Stage stage){
        roomFrame = new ShapeDrawable(new RectShape());
        roomFrame.getPaint().setColor(0xff74AC23);   
        roomFrame.setBounds(10, 10, stage.getWidth(), stage.getHeight());
    }

    public void setShelves(ArrayList<Shelf> shelves){
        shelfFrames = new ArrayList<ShapeDrawable>();
        for(int i = 0; i<shelves.size(); i++){
            ShapeDrawable shelfFrame = new ShapeDrawable(new RectShape());
            shelfFrame.getPaint().setColor(0xff74AC23);
            shelfFrame.setBounds(shelves.get(i).getXPosition(), shelves.get(i).getYPosition(), shelves.get(i).getWidth(), shelves.get(i).getHeight());
            shelfFrames.add(shelfFrame);
        }
    }
}

现在简单地说,当询问新房间计划时,我试图将此类分配给 xml 布局中的 View 对象:

public void loadRoomPlan(Room room, ArrayList<Shelf> shelves){
    CustomView asdsView = (CustomView)findViewById(R.id.roomplan);
    asdsView.setRoom(room);
    asdsView.setShelves(shelves);
    asdsView.invalidate();
}

我总是得到

引起:java.lang.ClassCastException: android.view.View 无法转换为 org.example.myproject.CustomView

错误。

可能我这样做非常非常错误,不是吗?

4

1 回答 1

0

错误似乎在这一行:

CustomView asdsView = (CustomView)findViewById(R.id.shopplan);

What is shopplan? In case it is a mistake and you meant R.id.roomplan try to subtitute the View in your layout for your Custom view:

<org.example.myproject.CustomView
    android:id="@+id/roomplan"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView3"
    android:layout_marginTop="40dp"
    android:background="@android:color/white" />

UPDATE:

Try adding the other two constructors to your CustomView class:

public CustomView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

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

When you use a custom view in an xml layout, your view has to deal with the layout attributes (the constructor AttributeSet param).

于 2012-06-02T23:06:37.917 回答