Spannable字符串错误 [英] Spannable String Error

查看:172
本文介绍了Spannable字符串错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示一个警告对话框中一些文字作为超链接。这一进程的一部分要求我使用SpannableString格式化一些文字。问题是,我的应用程序遇到我的code的SpannableString部分运行时错误。

I am trying to display some text in an alert dialog box as a hyperlink. Part of the process requires me to use SpannableString to format some text. The problem is that my application experiences a runtime error on the SpannableString part of my code.

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方法很难codeD字符串。我不知道为什么它崩溃是这样的。

I looked in the DDMS and the Error says Java.Lang.NullPointerException. Has anyone experienced this? I Should be able to pass the SpannableString method a hardcoded string. I do not know why it is crashing like this.

这是我的java文件中的OnCreate功能:

This is the OnCreate function in my java file:

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文件中的文本视图:

This is the text view in my XML file:

 <TextView
    android:id="@+id/textviewH"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/textView5"
    android:layout_alignLeft="@+id/textView2"
    android:autoLink="all"
    android:clickable="true"
    android:text="Humium"
    android:textSize="15sp" />

推荐答案

好了,所以我觉得这个问题从通胀过程中来。
好像你正在尝试访问的TextView你抬高布局之前。因此, findViewById 不会找到任何东西,因为它会在活动布局搜索。

Ok, so I think the issue come from the inflation process. It seems like you're trying to access the TextView before you inflate the Layout. So findViewById won't find anything as it will search in the Activity Layout.

这里的技巧是虚增您自定义布局第一(,这里是你可以做的为例:

The trick here would be inflate your custom Layout first (, here's an exemple of what you could do :

    AlertDialog.Builder dialog = new AlertDialog.Builder(this);
    //Inflating the custom Layout
    View view = LayoutInflater.from(this).inflate(R.layout.activity_about_t2m, null);

    //Searching for the TextView in it
    TextView tv = (TextView)view.findViewById(R.id.textviewH);

    //Then making the links
    SpannableString s = new SpannableString("www.google.fr");
    Linkify.addLinks(s, Linkify.WEB_URLS);

    //Adding the text to the View and make the links clickable 
    tv.setText(s);
    tv.setMovementMethod(LinkMovementMethod.getInstance());

    //Finally applying the custom view we inflated (R.layout.activity_about_t2m) on the AlertDialog and ....
    dialog.setView(view);
    dialog.setTitle("About T2M");
    dialog.setIcon(R.drawable.ic_launcher);
    dialog.setCancelable(false);
    dialog.setPositiveButton(android.R.string.ok,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id)
                {
                    dialog.cancel();
                }
            } 
    );

编辑1:
我犯了一个错误这个例子......在你的情况在 getApplicationContext()这个(我做在code中的修复)

Edit 1: I made a mistake with this example… In your case the getApplicationContext() should be this (I made the fix in the code)

此处了解详情:
Android的 - Linkify问题

编辑2:
好了,现在应该是可以理解的吧?

Edit 2: Ok, so now that should be understandable right ?

这篇关于Spannable字符串错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆