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>
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
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.