基本上我想要两个观点。一个视图用于画布,用于绘制矩形和另一个视图或在绘制矩形时添加新的editText框。当我运行我的程序时,出现错误“java.lang.NullPointerException”。是否可以将edittext框添加到画布视图中,以便我只保留一个视图?
我的代码如下:
public class MainActivity extends Activity {
public static DrawRect DR;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DR = new DrawRect(this);
rectbutton = (Button) findViewById(R.id.rectbutton);
RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.RelativeLayout);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
lp.addRule(RelativeLayout.BELOW, R.id.rectbutton);
DR.setLayoutParams(lp);
mainLayout.addView(DR);
rectbutton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
addRect(); // in my DrawRect class i have this method to draw rect on canvas
addEditText();
}// onclick
});
}
private void addEditText(){
RelativeLayout editTextLayout = new RelativeLayout(this);
EditText editText = new EditText(this);
editTextLayout.addView(editText);
mainLayout.addView(editTextLayout);
}
}
请咨询。谢谢。
答案 0 :(得分:0)
哪一行给出错误。如果它在
中给出错误 mainLayout.addView(editTextLayout);
表示mainLayout为null。您可以在画布中添加EditText。 问题在于
RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.RelativeLayout);
检查布局xml中的id。