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

How to add multiple custom views programmatically in Android

我这里有个小问题。 我制作了一个包含一些数据的自定义视图 class,我正在尝试将其动态添加到我的主视图 activity 中,现在当我尝试添加单个视图时它工作正常。但是当我把它放在一个 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

自定义视图:

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;
    }
}

来自主要 activity 的代码片段,带有一些用于测试的静态值:

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);
        }

现在这让我很困惑,因为我似乎无法理解这里可能为 null 的东西,我只是想在每次 x 位置是唯一变化的地方时创建新的视图实例。

问题正是错误所说的:

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

您正在尝试执行 null.addView,这导致了错误!

你唯一一次在你的代码中做 "addView" 是在这里:

floorPlan.addView(tv);

所以这意味着 floorPlan == null。

检查初始化 floorPlan 的位置并确保它设置正确:)