如何添加多个自定义视图programaticaly

时间:2017-01-29 18:17:25

标签: java android android-layout for-loop android-custom-view

我在这里有一个小问题,希望你们能帮助我! 我已经创建了一个自定义视图类,它携带一些数据,我试图在我的主要活动中动态添加它,现在当我尝试添加单个视图时,它工作正常。但是,当我把它放在一个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;
    }
}

来自主要活动的代码段,其中包含一些用于测试的静态值:

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位置唯一改变的情况下创建新的视图实例...任何帮助非常感谢,谢谢!

1 个答案:

答案 0 :(得分:1)

问题正是错误所说的:

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

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

您在代码中执行“addView”的唯一时间是:

floorPlan.addView(tv);

所以这意味着floorPlan == null。

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