我想以编程方式将ImageView添加到bodyView方法中的左中心Horizontal。我尝试过以下方式,但没有用。
private LinearLayout makeBodyView() {
LinearLayout bodyView = new LinearLayout(this);
LinearLayout.LayoutParams bp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
//imgrightis imageview
imgright.setBackgroundResource(R.drawable.img_r);
bp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.LEFT;
imgright.setLayoutParams(bp);
bodyView.addView(imgright);
gridView = new SkyGridView(this);
int cn = this.getNumColumns();
gridView.setNumColumns(cn);
gridView.setHorizontalSpacing(px(20));
gridView.setVerticalSpacing(px(15));
gridView.setPadding(px(20), px(49), px(18), px(20));
gridView.setOnItemClickListener(onClick);
gridView.setOnItemLongClickListener(onLongClick);
gridAdapter = new SkyGridAdapter(this);
LayoutParams gp = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
gridView.setLayoutParams(gp);
bodyView.addView(gridView);
bodyView.setBackgroundResource(R.drawable.book1);
return bodyView;
}
Logcat错误:
java.lang.RuntimeException:无法启动活动ComponentInfo {com.adventurewalking / com.adventurewalking.activities.EpubReaderActivity}:java.lang.NullPointerException:尝试调用虚方法' void android.widget.ImageView。 setBackgroundResource(INT)'在空对象引用上
任何人都可以帮助我吗?