自定义SimpleCursorAdapter,数据库查询和NullPointerException异常 [英] Custom SimpleCursorAdapter, database query and NullPointerException

查看:132
本文介绍了自定义SimpleCursorAdapter,数据库查询和NullPointerException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使ListView的填充从数据库,和香料的每一行与花哨的删除按钮。所以我做了名单的活动和定制SimpleCursorAdapter。

这是主要的ListView活动:

 公共类EditEntries扩展ListActivity {
    光标光标;
    DBAdapter DB =新DBAdapter(本);
    私有静态的String [] FROM = {_ID,SCORE_COLUMN,
            日期(时间,'本地时间'),};
    私有静态诠释[] TO = {R.id.rowid,R.id.title,R.id.time,};

    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.entries_list);
        尝试 {
            光标= getEvents();
            showEvents(光标);
        } 最后 {
            db.close();
        }
    }

    私人无效showEvents(光标指针){
        //设置数据绑定
        SMSimpleCursorAdapter适配器=新SMSimpleCursorAdapter(这一点,
                R.layout.entries_list_item,光标,发件人,收件人);
        setListAdapter(适配器);

    }

    私人光标getEvents(){
        db.open();
        光标= db.getAllScores();
        startManagingCursor(光标);
        返回游标;
    }

    无效delRow(){//这是一种通过_id删除行
        db.open();
        db.deleteScore(3); //这里将TAG与_id的适配器,
        db.close(); //现在我只是用硬codeD _id
    }

}
 

这是自SimpleCursorAdapter:

 公共类SMSimpleCursorAdapter扩展SimpleCursorAdapter {

    光标C;
    上下文语境;
    活动活性;

    公共SMSimpleCursorAdapter(上下文的背景下,INT布局,光标C,
            的String []从,INT []键){
        超(背景下,布局,C,从,到);

        this.c = C;
        this.context =背景;
        this.activity =(活动)范围内;

    }
    EditEntries dbDel =新EditEntries(); //从previous code样品

    @覆盖
    公共查看getView(INT位置,查看convertView,ViewGroup中父){
        如果(convertView == NULL)
            convertView = View.inflate(背景下,R.layout.entries_list_item,NULL);
        查看排= convertView;

        c.moveToPosition(位置);

        TextView的得分=(TextView中)convertView.findViewById(R.id.title);
        TextView的时间=(TextView中)convertView.findViewById(R.id.time);
        TextView的ID =(TextView中)convertView.findViewById(R.id.rowid);

        id.setText(c.getString(0));
        time.setText(c.getString(2));
        score.setText(c.getString(1));

        串daTag = c.getString(1);

        的ImageButton delButton =(的ImageButton)convertView.findViewById(R.id.delButton);
        delButton.setFocusable(真正的);
        delButton.setClickable(真正的);
        //delButton.setTag(daTag);
        delButton.setOnClickListener(新OnClickListener(){//点击监听器来回回删除按钮
             @覆盖
             公共无效的onClick(视图查看){
                 dbDel.delRow(); //这是previos code样品删除行的方法
             }
             });

        返回(行);
    }

}
 

和所有这些东西给我的NullPointerException当我点击删除按钮。 我是新来的机器人,并假设我只是错过了一些东西明显。

LogCat中:

  

ERROR / AndroidRuntime(14027):致命异常:主要

     

ERROR / AndroidRuntime(14027):显示java.lang.NullPointerException

     

ERROR / AndroidRuntime(14027):在android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)

     

ERROR / AndroidRuntime(14027):在android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98)

     

ERROR / AndroidRuntime(14027):在namespace.DBAdapter.open(DBAdapter.java:66)

     

ERROR / AndroidRuntime(14027):在namespace.EditEntries.delRow(EditEntries.java:50)

     

ERROR / AndroidRuntime(14027):在namespace.SMSimpleCursorAdapter $ 1.onClick(SMSimpleCursorAdapter.java:64)

     

ERROR / AndroidRuntime(14027):在android.view.View.performClick(View.java:2414)

     

ERROR / AndroidRuntime(14027):在android.view.View $ PerformClick.run(View.java:8838)

     

ERROR / AndroidRuntime(14027):在android.os.Handler.handleCallback(Handler.java:587)

     

ERROR / AndroidRuntime(14027):在android.os.Handler.dispatchMessage(Handler.java:92)

     

ERROR / AndroidRuntime(14027):在android.os.Looper.loop(Looper.java:123)

     

ERROR / AndroidRuntime(14027):在android.app.ActivityThread.main(ActivityThread.java:4680)

     

ERROR / AndroidRuntime(14027):在java.lang.reflect.Method.invokeNative(本机方法)

     

ERROR / AndroidRuntime(14027):在java.lang.reflect.Method.invoke(Method.java:521)

     

ERROR / AndroidRuntime(14027):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:858)

     

ERROR / AndroidRuntime(14027):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

     

ERROR / AndroidRuntime(14027):在dalvik.system.NativeStart.main(本机方法)

解决方案

现在的问题是在你的SMSimpleCursorAdapter code:

  EditEntries dbDel =新EditEntries(); //从previous code样品
 

您创建一个新的对象,但它不会是一个管理活动(如在onCreate方法将不会被调用)。在NPE可能来自你的DBAdapter当您尝试创建它的第二次(从适配器)。

快速修复:

  EditEntries dbDel; //从previous code样品
公共SMSimpleCursorAdapter(上下文的背景下,INT布局,光标C,
        的String []从,INT []键){
    超(背景下,布局,C,从,到);

    this.c = C;
    this.context =背景;
    this.activity =(活动)范围内;
    this.dbDel =(EditEntries)范围内;
}
 


如果你不希望只是一个快速解决以下将是一个更好的解决方案:

  1. 将在delRow,getEvents方法到您的DBAdapter类
  2. 修改您的SMSimpleCursorAdapter的构造给了DBAdapter类(在你的ListActivity创建)。

I'm trying to make ListView populated from database, and spice each row with fancy delete button. So I made list Activity and custom SimpleCursorAdapter.

This is main ListView activity:

    public class EditEntries extends ListActivity {
    Cursor cursor;
    DBAdapter db = new DBAdapter(this);
    private static String[] FROM = { _ID, SCORE_COLUMN,
            "date(time, 'localtime')", };
    private static int[] TO = { R.id.rowid, R.id.title, R.id.time, };

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.entries_list);
        try {
            cursor = getEvents();
            showEvents(cursor);
        } finally {
            db.close();
        }
    }

    private void showEvents(Cursor cursor) {
        // Set up data binding
        SMSimpleCursorAdapter adapter = new SMSimpleCursorAdapter(this,
                R.layout.entries_list_item, cursor, FROM, TO);
        setListAdapter(adapter);

    }

    private Cursor getEvents() {
        db.open();
        cursor = db.getAllScores();
        startManagingCursor(cursor);
        return cursor;
    }

    void delRow() {        // this is method for deleting row by _id
        db.open();
        db.deleteScore(3); // here will be TAG with _id from adapter,
        db.close();        // for now I just use hardcoded _id
    }

}

And this is custom SimpleCursorAdapter:

    public class SMSimpleCursorAdapter extends SimpleCursorAdapter{

    Cursor c;
    Context context;
    Activity activity;

    public SMSimpleCursorAdapter(Context context, int layout, Cursor c,
            String[] from, int[] to) {
        super(context, layout, c, from, to);

        this.c = c;
        this.context=context;
        this.activity=(Activity) context;

    }
    EditEntries dbDel = new EditEntries(); //from previous code sample

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null)
            convertView = View.inflate(context, R.layout.entries_list_item, null);
        View row = convertView;

        c.moveToPosition(position);

        TextView score = (TextView) convertView.findViewById(R.id.title);
        TextView time = (TextView) convertView.findViewById(R.id.time);
        TextView id = (TextView) convertView.findViewById(R.id.rowid);

        id.setText(c.getString(0));
        time.setText(c.getString(2));
        score.setText(c.getString(1));

        String daTag = c.getString(1);

        ImageButton delButton = (ImageButton) convertView.findViewById(R.id.delButton);
        delButton.setFocusable(true);
        delButton.setClickable(true);
        //delButton.setTag(daTag);
        delButton.setOnClickListener(new OnClickListener() { //Click listener fro delete button
             @Override
             public void onClick(View view) {
                 dbDel.delRow(); //this is "delete row" method from previos code                    sample
             }
             });

        return(row);
    }

}

And all this stuff gives me NullPointerException when I click delete button. I'm new to android, and suppose that I just missed something obvious.

LogCat:

ERROR/AndroidRuntime(14027): FATAL EXCEPTION: main

ERROR/AndroidRuntime(14027): java.lang.NullPointerException

ERROR/AndroidRuntime(14027): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)

ERROR/AndroidRuntime(14027): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98)

ERROR/AndroidRuntime(14027): at namespace.DBAdapter.open(DBAdapter.java:66)

ERROR/AndroidRuntime(14027): at namespace.EditEntries.delRow(EditEntries.java:50)

ERROR/AndroidRuntime(14027): at namespace.SMSimpleCursorAdapter$1.onClick(SMSimpleCursorAdapter.java:64)

ERROR/AndroidRuntime(14027): at android.view.View.performClick(View.java:2414)

ERROR/AndroidRuntime(14027): at android.view.View$PerformClick.run(View.java:8838)

ERROR/AndroidRuntime(14027): at android.os.Handler.handleCallback(Handler.java:587)

ERROR/AndroidRuntime(14027): at android.os.Handler.dispatchMessage(Handler.java:92)

ERROR/AndroidRuntime(14027): at android.os.Looper.loop(Looper.java:123)

ERROR/AndroidRuntime(14027): at android.app.ActivityThread.main(ActivityThread.java:4680)

ERROR/AndroidRuntime(14027): at java.lang.reflect.Method.invokeNative(Native Method)

ERROR/AndroidRuntime(14027): at java.lang.reflect.Method.invoke(Method.java:521)

ERROR/AndroidRuntime(14027): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)

ERROR/AndroidRuntime(14027): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

ERROR/AndroidRuntime(14027): at dalvik.system.NativeStart.main(Native Method)

解决方案

The problem is in your SMSimpleCursorAdapter code:

EditEntries dbDel = new EditEntries(); //from previous code sample

You create a new object but it won't be a managed Activity (eg. the onCreate method won't be called). The NPE probably comes from your DBAdapter when you try to create it the second time (from your Adapter).

Quick fix:

EditEntries dbDel; //from previous code sample
public SMSimpleCursorAdapter(Context context, int layout, Cursor c,
        String[] from, int[] to) {
    super(context, layout, c, from, to);

    this.c = c;
    this.context = context;
    this.activity = (Activity) context;
    this.dbDel = (EditEntries) context;
}


If you don't want just a quick fix the following would be a much better solution:

  1. Move the delRow, getEvents method into your DBAdapter class
  2. Modify the constructor of your SMSimpleCursorAdapter to give in the DBAdapter class (created in your ListActivity).

这篇关于自定义SimpleCursorAdapter,数据库查询和NullPointerException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆