Tanuj Nayak
Tanuj Nayak

Reputation: 607

NullPointerException thrown while creating SpannableString

I am trying to create a textview whose text is partially underlined using a spannableString but I'm getting a nullpointerexception when I try to do that. Here's my code:

tv = (TextView) findViewById(R.id.tvHelp);
    SpannableString content = new SpannableString("Underlined text");
    content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
    tv.setText(content+"non-underlined text"); //exception is thrown here
    super.onCreate(savedInstanceState);

Thanks in advance for your help.

Upvotes: 0

Views: 481

Answers (1)

V.J.
V.J.

Reputation: 9580

tv = (TextView) findViewById(R.id.tvHelp);
SpannableString content = new SpannableString("Underlined text");
content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
tv.setText(content+"non-underlined text"); //exception is thrown here

Write the above code after

super.onCreate(savedInstanceState);
and
setContentView("Your Layout");

Upvotes: 1

Related Questions