[英]Android Dialog show HTML in TextView from String
I'm trying to show HTML code in a dialog but the HTML needs to be stored in a string which is first put into a textview. 我正在尝试在对话框中显示HTML代码,但HTML需要存储在首先放入textview的字符串中。
I have found many other questions about something similar but most are setting the text in code rather than in strings.xml. 我发现了许多其他类似问题,但大多数问题是在代码中而不是strings.xml中设置文本。 None I found seem to use a 'dialog to textview to string with html' process.
我发现似乎没有使用“对话框将textview转换为html字符串”的过程。 I've done so far what I think should work but I'm getting an error now (below).
到目前为止,我已经完成了我认为应该可以使用的工作,但是现在(下面)出现错误。 I've tried other ways but cannot get the HTML effects to show.
我尝试了其他方法,但是无法显示HTML效果。
I'm still new to Android so any help to see what I'm doing wrong would be great. 我对Android还是很陌生,所以任何帮助我发现自己在做错的事情都会很棒。 Thanks.
谢谢。
preferencesActivity.java - OnCreate 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;
}
});
}
helpfile .xml 帮助文件.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>
Strings .xml 字符串.xml
<string name="helpFile_text">Test of <b>bold and <u>underline</u> and bullet •</string>
Logcat Logcat
E/AndroidRuntime(1516): java.lang.NullPointerException
E/AndroidRuntime(1516): at com.example.newcalc.preferencesActivity$1.onPreferenceClick(preferencesActivity.java:44)
Change 更改
helpFile_textView = (TextView) findViewById(R.id.helpFile_textView);
helpFile_textView.setText(Html.fromHtml(getString(R.string.helpFile_text)));
dialog.setContentView(R.layout.helpfile);
to 至
dialog.setContentView(R.layout.helpfile);
helpFile_textView = (TextView) dialog.findViewById(R.id.helpFile_textView);
helpFile_textView.setText(Html.fromHtml(getString(R.string.helpFile_text)));
You have to inflate the layout that you want to use in the Dialog
before you can find it using findViewById
您必须先在
Dialog
使用您想使用的布局,然后才能使用findViewById
找到它。
Also since the id
you want to find (ie helpFile_textView
) is in the layout that you inflated in dialog
you have to use dialog.findViewById
instead of findViewById
to get the TextView
另外,由于要查找的
id
(即helpFile_textView
)位于dialog
中的展开布局中,因此必须使用dialog.findViewById
而不是findViewById
来获取TextView
Also 也
Dialog dialog = new dialog(preferencesActivity.this);
should be 应该
Dialog dialog = new Dialog(preferencesActivity.this);
but i think that is a typo. 但我认为这是一个错字。
try this 尝试这个
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)));
for more correction refer @Aproov's answer. 有关更多更正,请参阅@Aproov的答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.