Jacek Kwiecień
Jacek Kwiecień

Reputation: 12659

TextView setText problem

I'm trying to set text in TextView after geting it from database

<TextView 
    android:textAppearance="?android:attr/textAppearanceMedium" 
    android:text="2510.33" 
    android:layout_width="wrap_content" 
    android:id="@+id/so_you_spent"  
    android:layout_height="wrap_content"
    android:gravity="right">
</TextView> 

This is the activity:

public class SummaryOverall extends Activity{

    private CfmDbAdapter db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.summary_overall);

        db = new CfmDbAdapter(this);
        db.open();

        fillData();
    }

    private void fillData() {       
        TextView tv = (TextView)findViewById(R.id.so_you_spent);

        Cursor itemCursor = db.fetchAllItemsInOverallSummary();
        if (itemCursor.moveToFirst()) {
            String value = itemCursor.getString(0);         
            tv.setText(value);  //nullpointer exception 
        }                   
    }


}

It throws NullPointerException at me. Why?

Upvotes: 2

Views: 1755

Answers (2)

Samir Mangroliya
Samir Mangroliya

Reputation: 40426

Try this code----:::

And make sure There were textview id idOk in your xml file....summary_overall

Remove android:text="2510.33" and set as a android:text=""

private void fillData() {       
    TextView tv = (TextView)findViewById(R.id.so_you_spent);

    Cursor itemCursor = db.fetchAllItemsInOverallSummary();
    if (itemCursor.moveToFirst()) {
        String value = itemCursor.getString(0); //value taken "321" - string   
        if(value!=null&&value!=""&&tv!=null) {
            tv.setText(value);     
        } else { 
            tv.setText("NO value Found...");     
        }
    }                   
}

Upvotes: 1

Marco
Marco

Reputation: 57593

  1. Check if itemCursor returns what you tell us (321) or not
  2. Check if tv is not null

If 1 is true, check db connection or query.
If 2 is true, probably you're using an ID that's not in summary_overall layout

EDITED:
Try to remove android:text="2510.33" line from xml

Upvotes: 2

Related Questions