私はここにわずかな問題があります、皆さんが私を助けてくれることを願っています! 私はいくつかのデータを運ぶカスタムビュークラスを作っています。私は主なアクティビティに動的に追加しようとしています。しかし、私は、forループの中に入れて、私はこのエラーを持つ複数のコードブレークを追加したいと言うことができますとき:複数のカスタムビューを追加する方法programaticaly
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位置がどこにあるビューのたびに新しいインスタンスを作成しようとしています変化する唯一の事は...どんな助けも大変ありがとう!ありがとう!
Attempt to invoke method addView(android.view.View)' on a null object reference
あなたがエラーの原因となっているnull.addViewを、やろうとしている。
うん、ちょうど...笑自分自身ことに気づきました – vibetribe93