我有一个带有TextView
的应用程序小部件。我将其android:autoLink属性设置为"web",这样就可以点击链接在浏览器中启动它们。链接可以点击,但当我点击它们时,在打开浏览器"活动"时会引发异常
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)
这是小部件中的TextView
<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" />
在你的方法getView(...)
上,像这样膨胀你的视图:
LayoutInflater.from(YOUR_ACTIVITY_INSTANCE).inflate(R.layout.layout_with_text_view_autolink, parent, false);
好吧,这个问题来自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);
如果widget.getContext()不是活动的实例,则intent必须添加FLAG_activity_NEW_TASK标志;我们不能在这里添加代码,所以我们将使widget.getContext()返回activty实例,
LayoutInflaturer.from(activityInstance).fulle(R.layout.xxx,null);或新视图(activityInstance)。。
如果您来自一个从BaseAdapter 扩展的类,这里是解决方案
来自类主活动:
mLeDeviceListAdapter = new LeDeviceListAdapter(MainActivity.this);
从类LeDeviceListAdapter扩展BaseAdapter
LeDeviceListAdapter(Activity activity) {
beacons = new ArrayList<>();
mInflator = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}