如何在运行时在android中动态定位编辑文本?
以下是我的代码:
public class ABCActivity extends Activity {
EditText ed;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ed = new EditText(ABCActivity.this);
ed.setEnabled(false);
layout =new LinearLayout(this);
LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT, 1.0f);
layout.setLayoutParams(params);
btn = new Button(this);
btn.setLayoutParams(params);
btn.setText("Change");
RelativeLayout.LayoutParams params2 = (RelativeLayout.LayoutParams) ed.getLayoutParams();
params2.leftMargin = 10;
params2.topMargin = 10;
ed.setLayoutParams(params2);
layout.addView(btn);
layout.addView(ed);
}
}
给力关闭:
RelativeLayout.LayoutParams params2 = (RelativeLayout.LayoutParams) ed.getLayoutParams();
错误:空指针异常我尝试使用相对布局.....但不起作用