无法访问android中的另一个类

时间:2011-06-03 14:30:54

标签: android class nullpointerexception

我对我的简单代码发生了什么感到困惑。即使我检查了代码流,它也不会进入另一个类文件。下面是我在android中的代码:

公共类MedF1扩展了ListActivity {

DrugsDbAdapter drugsDbAdapter = new DrugsDbAdapter(this);
DrugsDbAdapter.myDbHelper mDbHelper = new DrugsDbAdapter.myDbHelper(this);

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.drug_list);



    try {

        mDbHelper.createDataBase();

        } catch (IOException ioe) {

        throw new Error("Unable to create database");

        }

        try {

        mDbHelper.openDataBase();

        }catch(SQLException sqle){

        throw sqle;

        }

        populateDrugList();

}

public void populateDrugList() {

    Cursor drugListCursor = drugsDbAdapter.getAllEntries();

    startManagingCursor (drugListCursor);

    String[] from = new String[] {DrugsDbAdapter.KEY_DRUG};

    int[] to = new int[] {R.id.text1};

    SimpleCursorAdapter drugs = new SimpleCursorAdapter(this, R.layout.drug_row, drugListCursor, from, to);

    setListAdapter(drugs);
}

数据库适配器类如下,代码只是不超出上面代码中populateDrugList()的druglistCursor!

public class DrugsDbAdapter {

private static final String DATABASE_TABLE = "data";


//The index column name for use in where clauses
public static final String KEY_ID = "_id";

//The name and column index of each column in DB
public static final String KEY_DRUG = "drug";
public static final String KEY_CONTENT = "content";
public static final String KEY_INDICATION = "indication";
public static final String KEY_DOSAGE = "dosage";
public static final String KEY_SPECIALPRECAUTION = "specialprecaution";


//variable to hold the database instance
private static SQLiteDatabase db;
//Context of the application using the database
private final Context context;
//Database open/upgrade helper
private myDbHelper dbHelper;

public DrugsDbAdapter(Context _context) {

    this.context = _context;

}

public void open() throws SQLiteException {
    try {
        db = dbHelper.getWritableDatabase();
    } catch (SQLiteException ex) {
        db = dbHelper.getReadableDatabase();
    }
}

//Creation of database and basic parameters
public static class myDbHelper extends SQLiteOpenHelper {

    private static String DB_PATH = "data/data/com.paad.MedF1/databases/";

    private static String DB_NAME = "data.sqlite";

    private SQLiteDatabase myDataBase;

    private Context myContext;

    public  myDbHelper (Context context) {

        super(context, DB_NAME, null, 1);

        this.myContext = context;

    }

    public void createDataBase() throws IOException{

        boolean dbExist = checkDataBase();

        if(dbExist) {
            // do nothing
        } else {

            this.getReadableDatabase();

            try {

                copyDataBase();

            } catch (IOException e) {

                throw new Error ("Error copying database");

            }

        }
    }

    private boolean checkDataBase(){


        boolean checkDB = false;

        try{
            String myPath = DB_PATH + DB_NAME;
            File dbfile = new File(myPath);

            checkDB = dbfile.exists();

        }catch(SQLiteException e){

            //database does't exist yet.

        }

        return checkDB;

    }


    private void copyDataBase() throws IOException{

        //Open your local db as the input stream
        InputStream myInput = myContext.getAssets().open(DB_NAME);

        // Path to the just created empty db
        String outFileName = DB_PATH + DB_NAME;

        //Open the empty db as the output stream
        OutputStream myOutput = new FileOutputStream(outFileName);

        //transfer bytes from the inputfile to the outputfile
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer))>0){
            myOutput.write(buffer, 0, length);
        }

        //Close the streams
        myOutput.flush();
        myOutput.close();
        myInput.close();

    }

    public void openDataBase() throws SQLException{

        //Open the database
        String myPath = DB_PATH + DB_NAME;
        myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

    }

    @Override
    public synchronized void close() {

            if(myDataBase != null)
                myDataBase.close();

            super.close();

    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

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

    }

}


public void close() {
    db.close();
}

public long insertEntry(myDrug _myDrug) {

    ContentValues newDrugValues = new ContentValues();

    newDrugValues.put(KEY_DRUG, _myDrug.getDrug());
    newDrugValues.put(KEY_CONTENT, _myDrug.getContent());
    newDrugValues.put(KEY_INDICATION, _myDrug.getIndication());
    newDrugValues.put(KEY_DOSAGE, _myDrug.getDosage());
    newDrugValues.put(KEY_SPECIALPRECAUTION, _myDrug.getSpecialprecaution());

    return db.insert(DATABASE_TABLE, null, newDrugValues);
}

public boolean removeEntry (long _rowIndex) {
    return db.delete(DATABASE_TABLE, KEY_ID + "=" + _rowIndex, null) > 0;
}

public Cursor getAllEntries () { 
    return db.query(DATABASE_TABLE, new String[] {KEY_ID, KEY_DRUG, KEY_CONTENT, KEY_INDICATION, KEY_DOSAGE, KEY_SPECIALPRECAUTION}, null, null, null, null, null);
}


}   

HElp !!!我的logcat输出是:

06-03 22:29:01.636: ERROR/AndroidRuntime(1424): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.paad.MedF1/com.paad.MedF1.MedF1}: java.lang.NullPointerException
06-03 22:29:01.636: ERROR/AndroidRuntime(1424):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
06-03 22:29:01.636: ERROR/AndroidRuntime(1424):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-03 22:29:01.636: ERROR/AndroidRuntime(1424):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-03 22:29:01.636: ERROR/AndroidRuntime(1424):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-03 22:29:01.636: ERROR/AndroidRuntime(1424):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-03 22:29:01.636: ERROR/AndroidRuntime(1424):     at android.os.Looper.loop(Looper.java:123)
06-03 22:29:01.636: ERROR/AndroidRuntime(1424):     at android.app.ActivityThread.main(ActivityThread.java:4627)
06-03 22:29:01.636: ERROR/AndroidRuntime(1424):     at java.lang.reflect.Method.invokeNative(Native Method)
06-03 22:29:01.636: ERROR/AndroidRuntime(1424):     at java.lang.reflect.Method.invoke(Method.java:521)
06-03 22:29:01.636: ERROR/AndroidRuntime(1424):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-03 22:29:01.636: ERROR/AndroidRuntime(1424):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-03 22:29:01.636: ERROR/AndroidRuntime(1424):     at dalvik.system.NativeStart.main(Native Method)
06-03 22:29:01.636: ERROR/AndroidRuntime(1424): Caused by: java.lang.NullPointerException
06-03 22:29:01.636: ERROR/AndroidRuntime(1424):     at com.paad.MedF1.MedF1.populateDrugList(MedF1.java:51)
06-03 22:29:01.636: ERROR/AndroidRuntime(1424):     at com.paad.MedF1.MedF1.onCreate(MedF1.java:45)
06-03 22:29:01.636: ERROR/AndroidRuntime(1424):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-03 22:29:01.636: ERROR/AndroidRuntime(1424):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-03 22:29:01.636: ERROR/AndroidRuntime(1424):     ... 11 more

3 个答案:

答案 0 :(得分:1)

您的drugDbAdapter为空。您只声明了 DrugsDbAdapter drugsDbAdapter; ,但您需要实例化它。

答案 1 :(得分:0)

你有没有初步化药物DDAdapter?我无法在代码中的任何位置看到初始化。

答案 2 :(得分:0)

这看起来就像我每次在Activity的布局XML中出现错误时得到的错误。检查以确保XML格式正确且正确。将其简化为最简单的状态并对其进行测试以确保这不是问题。

另外,正如其他人所述,使用给定的代码看起来并不像是实例化数据库适配器。