【问题标题】:Android Dialog show HTML in TextView from StringAndroid 对话框从字符串的 TextView 中显示 HTML
【发布时间】:2014-06-04 10:34:55
【问题描述】:

我正在尝试在对话框中显示 HTML 代码,但 HTML 需要存储在一个字符串中,该字符串首先放入 textview。

我发现了许多其他类似的问题,但大多数是在代码中而不是在 strings.xml 中设置文本。没有我发现似乎使用“对话框到 textview 到字符串与 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 &#8226;</string>

Logcat

E/AndroidRuntime(1516): java.lang.NullPointerException
E/AndroidRuntime(1516):     at com.example.newcalc.preferencesActivity$1.onPreferenceClick(preferencesActivity.java:44)

【问题讨论】:

    标签: android html dialog textview


    【解决方案1】:

    改变

    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);
    

    但我认为这是一个错字。

    【讨论】:

    • 感谢@Apoorv 修复了错误,一旦你解释它就非常有意义。谢谢! (是的,这只是一个错字)。一个提法是 HTML 仍然没有格式化 - 但我在其他答案中看到的是字符串需要放在 'bold 和 underline u>]]'
    • 抱歉,我对此一无所知,但我通常使用 &lt;html&gt;&lt;body&gt; 标签,然后使用它们之间的所有内容。这对我有用,你可以试一试。
    • @Apoorv 嘿 +1 来自我>
    【解决方案2】:

    试试这个

    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 的回答。

    【讨论】:

    • 谢谢,这也是正确的,但标记了 Aproov 的答案,因为它有更多解释。
    • @SteddyRyan 没关系。当然,Apoorv 的答案是最好的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-06
    • 1970-01-01
    • 2018-01-23
    • 2016-08-25
    • 1970-01-01
    • 1970-01-01
    • 2015-07-05
    相关资源
    最近更新 更多