1

I have an app widget with a TextView in it. I set its android:autoLink property to "web" so that the links can be clicked to launch them in browser. The links become clickable alright, but when I click on them an exception is thrown while opening the browser Activity.

10-31 01:27:56.155: E/Nova.AppWidget(9427): com.capturekenya/.CaptureWidgetProvider v1.0 (1): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
10-31 01:27:56.155: E/Nova.AppWidget(9427):     at android.app.ContextImpl.startActivity(ContextImpl.java:1026)
10-31 01:27:56.155: E/Nova.AppWidget(9427):     at android.app.ContextImpl.startActivity(ContextImpl.java:1013)
10-31 01:27:56.155: E/Nova.AppWidget(9427):     at android.content.ContextWrapper.startActivity(ContextWrapper.java:284)
10-31 01:27:56.155: E/Nova.AppWidget(9427):     at android.text.style.URLSpan.onClick(URLSpan.java:64)
10-31 01:27:56.155: E/Nova.AppWidget(9427):     at android.text.method.LinkMovementMethod.onTouchEvent(LinkMovementMethod.java:212)

This is the TextView in the widget

<TextView
android:id="@+id/notification_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/calendar_textView"
android:layout_marginTop="8dp"
android:autoLink="web"
android:ellipsize="end"
android:gravity="center"
android:text="@string/notification"
android:textColor="@color/white"
android:textSize="12dp" />
Paul Muriithi
  • 41
  • 1
  • 5
  • Please add your code, and see if it relates to this https://coderwall.com/p/cg_efa?i=1&p=1&q=author%3Aldurazo&t%5B%5D=ldurazo – Ringo Oct 30 '14 at 23:38
  • If you create a widget and have a TextView with with android:autoLink="web" then Set the text to something with a URL,the links will be colored but if the user clicks on the URL,you get the exception android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? – Paul Muriithi Oct 31 '14 at 09:09
  • I meant more code, where is the textview? over an adapter? outside an activity? – Ringo Oct 31 '14 at 13:56

3 Answers3

2

On your method getView(...) inflate your view like this:

 LayoutInflater.from(YOUR_ACTIVITY_INSTANCE).inflate(R.layout.layout_with_text_view_autolink, parent, false);
Alex
  • 265
  • 3
  • 6
1

well, this question result from URLSpan onClick

    Uri uri = Uri.parse(getURL());
    Context context = widget.getContext();
    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    intent.putExtra(Browser.EXTRA_APPLICATION_ID, context.getPackageName());
    context.startActivity(intent);

if widget.getContext() is not instance of activity , intent must add FLAG_ACTIVITY_NEW_TASK flag ; we cannot add code here,so we shall make widget.getContext() return activty instance,

LayoutInflater.from( activityInstance ).inflate(R.layout.xxx,null); or new View( activityInstance)..

包奇锋
  • 11
  • 2
0

Here the solution in case you are from a class that extends from BaseAdapter

from class MainActivity:

mLeDeviceListAdapter = new LeDeviceListAdapter(MainActivity.this);

from class LeDeviceListAdapter extends BaseAdapter

LeDeviceListAdapter(Activity activity) {
        beacons = new ArrayList<>();
        mInflator = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}