简体   繁体   English

如何在 Android 中以编程方式添加多个自定义视图

[英]How to add multiple custom views programmatically in Android

I have a slight issue here.我这里有一个小问题。 I've made a custom view class that carries some data and I am trying to add it dynamically in my main activity, now when I try to add a single view it works fine.我制作了一个带有一些数据的自定义视图类,我试图在我的主要活动中动态添加它,现在当我尝试添加单个视图时,它工作正常。 But when I put it in a for loop and let's say I want to add more than one the code breaks with this error:但是,当我将它放入 for 循环并假设我想添加多个时,代码会因此错误而中断:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.digiart.xapp/com.digiart.xapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.addView(android.view.View)' on a null object reference

custom view:自定义视图:

public class TableView extends View {

    private static final String TAG = "TableView";
    private int numberOfSeats;
    private int tableId;
    private int positionX;
    private int positionY;
    private int objectWidth;
    private int objectHeight;
    private boolean isTaken;
   private  String tableKind;


     private Rect rectangle;
    private Paint paint;


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

    }
    public TableView(Context context,int numberOfSeats,int tableId,int positionX,int positionY,int width,int height
    ,boolean isTaken, String tableKind) {
        super(context);

        this.numberOfSeats = numberOfSeats;
        this.tableId = tableId;
        this.positionX = positionX;
        this.positionY = positionY;
        this.objectWidth = width;
        this.objectHeight = height;
        this.isTaken = isTaken;
        this.tableId = tableId;
        this.tableKind = tableKind;

        //defining shape
        rectangle = new Rect(positionX,positionY,width,height);
        //defining shape color
        paint = new Paint();
        paint.setColor(Color.GRAY);
        Log.i(TAG, "TableView: tableId: "+tableId+" isTaken: "+isTaken);


    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //canvas.drawColor(Color.BLUE);
        canvas.drawRect(rectangle, paint);

    }

    public int getNumberOfSeats() {
        return numberOfSeats;
    }

    public int getTableId() {
        return tableId;
    }

    public int getPositionX() {
        return positionX;
    }

    public int getPositionY() {
        return positionY;
    }

    public int getObjectWidth() {
        return objectWidth;
    }

    public int getObjectHeight() {
        return objectHeight;
    }

    public boolean isTaken() {
        return isTaken;
    }

    public String getTableKind() {
        return tableKind;
    }
}

code snippet from main activity with some static values for testing:来自主要活动的代码片段,带有一些用于测试的静态值:

for (int i = 0;i<15;i++) {
           tv = new TableView(MainActivity.this, numberOfSeats, tableId, positionX, positionY, objectWidth, objectHeight
                    , isTaken, tableKind);
            positionX +=20;
            floorPlan.addView(tv);
        }

Now this is quite confusing for me because I can not seem to grasp what could be null here, I am just trying to make new instance of view every time where the x position is the only thing that changes.现在这对我来说很困惑,因为我似乎无法理解这里可能为空的内容,我只是试图在每次 x 位置是唯一改变的地方时创建新的视图实例。

The problem is exactly what the error says:问题正是错误所说的:

Attempt to invoke method addView(android.view.View)' on a null object reference

You are trying to do null.addView, which is causing the error!您正在尝试执行 null.addView,这会导致错误!

The only time you do "addView" in your code is here:您在代码中执行“addView”的唯一时间是在这里:

floorPlan.addView(tv);

So this means that floorPlan == null.所以这意味着 floorPlan == null。

Check where you initialise floorPlan and ensure that it is being set correctly :)检查你在哪里初始化 floorPlan 并确保它被正确设置:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM