0

我有兩個xml文件之一是有一個listview和另一個有listview的佈局vth一些texviews,我想改變第二個xml文件中的textview的顏色。這是我迄今爲止所做的。simplecursoradapter textview給nullpointerexception

main1.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:paddingTop="4px"> 

    <ListView droid:id="@android:id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_below="@id/header" 
     android:layout_above="@+id/bottom_control_bar1" 
     android:layout_marginBottom="45dip" 
     android:drawSelectorOnTop="false"/> 

</RelativeLayout> 

opp_main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:padding="8px"> 

    <TextView 
     android:id="@+id/firstName" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 

    <TextView 
     android:id="@+id/lastName" 
     android:layout_marginLeft="6px" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/firstName" /> 

    <TextView 
     android:id="@+id/title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/firstName" 
     android:layout_toRightOf="@id/total" 
     android:layout_marginLeft="8px" 
     android:textColor="@color/closedDate" /> 
</RelativeLayout> 

opp.java:

public class OppMain extends ListActivity { 
    /** Called when the activity is first created. */ 
    final String tag = "output"; 
    static String Header = "Opportunities"; 
    static String DBName, Date = null; 
    protected Cursor cursor; 
    protected ListAdapter adapter; 
    SQLiteDatabase db; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main1); 

     TextView hdr = (TextView)findViewById(R.id.header); 
     hdr.setText(Header); 

     //database    
     db = (new openDataBaseAdapter(this)).getWritableDatabase(); 
     cursor = db.query("userdata", new String []{"_id", "Name","Name2", "Date","title"},null, null, null, null,null); 
     adapter = new SimpleCursorAdapter(
      this, 
      R.layout.opp_main, 
      cursor, 
      new String[] {"Name", "Name2", "Date","title"}, 
      new int[] {R.id.firstName,R.id.total, R.id.title, R.id.accountname}); 

     cursor.moveToFirst(); 

     while(cursor.isAfterLast() == false) { 
      CloseDate = cursor.getString(cursor.getColumnIndex("Date")); 
      if(CloseDate.equals("30/09/2011 00:00")) { 
       TextView cl = (TextView)findViewById(R.id.title); 
       Log.d(tag, "cl: " +cl); 
       Log.d(tag, "Date11111: " +Date); 
       cl.setTextColor(Color.CYAN); 
      } 

      cursor.moveToNext(); 
     } 

     setListAdapter(adapter); 
    } 
} 

我想這是存儲改變的TextView日期的顏色R.id.title。 在這裏日誌貓是賦予空值的CL .....並給我空指針異常... :( 可以任何一個告訴我請.......? 謝謝你提前... :)

回答

0

opp_main.xml文件中,您沒有稱爲total的視圖。嘗試添加它,看看是否解決了這個問題。

+0

thanx的帖子,我已經改變了代碼,但它仍然是一樣的.... :( – rj87877

+0

你能否更新你的問題中的代碼還包括你從logcat獲得的堆棧跟蹤? – Craigy

相關問題