我正在尝试在listview中显示网址,当用户点击网址时,它应该在浏览器中打开,我的代码是
package com.app.adapter;
import com.app.myTracker.UI.R;
import com.app.utils.Logger;
import android.content.Context;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.text.method.MovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class BrowserLogListAdapter extends BaseAdapter{
String []VISIT,DATE,URL;
int []ID;
Context c;
private LayoutInflater mInflater;
static class ViewHolder {
TextView url;
TextView visit;
TextView date;
ImageView imgIcon;
}
public BrowserLogListAdapter(Context context,int[]id, String[]visit, String[]date,String []url){
c=context;
mInflater = LayoutInflater.from(context);
ID = id;
VISIT = visit;
DATE = date;
URL = url;
}
public int getCount() {
// TODO Auto-generated method stub
return ID.length;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.browser_log_list_item, null);
holder = new ViewHolder();
// holder.visit = (TextView) convertView.findViewById(R.id.txt_visit);
holder.date = (TextView)convertView.findViewById(R.id.txt_time);
holder.url = (TextView)convertView.findViewById(R.id.txtURL);
convertView.setTag(holder);
}
else
holder = (ViewHolder) convertView.getTag();
Logger.debug(VISIT[position]);
//holder.visit.setText(c.getString(R.string.txt_total_visit)+": "+ VISIT[position]);
String text = "<a href='"+URL[position]+"'>"+URL[position]+"</a>";
Logger.debug("text:"+text);
holder.date.setText(DATE[position]);
holder.url.setText(Html.fromHtml(text));
holder.url.setMovementMethod(LinkMovementMethod.getInstance());
return convertView;
}
}
并且在活动中我称这个适配器为
lvlView.setAdapter(new BrowserLogListAdapter(getApplicationContext(), id, visit, time, url));
因此当我尝试点击列表视图中的url链接时,它会显示错误,如
05-10 13:59:22.630: ERROR/AndroidRuntime(5709): FATAL EXCEPTION: main
05-10 13:59:22.630: ERROR/AndroidRuntime(5709): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
05-10 13:59:22.630: ERROR/AndroidRuntime(5709): at android.app.ContextImpl.startActivity(ContextImpl.java:617)
05-10 13:59:22.630: ERROR/AndroidRuntime(5709): at android.content.ContextWrapper.startActivity(ContextWrapper.java:258)
05-10 13:59:22.630: ERROR/AndroidRuntime(5709): at android.text.style.URLSpan.onClick(URLSpan.java:62)
05-10 13:59:22.630: ERROR/AndroidRuntime(5709): at android.text.method.LinkMovementMethod.onTouchEvent(LinkMovementMethod.java:216)
05-10 13:59:22.630: ERROR/AndroidRuntime(5709): at android.widget.TextView.onTouchEvent(TextView.java:6577)
05-10 13:59:22.630: ERROR/AndroidRuntime(5709): at android.view.View.dispatchTouchEvent(View.java:3766)
05-10 13:59:22.630: ERROR/AndroidRuntime(5709): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
05-10 13:59:22.630: ERROR/AndroidRuntime(5709): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
05-10 13:59:22.630: ERROR/AndroidRuntime(5709): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
05-10 13:59:22.630: ERROR/AndroidRuntime(5709): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
05-10 13:59:22.630: ERROR/AndroidRuntime(5709): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
05-10 13:59:22.630: ERROR/AndroidRuntime(5709): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
05-10 13:59:22.630: ERROR/AndroidRuntime(5709): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1671)
05-10 13:59:22.630: ERROR/AndroidRuntime(5709): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
05-10 13:59:22.630: ERROR/AndroidRuntime(5709): at android.app.Activity.dispatchTouchEvent(Activity.java:2086)
05-10 13:59:22.630: ERROR/AndroidRuntime(5709): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1655)
05-10 13:59:22.630: ERROR/AndroidRuntime(5709): at android.view.ViewRoot.handleMessage(ViewRoot.java:1785)
05-10 13:59:22.630: ERROR/AndroidRuntime(5709): at android.os.Handler.dispatchMessage(Handler.java:99)
05-10 13:59:22.630: ERROR/AndroidRuntime(5709): at android.os.Looper.loop(Looper.java:123)
05-10 13:59:22.630: ERROR/AndroidRuntime(5709): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-10 13:59:22.630: ERROR/AndroidRuntime(5709): at java.lang.reflect.Method.invokeNative(Native Method)
05-10 13:59:22.630: ERROR/AndroidRuntime(5709): at java.lang.reflect.Method.invoke(Method.java:521)
05-10 13:59:22.630: ERROR/AndroidRuntime(5709): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-10 13:59:22.630: ERROR/AndroidRuntime(5709): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-10 13:59:22.630: ERROR/AndroidRuntime(5709): at dalvik.system.NativeStart.main(Native Method)
如果我在简单文本视图中使用相同的代码进行网址重定向,而不是自定义列表适配器,它可以正常工作,但在列表视图中显示此错误
答案 0 :(得分:0)
试试这个
holder.url.setText(URL[position]);
holder.url.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent i = new Intent(Intent.ACTION_VIEW,Uri.parse(URL[position]));
startActivity(i);
}
});