可跨越字符串错误

时间:2013-08-03 13:42:55

标签: android spannablestring spannable

我正在尝试在警告对话框中显示一些文本作为超链接。部分过程要求我使用SpannableString格式化一些文本。问题是我的应用程序在我的代码的SpannableString部分遇到运行时错误。

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

 <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" />

My Layout

3 个答案:

答案 0 :(得分:1)

此代码对我来说很好。我希望这对你也有帮助。

TextView richTextView = (TextView) findViewById(R.id.text_aboutus);
    // this is the text we'll be operating on
    SpannableString text = new SpannableString("Hi Click on Google.com and yahoo.com or Pmstudy.com");
    text.setSpan(
            new URLSpan("http://www.google.com/"),
            13, 22, 0);
    text.setSpan(new URLSpan("http://www.yahoo.com/"),
            28, 36, 0);
    text.setSpan(new URLSpan("http://www.pmstudy.com"), 41, 51, 0);

    richTextView.setMovementMethod(LinkMovementMethod.getInstance());
    richTextView.setText(text, BufferType.SPANNABLE);

享受......

谢谢, 穆克什

答案 1 :(得分:1)

好的,所以我认为这个问题来自通货膨胀过程。 您似乎在尝试在扩充布局之前访问TextView。所以 findViewById 将找不到任何内容,因为它会在活动布局中进行搜索。

这里的诀窍是首先夸大你的自定义布局(这里是你可以做的事情的例子:

    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()应该这个(我在代码中修改了)

更多信息: Android - Linkify Problem

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

答案 2 :(得分:1)

**You can Use Same code in your custom Dialog as well. This is currently working for me.

you need to inflate your custom-dialog-layout in your code .**

custom_dialog.xml

//this xml is for custom Dialog.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/text_aboutus"
        android:text="Hi Click on Google.com and yahoo.com or Pmstudy.com"/>

</LinearLayout>



package PackageName;

// import Files

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    Button show_dialog=(Button) findViewById(R.id.imgView);
    show_dialog.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Dialog d=new Dialog(MainActivity.this);
            View v=getLayoutInflater().inflate(R.layout.custom_dialog, null);
            TextView richTextView = (TextView) v.findViewById(R.id.text_aboutus);
            SpannableString text = new SpannableString("Hi Click on Google.com and yahoo.com or Pmstudy.com");
            text.setSpan(
                    new URLSpan("http://www.google.com/"),
                    13, 22, 0);
            text.setSpan(new URLSpan("http://www.yahoo.com/"),
                    28, 36, 0);
            text.setSpan(new URLSpan("http://www.pmstudy.com"), 41, 51, 0);

            richTextView.setMovementMethod(LinkMovementMethod.getInstance());
            richTextView.setText(text, BufferType.SPANNABLE);

            d.setContentView(v);
            d.show();

        }
    });

    }

}