0

我试图在我的列表视图中实现按钮。当用户单击列表视图中的按钮时,它将在后台运行异步任务。但是我得到空指针异常。请帮帮我。

我的自定义适配器

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>
4

5 回答 5

3

在 getView 方法中使用 row.findViewById() 而不是仅使用 findViewById 方法

于 2013-10-21T06:38:11.253 回答
2

您可以像这样优化和提高 Listview 的 性能

于 2013-10-21T06:46:46.117 回答
1

用这个

Button btn = (Button) row.findViewById(R.id.btn_launch);

这里发生 NPE 是因为您没有给出放置按钮的视图。因此,如果您在自定义适配器中执行任何 eventListner,请始终指定您所指的视图

于 2013-10-21T06:39:00.173 回答
0

试试这个代码

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) row.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;
}

}

于 2013-10-21T06:41:10.573 回答
0

尝试这个,

Button btn = (Button) row.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()));

        }
    });
于 2013-10-21T06:39:52.200 回答