[英]How to handle exception while clicking link using textview in android
我正在一個Android項目中,我必須按照以下示例添加鏈接。.但是,當我單擊鏈接時,它既不會突出顯示,也無法訪問該鏈接。 點擊該鏈接會導致一個異常,該異常顯示
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
當我單擊鏈接時,我想在瀏覽器中打開鏈接。如何有效地做到這一點? 我期待很快的答復!謝謝
注意:我在實際項目中使用了動態Textview
public class StackOverflowActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView link = new TextView(getApplicationContext);
String linkText = "Visit the <a href='http://stackoverflow.com'>StackOverflow</a> web page.";
link.setText(Html.fromHtml(linkText));
link.setMovementMethod(LinkMovementMethod.getInstance());
}
我相信您必須先膨脹包含TextView的布局,然后才能使用它,如下所示:
getActivity().getLayoutInflater()
在您的TextView
xml中使用android:autoLink="web"
。 如果不起作用,請嘗試android:linksClickable="true"
根據您的錯誤,如果使用動態TextView
,請確保this
對象引用為
TextView yourView = new TextView(this);
this
是指您的“ Activity
上下文,我認為您可能會犯一些小錯誤。
您不應使用getApplicationContext
來創建帶有Activity
上下文的視圖。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.