[英]click URL causes Force Close error in ListView when using SimpleAdapter
i'm a begginner of android.Shortly what i'm doing is using a simpleAdapater to show a web URL list for practice purpose. 我是android的初学者。我正在做的一件事就是使用simpleAdapater来显示Web URL列表以供练习。 however, when i click on the URL,it cause a forceclose error. 但是,当我单击URL时,会导致强制关闭错误。 the logcat shows that 日志显示
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
i already found out some similar problems about this kind of exception.one possible solution is that add the flag before calling startActivity method.for example: 我已经发现了关于这种异常的一些类似问题。一种可能的解决方案是在调用startActivity方法之前添加标志。例如:
intent.addFlag(FLAG_ACTIVITY_NEW_TASK);
unfortunately,the click event of the web URL is responsed by system automatically. 不幸的是,Web URL的点击事件是系统自动响应的。 so how can i resolve this problem in my case? 所以我该如何解决这个问题? And can anyone explain why this problem occured when using simpleAdapter but not ArrrayAdapter? 谁能解释为什么在使用simpleAdapter而不是ArrrayAdapter时会出现此问题?
here is the main code: 这是主要代码:
public class RSSContentActivity extends ListActivity
{
private SimpleAdapter simpleAdapter;
private ListView lv;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
lv = getListView();
simpleAdapter = new SimpleAdapter(getApplicationContext(), getData(),
R.layout.rss_content_item, new String[]
{"item_link" }, new int[]
{R.id.rss_item_content});
lv.setAdapter(simpleAdapter);
}
private List<Map<String, Object>> getData()
{
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("item_link",
"http://www.bom.gov.au/vic/warnings/coastalwind.shtml");
list.add(map);
map = new HashMap<String, Object>();
map.put("item_link",
"http://www.bom.gov.au/cgi-bin/wrap_fwo.pl?IDV36050.html");
list.add(map);
map = new HashMap<String, Object>();
map.put("item_link", "http://www.bom.gov.au/products/IDY21000.shtml");
list.add(map);
return list;
}
}
here is the rss_item_content.xml file: 这是rss_item_content.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rss_item_content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:autoLink="web"
android:padding="10dp"
android:textAppearance="?android:attr/textAppearanceMedium" >
</TextView>
Eventually, i resolved this problem by myself.The thought is that instead of asking the system to deal with the link, do the whole process manually. 最终,我自己解决了这个问题,以为不是手动要求系统处理链接,而是手动完成整个过程。 Here is the solution: 解决方法如下:
First,delete the autolink attribute in the layout file 首先,在布局文件中删除自动链接属性
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rss_item_content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textAppearance="?android:attr/textAppearanceMedium" >
</TextView>
then implement a adapater class which inherited from the SimpleAdapter,and rewrite the getView method to change the link text to a web link apperance and deal with the click event manually 然后实现从SimpleAdapter继承的adapater类,并重写getView方法以将链接文本更改为Web链接外观并手动处理click事件
public class RSSContentAdapter extends SimpleAdapter
{
private Context context;
public RSSContentAdapter(Context context, List<? extends Map<String, ?>> data, int resource,String[] from, int[] to)
{
super(context, data, resource, from, to);
this.context=context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view = super.getView(position, convertView, parent);
if (view != null)
{
TextView link = (TextView) view.findViewById(R.id.rss_item_content);
if (link != null)
{
String linkText = link.getText().toString();
SpannableString sp = new SpannableString(linkText);
// set the link text to a link appearance and rewrite the onClick method
sp.setSpan(new URLSpan(linkText){
@Override
public void onClick(View widget)
{
//linkText
String linkText=((TextView)widget).getText().toString();
Log.d(getClass().toString(), "onClick");
//send URL to web browser
Uri uri = Uri.parse(linkText);
Intent it = new Intent(Intent.ACTION_VIEW,uri);
it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//add the flag to avoid force close error
context.startActivity(it);
}
}, 0, linkText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
link.setText(sp);
MovementMethod m = link.getMovementMethod();
if ((m == null) || !(m instanceof LinkMovementMethod))
{
if (link.getLinksClickable())
{
link.setMovementMethod(LinkMovementMethod.getInstance());
}
}
}
}
return view;
}
} }
the last step is fairly simple.Just use this new adapater in the activity. 最后一步非常简单。只需在活动中使用这个新的适配器即可。
lv = getListView();
listAdapter = new
RSSContentAdapter(getApplicationContext(), getData(),
R.layout.rss_content_item, new String[] { "item_link" }, new int[] {
R.id.rss_item_content });
lv.setAdapter(listAdapter);
However, i still wonder why this problem occured. 但是,我仍然想知道为什么会出现此问题。 Does anyone have any ideas? 有人有什么想法吗?
try: 尝试:
simpleAdapter = new SimpleAdapter(this, ...
getApplicationContext() -> this
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.