我在Realtive Layout中使用ImageView和textview。相对布局的Onclcik我需要获取文本View的值并需要更改imageview。获得空指针异常。 这是代码
活动代码
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int id=0;
for (int idx = 0; idx < dataFromDb.size(); idx++) {
id = idx + 1;
rel = (RelativeLayout) inflater.inflate(
R.layout.newdata, null);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 50, 0, 0);
txt = (TextView) rel.findViewById(R.id.fetchData);
txt.setText(dataFromDb.get(idx));
txt.setId(id);
img = (ImageView) rel.findViewById(R.id.tickbox);
img.setImageResource(R.drawable.tickbox1a);
img.setId(id);
rel.setOnClickListener(new ImageView.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(DoTask.this,
"Selected" + v.getId(),
Toast.LENGTH_LONG).show();
RelativeLayout im=(RelativeLayout)v;
ImageView i=(ImageView)im.findViewById(im);
i.setImageResource(R.drawable.tickbox1b);
}
});
content.addView(rel, params);
}
newData.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/tickbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/tickbox1a"
android:paddingBottom="10dp" />
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fetchData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:paddingTop="2dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/gray"
android:textSize="18sp" />
</RelativeLayout>
任何帮助将不胜感激。
答案 0 :(得分:5)
我相信你应该是这样的:
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(DoTask.this,
"Selected" + v.getId(),
Toast.LENGTH_LONG).show();
RelativeLayout im=(RelativeLayout)v;
ImageView i=(ImageView)im.findViewById(R.id.tickbox);
i.setImageResource(R.drawable.tickbox1b);
}
你试图在relativelayout上调用findViewById,它是一个预期和id的视图。 或者只使用上面定义的img,不需要创建另一个imageview实例,因为你已经拥有了它。
答案 1 :(得分:0)
活动代码应为
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int id=0;
for (int idx = 0; idx < dataFromDb.size(); idx++) {
id = idx + 1;
rel = (RelativeLayout) inflater.inflate(
R.layout.newdata, null);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 50, 0, 0);
txt = (TextView) rel.findViewById(R.id.fetchData);
txt.setText(dataFromDb.get(idx));
txt.setId(id);
img = (ImageView) rel.findViewById(R.id.tickbox);
img.setImageResource(R.drawable.tickbox1a);
img.setId(id);
rel.setOnClickListener(new ImageView.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(DoTask.this,
"Selected" + v.getId(),
Toast.LENGTH_LONG).show();
img.setImageResource(R.drawable.tickbox1b);
}
});
content.addView(rel, params);
}