开发者

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 savedInstanceSt开发者_如何学JAVAate) {

        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?


  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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜