0

我已经制作了 ListView 适配器并在每一行中实现了唯一的 URL,并且在 XML 中使用相同的按钮 id,现在每当我尝试单击按钮时,它都会显示Force Close

PS:我是新手

public class ResultListBaseAdapter extends BaseAdapter  {
    private static ArrayList<ItemResults> ItemResultsarraylist;
    public static String[] metador = new String[100];
    String manu;
    String mat;

    private LayoutInflater l_Inflater;
    private Context context;
    protected Context activity;

    public ResultListBaseAdapter() {
    }

    public ResultListBaseAdapter(Context context, ArrayList<ItemResults> resultc) {
        ItemResultsarraylist = resultc;
        l_Inflater = LayoutInflater.from(context);
        this.context=context;
    }

    public int getCount() {
        return ItemResultsarraylist.size();
    }

    public Object getItem(int position) {
        return ItemResultsarraylist.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = l_Inflater.inflate(R.layout.item_results_view, null);
            holder = new ViewHolder();
            holder.txt_date = (TextView) convertView.findViewById(R.id.date);
            holder.txt_type = (TextView) convertView.findViewById(R.id.type);
            holder.txt_subject = (TextView) convertView.findViewById(R.id.subject);
            holder.txt_teacher = (TextView) convertView.findViewById(R.id.teacher);

            holder.url =(Button) convertView.findViewById(R.id.btns);


            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.txt_date.setText(ItemResultsarraylist.get(position).getDate());
        holder.txt_type.setText(ItemResultsarraylist.get(position).gettype());
        holder.txt_subject.setText(ItemResultsarraylist.get(position).getsubject());
        holder.txt_teacher.setText(ItemResultsarraylist.get(position).getteacher());

        holder.url.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v )
            {
                mat=ItemResultsarraylist.get(position).geturl1();
                System.out.println("Mat value is"+mat);
                Uri uri = Uri.parse(mat);
                Intent intent = new Intent(Intent.ACTION_VIEW, uri); 
                System.out.println("Uri is "+intent);
                activity.startActivity(intent);
            }
        });
        return convertView;
    }       
    static class ViewHolder {
        TextView txt_date;
        TextView txt_type;
        TextView txt_subject;
        TextView txt_teacher;
        Button url;

    }
}

当我点击按钮时,这里有我的 logcat 详细信息

02-03 14:38:08.379: I/System.out(556): Mat value ishttp://docs.google.com/gview?embedded=true&url=http://baseballglovessubhash.com/pdf/pic6.pdf
02-03 14:38:08.379: I/System.out(556): Uri is http://docs.google.com/gview?embedded=true&url=http://baseballglovessubhash.com/pdf/pic6.pdf
**02-03 14:38:08.379: D/AndroidRuntime(556): Shutting down VM
02-03 14:38:08.389: W/dalvikvm(556): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
02-03 14:38:08.449: E/AndroidRuntime(556): FATAL EXCEPTION: main
02-03 14:38:08.449: E/AndroidRuntime(556): java.lang.NullPointerException
02-03 14:38:08.449: E/AndroidRuntime(556):  at com.android.nxgen.ResultListBaseAdapter$1.onClick(ResultListBaseAdapter.java:105)
02-03 14:38:08.449: E/AndroidRuntime(556):  at android.view.View.performClick(View.java:2408)
02-03 14:38:08.449: E/AndroidRuntime(556):  at android.view.View$PerformClick.run(View.java:8816)
02-03 14:38:08.449: E/AndroidRuntime(556):  at android.os.Handler.handleCallback(Handler.java:587)
02-03 14:38:08.449: E/AndroidRuntime(556):  at android.os.Handler.dispatchMessage(Handler.java:92)
02-03 14:38:08.449: E/AndroidRuntime(556):  at android.os.Looper.loop(Looper.java:123)
02-03 14:38:08.449: E/AndroidRuntime(556):  at android.app.ActivityThread.main(ActivityThread.java:4627)
02-03 14:38:08.449: E/AndroidRuntime(556):  at java.lang.reflect.Method.invokeNative(Native Method)
02-03 14:38:08.449: E/AndroidRuntime(556):  at java.lang.reflect.Method.invoke(Method.java:521)
02-03 14:38:08.449: E/AndroidRuntime(556):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-03 14:38:08.449: E/AndroidRuntime(556):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-03 14:38:08.449: E/AndroidRuntime(556):  at dalvik.system.NativeStart.main(Native Method)**
4

1 回答 1

1

我猜是你的问题。

activity.startActivity(intent); //Problem is here you never initialize the activity.

试试这个。

holder.url.setOnClickListener(new OnClickListener()
{
    public void onClick(View v )
    {
        mat=ItemResultsarraylist.get(position).geturl1();
        System.out.println("Mat value is"+mat);
        Uri uri = Uri.parse(mat);
        Intent intent = new Intent(Intent.ACTION_VIEW, uri); 
        System.out.println("Uri is "+intent);
        context.startActivity(intent); //Change here activity to context.
    }
}); 

或者

public ResultListBaseAdapter(Context context, ArrayList<ItemResults> resultc) {
        ItemResultsarraylist = resultc;
        l_Inflater = LayoutInflater.from(context);
        this.context=context;
        this.activity = context;
}
于 2013-02-03T09:14:18.700 回答