android - NPE when adding inflated button -
i try create new button programatically , existing viewgroup (i moved custom classical linearlayout ensure bug not in custom viewgroup). a code simple , working in different use case: private void appendtile() { view view = getlayoutinflater().inflate(r.layout.template_tile, null); view.setid(view.generateviewid()); view.setonclicklistener(tilelistener); hiddenpicture.addview(view, view.getlayoutparams()); } template_tile.xml: <button xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" style="@style/formulavalue" /> but fails, because view.getlayoutparams() null (though there layout_width , layout_height in xml). caused by: java.lang.nullpointerexception: attempt read field 'int android.view.viewgroup$layoutparams.width' on null object reference @ android.view.viewgroup$layoutparams.<init...