0

我正在尝试创建一个 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)
4

2 回答 2

1

如果您在模拟器上尝试映射地址,Linkify 将无法工作,但其余的工作正常。在您的代码中,您试图通过提供链接来打开浏览器,这没有问题,这是完美的。当我看到您的错误日志时,我可以告诉您,您的 linkify 没有问题。您正试图从上下文之外调用您的活动。在这种情况下,在启动活动时使用 FLAG_ACTIVITY_NEW_TASK 标志启动它。希望这能解决您的问题

于 2012-01-22T05:22:56.810 回答
0

我无法让这种方法起作用,所以我根据网站采用了以下方法:http: //mgmblog.com/2009/01/06/four-different-ways-of-opening-a-web-page-在-android/

<TableRow>
        <TextView
                android:id="@+id/link1_lbl"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text=""
                android:textSize="16sp"
                android:paddingRight="5dip"/>
            <!--
                Using autoLink='web' to show the text as a web link
             -->
            <TextView
                android:id="@+id/link1_view"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:autoLink="web"
                android:text=""/>
        </TableRow>

我以编程方式设置了 link1_lbl 和 link1_view 属性

于 2012-01-23T17:07:24.520 回答