我试图在对话框中显示HTML代码,但是HTML需要存储在一个字符串中,该字符串首先放在文本视图中。
我发现了许多类似的问题,但大多数是用代码而不是strings.xml设置文本。我发现似乎没有使用‘对话框文本视图字符串与html’的进程。到目前为止,我已经完成了我认为应该有效的工作,但现在我遇到了一个错误(下面)。我尝试过其他方法,但无法显示HTML效果。
我对Android还不熟悉,所以任何帮助我看看自己做错了什么都很好。谢谢。
preferencesActivity.java - OnCreate
Preference myPref = (Preference) findPreference("pref_showHelp");
myPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
Dialog dialog = new dialog(preferencesActivity.this);
helpFile_textView = (TextView) findViewById(R.id.helpFile_textView);
helpFile_textView.setText(Html.fromHtml(getString(R.string.helpFile_text))); // Line 44
dialog.setContentView(R.layout.helpfile);
dialog.setTitle("Help");
dialog.show();
return false;
}
});
}帮助文件.xml
<TextView
android:id="@+id/helpFile_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="@string/helpFile_text" />
</LinearLayout>字符串.xml
<string name="helpFile_text">Test of <b>bold and <u>underline</u> and bullet •</string>Logcat
E/AndroidRuntime(1516): java.lang.NullPointerException
E/AndroidRuntime(1516): at com.example.newcalc.preferencesActivity$1.onPreferenceClick(preferencesActivity.java:44)发布于 2014-06-04 10:37:44
变化
helpFile_textView = (TextView) findViewById(R.id.helpFile_textView);
helpFile_textView.setText(Html.fromHtml(getString(R.string.helpFile_text)));
dialog.setContentView(R.layout.helpfile); 至
dialog.setContentView(R.layout.helpfile);
helpFile_textView = (TextView) dialog.findViewById(R.id.helpFile_textView);
helpFile_textView.setText(Html.fromHtml(getString(R.string.helpFile_text)));在使用Dialog查找布局之前,必须对要在findViewById中使用的布局进行充气。
另外,由于您想要找到的id (即helpFile_textView)位于您在dialog中膨胀的布局中,您必须使用dialog.findViewById而不是findViewById来获取TextView
也是
Dialog dialog = new dialog(preferencesActivity.this);应该是
Dialog dialog = new Dialog(preferencesActivity.this);但我觉得这是个错误。
发布于 2014-06-04 10:37:35
尝尝这个
dialog.setContentView(R.layout.helpfile);
helpFile_textView = (TextView) dialog.findViewById(R.id.helpFile_textView);
helpFile_textView.setText(Html.fromHtml(preferencesActivity.this.getResources().getString(R.string.helpFile_text)));要获得更多的更正,请参阅@Aproov的答案。
https://stackoverflow.com/questions/24035162
复制相似问题