我正在尝试创建一个 Android Activity 页面,SecondaryActivity,它提供了一个指向打开具有指定链接的 Web 浏览器的网页的链接。我正在从我的主活动页面启动活动页面,如下所示:
Intent intent = new Intent(this, SecondaryActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
MainActivity.this.startActivity(intent);
我的 SecondaryActivity 中有以下代码,但它给出了以下错误:
代码
protected void onPostExecute(Map<String,Float> sampleData) {
TableLayout table = (TableLayout) findViewById(R.id.mainTable);
if(sampleData == null) {
// If no information is found for the user, add the message to the view letting them know where
// to go to set up their appliances...
TextView tvDynamicKey = new TextView(getApplicationContext());
tvDynamicKey.setText("No appliances have been found, please help us by \nlogging onto our website. Click here.");
Pattern pattern = Pattern.compile("here");
String scheme = "http://www.google.com/";
Linkify.addLinks(tvDynamicKey, pattern, scheme);
tvDynamicKey.setLayoutParams(new TableRow.LayoutParams(1));
TableRow tr = new TableRow(getApplicationContext());
tr.setId(101);
tr.addView(tvDynamicKey);
table.addView(tr);
TextView tvDynamicKey2 = new TextView(getApplicationContext());
tvDynamicKey2.setText("Contact me at test@test.com \nfor details!");
pattern = Pattern.compile("test@test.com");
scheme = "test@test.com";
Linkify.addLinks(tvDynamicKey2, pattern, scheme);
// TODO When I click the link here, I get an error:
tr = new TableRow(getApplicationContext());
tr.setId(102);
tr.addView(tvDynamicKey2);
table.addView(tr);
}
}
错误
01-21 23:31:52.715: ERROR/AndroidRuntime(838): FATAL EXCEPTION: main
01-21 23:31:52.715: ERROR/AndroidRuntime(838): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
01-21 23:31:52.715: ERROR/AndroidRuntime(838): at android.app.ContextImpl.startActivity(ContextImpl.java:617)
01-21 23:31:52.715: ERROR/AndroidRuntime(838): at android.content.ContextWrapper.startActivity(ContextWrapper.java:258)
01-21 23:31:52.715: ERROR/AndroidRuntime(838): at android.text.style.URLSpan.onClick(URLSpan.java:62)
01-21 23:31:52.715: ERROR/AndroidRuntime(838): at android.text.method.LinkMovementMethod.onTouchEvent(LinkMovementMethod.java:216)
01-21 23:31:52.715: ERROR/AndroidRuntime(838): at android.widget.TextView.onTouchEvent(TextView.java:6577)
01-21 23:31:52.715: ERROR/AndroidRuntime(838): at android.view.View.dispatchTouchEvent(View.java:3766)
01-21 23:31:52.715: ERROR/AndroidRuntime(838): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
01-21 23:31:52.715: ERROR/AndroidRuntime(838): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
01-21 23:31:52.715: ERROR/AndroidRuntime(838): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
01-21 23:31:52.715: ERROR/AndroidRuntime(838): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
01-21 23:31:52.715: ERROR/AndroidRuntime(838): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
01-21 23:31:52.715: ERROR/AndroidRuntime(838): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
01-21 23:31:52.715: ERROR/AndroidRuntime(838): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1671)
01-21 23:31:52.715: ERROR/AndroidRuntime(838): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
01-21 23:31:52.715: ERROR/AndroidRuntime(838): at android.app.Activity.dispatchTouchEvent(Activity.java:2086)
01-21 23:31:52.715: ERROR/AndroidRuntime(838): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1655)
01-21 23:31:52.715: ERROR/AndroidRuntime(838): at android.view.ViewRoot.handleMessage(ViewRoot.java:1785)
01-21 23:31:52.715: ERROR/AndroidRuntime(838): at android.os.Handler.dispatchMessage(Handler.java:99)
01-21 23:31:52.715: ERROR/AndroidRuntime(838): at android.os.Looper.loop(Looper.java:123)
01-21 23:31:52.715: ERROR/AndroidRuntime(838): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-21 23:31:52.715: ERROR/AndroidRuntime(838): at java.lang.reflect.Method.invokeNative(Native Method)
01-21 23:31:52.715: ERROR/AndroidRuntime(838): at java.lang.reflect.Method.invoke(Method.java:521)
01-21 23:31:52.715: ERROR/AndroidRuntime(838): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-21 23:31:52.715: ERROR/AndroidRuntime(838): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-21 23:31:52.715: ERROR/AndroidRuntime(838): at dalvik.system.NativeStart.main(Native Method)