链接未在Web浏览器中打开

时间:2013-02-03 08:37:39

标签: android listview listadapter

我已经制作了ListView适配器并在每行中实现了具有相同按钮ID的唯一URL,现在每当我尝试点击按钮时,它会显示强制关闭

P.S:我是新手

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)**

1 个答案:

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