我试图在我的列表视图中实现按钮。当用户单击列表视图中的按钮时,它将在后台运行异步任务。但是我得到空指针异常。请帮帮我。
我的自定义适配器
private class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context context, int resource, int textViewResourceId,
List<String> result) {
super(context, resource, textViewResourceId, result);
}
@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
LayoutInflater inflator = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflator.inflate(R.layout.list_row, parent, false);
TextView tv = (TextView) row.findViewById(R.id.title);
tv.setText(code.get(position));
Button btn = (Button) findViewById(R.id.btn_launch);
btn.setTag(position);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new GetUrl()
.execute("https://www.something.com/services/rest/MyAccount/"
+ userid
+ "/"
+ type.get((Integer) v.getTag())
+ "/"
+ code.get((Integer) v.getTag()));
}
});
return row;
}
}
还有我的 list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn_launch"
android:layout_width="80dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/title"
android:layout_marginRight="10dp"
android:background="@drawable/btn"
android:textColor="#ffffff"
android:textSize="12sp"
android:text="@string/launch" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:focusable="false"
android:textColor="#040404"
android:textIsSelectable="true"
android:textSize="15sp"
android:typeface="sans" />
</RelativeLayout>