1

i'm a begginner of android.Shortly what i'm doing is using a simpleAdapater to show a web URL list for practice purpose. however, when i click on the URL,it cause a forceclose error. 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:

intent.addFlag(FLAG_ACTIVITY_NEW_TASK);

unfortunately,the click event of the web URL is responsed by system automatically. so how can i resolve this problem in my case? And can anyone explain why this problem occured when using simpleAdapter but not 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:

<?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>
4

2 回答 2

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链接外观,手动处理点击事件

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

但是,我仍然想知道为什么会出现这个问题。有没有人有任何想法?

于 2012-05-15T13:43:44.993 回答
0

尝试:

simpleAdapter = new SimpleAdapter(this, ...

getApplicationContext() -> this
于 2014-04-04T07:14:07.463 回答