在android listview中实现按钮时出现空指针异常

时间:2013-10-21 06:35:57

标签: android android-layout android-listview

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

我的自定义适配器

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>

5 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:2)

你可以优化和提高Listview的性能 THIS

答案 2 :(得分:1)

使用此

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

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

答案 3 :(得分: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()));

        }
    });

答案 4 :(得分: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;
}

}