晚上好,我正在尝试将按钮简单地添加到现有布局中......在阅读了其他一些答案后,我尝试了这个:
public void addButton(View v)
{
Button cb=new Button(this);
try {
LinearLayout l= (LinearLayout) findViewById(R.layout.main);
//cb.setLayoutParams( new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
l.addView(cb,new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
} catch (Exception e) {
Log.d("EXCEPTION", e.getMessage(),e.getCause());
e.printStackTrace();
}
}
然后我得到一个空指针异常,如下所示:
03-01 22:34:03.967: W/System.err(7047): java.lang.NullPointerException
03-01 22:34:03.967: W/System.err(7047): at custom.component.app.CustomComponentActivity.addButton(CustomComponentActivity.java:49)
03-01 22:34:03.967: W/System.err(7047): at java.lang.reflect.Method.invokeNative(Native Method)
03-01 22:34:03.967: W/System.err(7047): at java.lang.reflect.Method.invoke(Method.java:511)
谁能告诉我那是什么?我什至没有使用线程