【问题标题】:NPE while using simple list item 2 in android在android中使用简单列表项2时的NPE
【发布时间】:2014-03-18 13:28:02
【问题描述】:

我正在尝试使用简单的光标适配器将数据库中创建的两列的值放入简单列表项 2 的两个文本视图中,但我得到了 NPE。 请帮忙。
问候

我的代码是:

class NotesList extends ListActivity{
    private DataSource datasource;
     ArrayAdapter<Message> adapter;
     Cursor cursors = null;
     private SQLiteDatabase database;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list);
        datasource = new DataSource(this);
        datasource.open();


       // List<Message> values = datasource.getAllMessages();
    //    adapter = new ArrayAdapter<Message>(this, android.R.layout.simple_list_item_1, values);
    //     setListAdapter(adapter);


        String queryz = "SELECT " + MySQLiteHelper.COLUMN_ID + "," + MySQLiteHelper.COLUMN_MESSAGE+ " FROM " + MySQLiteHelper.TABLE_NAME ;
        cursors = database.rawQuery(queryz, null);

           ListAdapter adapter = new SimpleCursorAdapter(
                     this,
                     android.R.layout.simple_list_item_2,
                     cursors,     // Pass in the cursor to bind to.
                     new String[] {MySQLiteHelper.COLUMN_ID, MySQLiteHelper.COLUMN_MESSAGE}, // Array of cursor columns to bind to.
                     new int[] {android.R.id.text1, android.R.id.text2});  // Parallel array of which template objects to bind to those columns.

             // Bind to our new adapter.
             setListAdapter(adapter);

    }
    @Override
    public void onResume() {
        datasource.open();

        super.onResume();
      }


      @Override
    public void onPause() {
        datasource.close();

        super.onPause();

      }

Datasource 和 Mysqlitehelper 是我的类的名称。MySqliteHelper 类创建了 id 和 notes 两列。 Logcat 是:

03-19 01:15:32.557: E/AndroidRuntime(10579): FATAL EXCEPTION: main
03-19 01:15:32.557: E/AndroidRuntime(10579): java.lang.RuntimeException: Unable to start activity ComponentInfo{soft.b.notes/soft.b.notes.NotesList}: java.lang.NullPointerException
03-19 01:15:32.557: E/AndroidRuntime(10579):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
03-19 01:15:32.557: E/AndroidRuntime(10579):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
03-19 01:15:32.557: E/AndroidRuntime(10579):    at android.app.ActivityThread.access$600(ActivityThread.java:122)
03-19 01:15:32.557: E/AndroidRuntime(10579):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
03-19 01:15:32.557: E/AndroidRuntime(10579):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-19 01:15:32.557: E/AndroidRuntime(10579):    at android.os.Looper.loop(Looper.java:137)
03-19 01:15:32.557: E/AndroidRuntime(10579):    at android.app.ActivityThread.main(ActivityThread.java:4340)

【问题讨论】:

    标签: java android sqlite android-listview simplecursoradapter


    【解决方案1】:

    您的database 未初始化。

    此外,检查完整的堆栈跟踪也很有用。在RuntimeException 中嵌套了一个“由”异常。在 IDE 中双击堆栈跟踪行将准确显示出现问题的代码行。

    【讨论】:

      【解决方案2】:

      您正在一个空数据库上运行 rawQuery。我在任何地方都看不到您实际上为您的数据库变量获得了 SQLiteDatabase。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-10-17
        • 1970-01-01
        • 2020-02-20
        • 1970-01-01
        • 1970-01-01
        • 2011-10-29
        • 1970-01-01
        相关资源
        最近更新 更多