public class StartDraw extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
MyView view1 =new MyView(this);
// view1.setBackgroundResource(R.drawable.ic_launcher);
setContentView(view1);
}
public class MyView extends View implements OnTouchListener{
RelativeLayout.LayoutParams LP;
ViewGroup dlayout;
ImageView[] ActionI = new ImageView[4];
ImageView[] ReciverI = new ImageView[4];
public MyView(Context c) {
super(c);
// dlayout = new ViewGroup(c); // if i use this then error shown {Cannot instantiate the type ViewGroup}
LP = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
// ViewGroup dlayout = (ViewGroup)findViewById(R.id.dlayout);
dlayout.setLayoutParams(LP); // at this position gettting null pointer exception
}
@Override
protected void onDraw(Canvas canvas) {
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(2);
paint.setColor(Color.GREEN);
canvas.drawColor(Color.BLUE);
canvas.drawLine(offset_x, offset_y, currentX, currentY, paint);
}
}
问问题
95 次
3 回答
2
设置内容视图(R.layout.main);删除此评论并启用它
setContentView(R.layout.main); remove this comments and enable it
设置内容视图(R.layout.main);删除此评论并启用它
于 2013-04-22T06:33:56.213 回答
0
您已经注释掉了dlayout
初始化。因此它是空的。导致NullPointerException在dlayout.setLayoutParams
.
如果您对此原因/结果有任何不理解的地方,请扩展您的问题以澄清。但是,如果您不确定如何初始化 dlayout 变量,请将其作为它自己的问题。
于 2013-03-15T07:14:26.577 回答
0
您尝试设置未初始化的对象的属性
dlayout.setLayoutParams(LP);
取消注释上面的代码,应该修复错误。
// ViewGroup dlayout = (ViewGroup)findViewById(R.id.dlayout);
于 2013-03-15T07:14:26.957 回答