从sqlite数据库填充android列表视图时出错

时间:2014-04-21 13:29:52

标签: android listview nullpointerexception runtime-error android-sqlite

我正在创建使用sqlite数据库的android应用程序,该数据库将填充onCreate()方法的列表视图。 sqlite数据库已预先填充。

我知道错误是NullPointerException,但我不知道为什么会出现此错误以及如何修复它。

这是第125行:

myList.setAdapter(myCursorAdapter);

的logcat

04-21 12:44:21.571: E/AndroidRuntime(1354): Caused by: java.lang.NullPointerException
04-21 12:44:21.571: E/AndroidRuntime(1354):     at com.devleb.expandablelistdemo3.MatchScheduleList.pupulateListViewFromDB(MatchScheduleList.java:125)
04-21 12:44:21.571: E/AndroidRuntime(1354):     at com.devleb.expandablelistdemo3.MatchScheduleList.onCreate(MatchScheduleList.java:65)

ExternalDbOpenHelper.java

package com.devleb.expandablelistdemo3;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class ExternalDbOpenHelper extends SQLiteOpenHelper {

    public static String DB_PATH;
    private static final String TABLE_NAME = "match_list";

    public static String DB_NAME;
    public SQLiteDatabase database;
    public final Context context;
    public SQLiteDatabase getDb() {
        return database;
    }

    public ExternalDbOpenHelper(Context context, String databaseName) {
        super(context, databaseName, null, 1);
        this.context = context;

        String packageName = context.getPackageName();
        DB_PATH = String.format("//data//data//%s//databases//", packageName);
        DB_NAME = databaseName;
        openDataBase();
    }

    public void createDataBase() {
        boolean dbExist = checkDataBase();
        if (!dbExist) {
            this.getReadableDatabase();
            try {
                copyDataBase();
            } catch (IOException e) {
                Log.e(this.getClass().toString(), "Copying error");
                throw new Error("Error copying database!");
            }
        } else {
            Log.i(this.getClass().toString(), "Database already exists");
        }
    }

    private boolean checkDataBase() {
        SQLiteDatabase checkDb = null;
        try {
            String path = DB_PATH + DB_NAME;
            checkDb = SQLiteDatabase.openDatabase(path, null,
                    SQLiteDatabase.OPEN_READONLY);
        } catch (SQLException e) {
            Log.e(this.getClass().toString(), "Error while checking db");
        }
        // ������� �� ����� ������
        // ��������, ��� ������
        // �����������
        if (checkDb != null) {
            checkDb.close();
        }
        return checkDb != null;
    }

    // ����� ����������� ����
    private void copyDataBase() throws IOException {
        // ��������� ����� ���
        // ������ �� ��� ���������
        // ���� ��
        // �������� � assets
        InputStream externalDbStream = context.getAssets().open(DB_NAME);

        // ���� � ��� ���������
        // ������ ���� � ��������
        String outFileName = DB_PATH + DB_NAME;

        // ������ �������� ����� ���
        // ������ � ��� �� ��������
        OutputStream localDbStream = new FileOutputStream(outFileName);

        // ���������� �����������
        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = externalDbStream.read(buffer)) > 0) {
            localDbStream.write(buffer, 0, bytesRead);
        }

        localDbStream.close();
        externalDbStream.close();

    }

    public SQLiteDatabase openDataBase() throws SQLException {
        String path = DB_PATH + DB_NAME;
        if (database == null) {
            createDataBase();
            database = SQLiteDatabase.openDatabase(path, null,
                    SQLiteDatabase.OPEN_READWRITE);
        }
        return database;
    }

    @Override
    public synchronized void close() {
        if (database != null) {
            database.close();
        }
        super.close();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }
}

MatchScheduleList.java

package com.devleb.expandablelistdemo3;

import java.util.ArrayList;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class MatchScheduleList extends Activity {

    private static final String DB_NAME = "world_cup.db";
    // *****Tables name**************************//
    private static final String TABLE_NAME = "match_list";

    // *****Tbale name******************************//
    public static final String PLACE_ID = "_id";
    public static final String STAD_NAME = "stad_name";
    public static final String TEAM1 = "team1";
    public static final String TEAM2 = "team2";
    public static final String STAGE = "stage";
    public static final String MATCH_DATE = "match_date";
    public static final String[] ALL_KEYS = new String[] { PLACE_ID, STAD_NAME,
            TEAM1, TEAM2, STAGE, MATCH_DATE };
    // *****Tbale name******************************//

    private SQLiteDatabase database;
    private ListView listView;
    private ArrayList<String> matchList;

    ExternalDbOpenHelper dbOpenHelper;

    // ****IMAGES FROM DRAWABLE************************//
    int[] imgID = { R.drawable.brazil_flag, R.drawable.croatian_flag,
            R.drawable.mexico_flag, R.drawable.cameroon_flag, R.drawable.spain,
            R.drawable.netherlands_flag, R.drawable.czech_republic_flag,
            R.drawable.australia, R.drawable.colombia_flag, R.drawable.gress,
            R.drawable.cote_divoire_flag, R.drawable.japan,
            R.drawable.uruguay_flag, R.drawable.costa_rica_flag,
            R.drawable.england_flag, R.drawable.italy_flag,
            R.drawable.switzerland, R.drawable.ecuador_flag,
            R.drawable.france_flag, R.drawable.honduras_flag,
            R.drawable.argentina_flag, R.drawable.bousna, R.drawable.iran_flag,
            R.drawable.nigeria_flag, R.drawable.germany_flag,
            R.drawable.portugal, R.drawable.ghana_flag,
            R.drawable.united_states_flag, R.drawable.belgium_flag,
            R.drawable.algeria_flag, R.drawable.russia_flag,
            R.drawable.korea_flag };

    int nextImageIndex = 0;

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

        int imageID = imgID[nextImageIndex];
        nextImageIndex = (nextImageIndex + 1) % imgID.length;
        dbOpenHelper = new ExternalDbOpenHelper(this, DB_NAME);
        database = dbOpenHelper.openDataBase();

        pupulateListViewFromDB();
        // fillFreinds();
        // setUpList();
    }


    public Cursor getAllRows() {
        String where = null;
        Cursor c = database.query(true, TABLE_NAME, ALL_KEYS, where, null,
                null, null, null, null);
        if (c != null) {
            c.moveToFirst();
        }
        return c;
    }

    private void pupulateListViewFromDB() {
        // TODO Auto-generated method stub

        Cursor cursor = getAllRows();

        // allow activity to manage lifeTime of the Cursor
        startManagingCursor(cursor);

        // setup maping from cursor to view fields:
        String[] fromFieldNames = new String[] { MATCH_DATE };
        int[] toViewIDs = new int[] { R.id.txtDate };
        SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(this,
                R.layout.row_list_match_schedule, cursor, fromFieldNames,
                toViewIDs);

        ListView myList = (ListView) findViewById(R.id.list);
        myList.setAdapter(myCursorAdapter);

    }

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

}

0 个答案:

没有答案