android自定义view空指针,android – 设置LayoutParams时出现空指针异常

我在programmaticaly中创建了按钮,而不是在xml文件中.然后我想在这个链接中设置LayoutParams:

How to programmatically set the layout_align_parent_right attribute of a Button in Relative Layout?

但是当我试图发射时,我有一个例外.

这是我的代码:

RelativeLayout ll2 = new RelativeLayout(this);

//ll2.setOrientation(LinearLayout.HORIZONTAL);

ImageButton go = new ImageButton(this);

go.setId(cursor.getInt(cursor.getColumnIndex("_id")));

go.setClickable(true);

go.setBackgroundResource(R.drawable.go);

RelativeLayout.LayoutParams params1 = (RelativeLayout.LayoutParams)go.getLayoutParams();

params1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); // LogCat said I have Null Pointer Exception in this line

go.setLayoutParams(params1);

go.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

Intent i3 = new Intent(RecipesActivity.this, MedicineActivity.class);

i3.putExtra(ShowRecipe, v.getId());

i3.putExtra("Activity", "RecipesActivity");

RecipesActivity.this.startActivity(i3);

}

});

ll2.addView(go);

为什么我的应用会抛出异常?谢谢.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值