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());
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.
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();
}
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" />