simplecursoradapter textview 给出 nullpointerException

发布于 2024-12-14 10:55:21 字数 3497 浏览 2 评论 0原文

我有两个 xml 文件,一个是列表视图,另一个是列表视图和一些 texview 的布局,我想更改第二个 xml 文件中文本视图的颜色。这就是我到目前为止所做的。

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);
    }
}

我想更改存储在 R.id.title 中的文本视图日期的颜色。 这里日志猫为值 cl 提供 null...并给我 nullpointer 异常...:( 谁能告诉我......? 先感谢您...:)

I have two xml files one is having the listview and another is having the layout of the listview vth some texviews, I want to change the color of the textview in the second xml file. This is what i have done so far.

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);
    }
}

I wanted to change the color of textview date which is storing in R.id.title.
here the log cat is giving null for the value cl.....and gives me nullpointer exception...:(
can any one tell me please.......?
thank you in advance...:)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

勿忘心安 2024-12-21 10:55:21

opp_main.xml 文件中没有名为 total 的视图。尝试添加它,看看是否可以解决问题。

You do not have a View called total in the opp_main.xml file. Try adding it and see if that solves the issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文