我試圖在警報對話框中顯示一些文本作爲超鏈接。部分過程需要我使用SpannableString來格式化一些文本。問題是我的應用程序在我的代碼的SpannableString部分遇到運行時錯誤。Spannable String Error
TextView Tv= (TextView) findViewById(R.id.textView3);
SpannableString s = new SpannableString("www.google.com");
Linkify.addLinks(s, Linkify.WEB_URLS);
Tv.setText(s);
Tv.setMovementMethod(LinkMovementMethod.getInstance());
我在DDMS中查找,錯誤說Java.Lang.NullPointerException。有沒有人經歷過這個?我應該能夠傳遞SpannableString方法一個硬編碼的字符串。我不知道爲什麼它會像這樣崩潰。
這是我的java文件中的OnCreate函數:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//System.out.println("+++++++++++++++++++++++");
TextView Tv= (TextView) findViewById(R.id.textviewH);
Tv.setText("GO!");
//System.out.println("+++++++++++++++++++++++");
SpannableString s = new SpannableString("www.google.com");
Linkify.addLinks(s, Linkify.WEB_URLS);
Tv.setText(s);
Tv.setMovementMethod(LinkMovementMethod.getInstance());
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("About T2M");
dialog.setIcon(R.drawable.ic_launcher);
dialog.setView(getLayoutInflater().inflate(R.layout.activity_about_t2m, null));
dialog.setCancelable(false);
dialog.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id)
{
dialog.cancel();
}
});
//System.out.println("+++++++++++++++++++++++");
dialog.create();
dialog.show();
}
這是我的XML文件中的文本視圖:
你可以給我們一行NullPointerException發生PLZ嗎? – Aleks
我做了一些使用println的測試,發現錯誤發生在SpannableString施工線 –
你確定它不能來自** findViewById **嗎? 因爲我想不出爲什麼** SpannableString **會出現NullPointerException ... – Aleks