Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java getReadableDatabase()';关于空对象引用_Java_Android_Listview_Android Sqlite - Fatal编程技术网

Java getReadableDatabase()';关于空对象引用

Java getReadableDatabase()';关于空对象引用,java,android,listview,android-sqlite,Java,Android,Listview,Android Sqlite,我是新来的,我正在做一些调试。我有一些难以表达的困难 我试图从另一个活动中创建的SQLite数据库中读取数据 我目前的问题似乎是: SQLiteDatabase db = myOpenHelper.getReadableDatabase(); 它应该指向我的数据库助手类 public class SecondActivity extends ListActivity { CustomOpenHelper myOpenHelper; @Override protect

我是新来的,我正在做一些调试。我有一些难以表达的困难

我试图从另一个活动中创建的SQLite数据库中读取数据

我目前的问题似乎是:

SQLiteDatabase db = myOpenHelper.getReadableDatabase();
它应该指向我的数据库助手类

public class SecondActivity extends ListActivity {

    CustomOpenHelper myOpenHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        displayDataInTable();

    }

        void displayDataInTable() {
        List<String> values = queryTable();

        if (values != null) {
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(SecondActivity.this, android.R.layout.simple_list_item_1, values);
            setListAdapter(adapter);


        }

    }

    List<String> queryTable() {
        List<String> player = new ArrayList<String>();

        SQLiteDatabase db = myOpenHelper.getReadableDatabase();
        Cursor cursor = db.query(true, CustomOpenHelper.TABLE_NAME, new String[]{"_id, name, score"}, null, null, null, null, null, null, null);

        while (cursor.moveToNext()) {
            int id = cursor.getInt(cursor.getColumnIndex("_id"));
            String name = cursor.getString(cursor.getColumnIndex("name"));
            int score = cursor.getInt(cursor.getColumnIndex("score"));
            player.add(id + " --> the player " + name + " has got a score of " + score + "s");
        }

        return player;
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_second, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

你觉得怎么样?

试试这个,你可以创建这样合适的对象

CustomOpenHelper myOpenHelper = new CustomOpenHelper(this);
然后写下面的陈述

SQLiteDatabase db = myOpenHelper.getReadableDatabase();

不必打电话:

SQLiteDatabase db = myOpenHelper.getReadableDatabase();
如果出现错误,请改用此选项,这样就不需要sqlitedatabase对象:

Cursor cursor = getActivity().getContentResolver().query(Uri, 
projection,selection,selectionArgs,sortOrder);

你能发送完整的代码吗?你实例化myOpenHelper了吗?是的,先生,已编辑,请告知你是否想查看其他类?你应该实例化
CustomOpenHelper
并将其分配给
myOpenHelper
。嗨,mixel,我想我已经完成了?在代码的顶部?:CustomOpenHelper myOpenHelper;谢谢你们,谢谢甘帕特·卡莉娅。这花了我一段时间,但已经奏效了。
Cursor cursor = getActivity().getContentResolver().query(Uri, 
projection,selection,selectionArgs,sortOrder);