将List传递给方法会引发错误

时间:2014-12-09 04:36:36

标签: android sqlite arraylist

我将表行的List传递给适配器内的方法。如果我创建List然后将其传递给方法,它就会崩溃。我找到了一种方法使其工作,但不知道如何描述它或为什么它的工作原理。请帮我理解。

public class CatalogGridView extends Activity {
    MySQLiteHelper db = new MySQLiteHelper(this);

    //List<Poster> posters = db.getAllPosters(); <-- Here I have commented out the problem. If I pass "posters" to the setData method it crashes.

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

        GridView gridview = (GridView) findViewById(R.id.gridview);
        ImageAdapter mGridviewAdapter = new ImageAdapter(this);

        //---------------------------------------------------------
        mGridviewAdapter.setData(db.getAllPosters());
        //If I do it this way, it works.
        //---------------------------------------------------------

        gridview.setAdapter(new ImageAdapter(this));

    }

这是我的适配器内部的方法。

public class ImageAdapter extends BaseAdapter{
//I define the posterList as:
List<Poster> posterList = new ArrayList<Poster>(0);

    public void setData(List<Poster> posters){
        for (Poster p : posters){
            Log.d("posters", p.getPosterFilename());
        }
        posterList.addAll(posters);
    }
}

这是我的db方法

public List<Poster> getAllPosters() {
        List<Poster> posterList = new ArrayList<Poster>();
        // Select All Query
        String selectQuery = "SELECT * FROM " + "posters ORDER BY presenter_lname, presenter_fname";

//Line 99 is getWritableDatabase====================================================    
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Poster poster = new Poster(Integer.parseInt(cursor.getString(0)),Integer.parseInt(cursor.getString(1)),Integer.parseInt(cursor.getString(2)),cursor.getString(3),cursor.getString(4),cursor.getString(5),cursor.getString(6),cursor.getString(7),cursor.getString(8));

                // Adding contact to list
                posterList.add(poster);
            } while (cursor.moveToNext());
        }
        db.close(); // Closing database connection
        // return contact list
        return posterList;
    }

LogCat很难破译,但我可以用SQL来判断正在发生的事情

12-08 21:34:08.320: E/AndroidRuntime(22045): Caused by: java.lang.NullPointerException
12-08 21:34:08.320: E/AndroidRuntime(22045):    at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:229)
12-08 21:34:08.320: E/AndroidRuntime(22045):    at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
12-08 21:34:08.320: E/AndroidRuntime(22045):    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
12-08 21:34:08.320: E/AndroidRuntime(22045):    at com.myapp.e_posterviewer.MySQLiteHelper.getAllPosters(MySQLiteHelper.java:99)
12-08 21:34:08.320: E/AndroidRuntime(22045):    at com.myapp.e_posterviewer.CatalogGridView.<init>(CatalogGridView.java:26)

4 个答案:

答案 0 :(得分:2)

你正在创建一个MySQLiteHelper的对象并传递上下文,因为这是null并导致麻烦将其移动到oncreate并且它会没事。

答案 1 :(得分:1)

MySQLiteHelper db = new MySQLiteHelper(this);
List<Poster> posters = db.getAllPosters();

将此行移至onCreate()方法内。

Activity只有在调用onCreate()之后才创建数据库对象,因此此处contextnull

答案 2 :(得分:1)

直到你这样做:

super.onCreate(savedInstanceState);

this的值为null。如此有效您没有正确初始化对象db。全局声明变量(如果你真的想要全局变量)并在onCreate内初始化它,如下所示:

public class CatalogGridView extends Activity {

    MySQLiteHelper db;    
    List<Poster> posters;

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

        db = new MySQLiteHelper(this);
        posters = db.getAllPosters();

        GridView gridview = (GridView) findViewById(R.id.gridview);
        ImageAdapter mGridviewAdapter = new ImageAdapter(this);

        mGridviewAdapter.setData(posters);

        gridview.setAdapter(new ImageAdapter(this));

    }
    // Rest of your code
}

答案 3 :(得分:0)

更新Oncreate()方法

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_catalog_grid_view);
    MySQLiteHelper db = new MySQLiteHelper(this);
    GridView gridview = (GridView) findViewById(R.id.gridview);
    ImageAdapter mGridviewAdapter = new ImageAdapter(this);

    //---------------------------------------------------------
    mGridviewAdapter.setData(db.getAllPosters());
    //If I do it this way, it works.
    //---------------------------------------------------------

    gridview.setAdapter(new ImageAdapter(this));

}