0

In my activity I do

TextView bTitle = (TextView)findViewById(R.id.TextView);

bTitle.setText(Html.fromHtml(title));

where title is a string

but i get a null pointer exception at second line ie. bTitle.setText(Html.fromHtml(title));

can anyone help???

Activity

public class NewsDetails extends ListActivity{

public static final String LOG_TAG = "Infra";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listplaceholder);
    super.onStart();
    Intent myIntent = getIntent(); 
    String id = myIntent.getStringExtra("content_id");
    String title = myIntent.getStringExtra("title");
    //Spanned newTitle = Html.fromHtml(title);
    //tv.setText(Html.fromHtml(title));
    TextView bTitle = (TextView)findViewById(R.id.TextView);
    Log.d(LOG_TAG, "What is the value: " + bTitle);
    bTitle.setText(Html.fromHtml(title));

    ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();


    String xml = XMLfunctions.getBodyXML(id);
    String result = xml.replaceAll("<p>", "<p><div align=\"justify\">");
    String nXml = result.replaceAll("</p>", "</div></p>");
    TextView body = (TextView)findViewById(R.id.TextView1);
    body.setText(Html.fromHtml(nXml));


    //Spanned body = Html.fromHtml(nXml);
    /*HashMap<String, String> map = new HashMap<String, String>();  
    map.put("title", tv.toString());
    map.put("news", tv1.toString());
    mylist.add(map);*/

    /*ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.list_item, new String[] { "title", "news" }, new int[] { R.id.item_title, R.id.item_subtitle });

    setListAdapter(adapter);

    final ListView lv = getListView();
    lv.setTextFilterEnabled(true);*/
}

Layout - listplaceholder.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">    

    <ListView
    android:id="@id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:drawSelectorOnTop="false" />

    <TextView
        android:id="@id/android:empty"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="No data"
        android:focusable="false"
        android:clickable="true"/>

</LinearLayout>

text_view.xml

<LinearLayout android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView 
        android:id="@+id/TextView"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:textSize="16px"
        android:textStyle="bold">
    </TextView>
    <TextView 
        android:id="@+id/TextView1"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:textSize="16px"
        android:textStyle="bold">
    </TextView>

</LinearLayout>
4
  • plz add your all code and xml... Commented May 2, 2011 at 11:09
  • @CapDroid: i've added the code please have a look Commented May 2, 2011 at 11:15
  • Any luck so far? Please communicate your concerns, if any, so we could help more. Thank you. Commented May 6, 2011 at 3:02
  • @rekaszeru: Sorry for the delay in reply, thank you all for your help. I replaced textview with webview and now my application is running fine. Commented May 6, 2011 at 3:48

5 Answers 5

2

You should load the correct xml for your layout

setContentView(R.layout.text_view)

TextView bTitle = (TextView)findViewById(R.id.TextView);

bTitle.setText(Html.fromHtml(title));
Sign up to request clarification or add additional context in comments.

Comments

1

i found two issue in your code

1)first thing is your TextView is not in listplaceholder.xml .. so you it will be null pointer...

2)and you need add this code before setContentView();

 Intent myIntent = getIntent(); 
String id = myIntent.getStringExtra("content_id");
String title = myIntent.getStringExtra("title");

and let me know what u want in list and where you call setAdapter for list , where is your adapter...

Comments

1

Make sure, that you actually have the

<TextView android:id="@+id/TextView [...] 

view in your activity's layout xml.

Update
In your listplaceholder.xml there is no such TextView!
If you use the text_view.xml layout as your item renderer, then the code that tries to access the TextView should be placed inside your ListAdapter implementation's getView method!

2 Comments

i'll share my code can you have a look i'm not able to come out with a solution
i have posted a new question at [stackoverflow.com/questions/6196256/…. will be very greatful if you can help. Thanks
0

1) Make sure R is imported from your project (and not from android.R namespace) in your import(s) section on the top

2) After the

TextView bTitle = (TextView)findViewById(R.id.TextView);

perform a log:

Log.d("debug", "bTitle: " + (bTitle != null));

and check out from terminal (if you're using Linux, if Windows, just run the logcat)

adb logcat | grep bTitle

This should tell you if you properly got the TextView from your layout (if it is present in the layout, like @rekaszeru mentioned).

3) Make sure the title variable is not null as well

Comments

0

Thank you all for your help. I have replaced textview with webview as i needed a lot of html formatting in my application and now my text appears in proper format and also my appliction is running fine.

Thanks

1 Comment

Its great that you managed it to work, but this isn't answer to your question. You had a NPE caused by trying to access a view which wasn't present in the layout you referenced.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.