我是android的初学者。我正在做的是使用simpleAdapater来显示用于练习目的的Web URL列表。 但是,当我点击URL时,会导致强制关闭错误。 logcat显示
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
我已经发现了一些关于这种异常的类似问题。一种可能的解决方案是在调用startActivity方法之前添加标志。例如:
intent.addFlag(FLAG_ACTIVITY_NEW_TASK);
遗憾的是,系统会自动响应网址的点击事件。 那我怎么能在我的情况下解决这个问题呢? 任何人都可以解释为什么在使用simpleAdapter而不是ArrrayAdapter时出现这个问题?
这是主要代码:
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;
}
}
这是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>
答案 0 :(得分:1)
最终,我自己解决了这个问题。想到的是,不要让系统处理链接,而是手动完成整个过程。 这是解决方案:
首先,删除布局文件中的autolink属性
<?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>
然后实现一个继承自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;
}
}
最后一步非常简单。只需在活动中使用这个新的适配器。
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);
然而,我仍然想知道为什么会出现这个问题。有没有人有任何想法?
答案 1 :(得分:0)
尝试:
simpleAdapter = new SimpleAdapter(this, ...
getApplicationContext() -> this