开发者

Cannot access another class in android

开发者 https://www.devze.com 2023-03-10 09:59 出处:网络
I\'m baffled by what is happening to my simple code. It just doesn\'t go into another class file even though I checked that code flows. Below is my code in android:

I'm baffled by what is happening to my simple code. It just doesn't go into another class file even though I checked that code flows. Below is my code in android:

public class MedF1 extends 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);

    set开发者_StackOverflowListAdapter(drugs);
}

The database adapter class is below , the code just doesn't go beyond druglistCursor of the populateDrugList() in the above code!

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 !!! my logcat output is:

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


Your drugsDbAdapter is null. You have only declared DrugsDbAdapter drugsDbAdapter;, but you need to instantiate it.


Have you initialised drugsDbAdapter? I can't see the initialisation anywhere in your code.


This looks like the error I get every time I have an error in my Activity's layout XML. Check to make sure the XML is well formatted and correct. Simplify it down to its simplest state and test it to make sure this isn't the problem.

Also, as the others stated, with the code given it doesn't appear as though you instantiated the db adapter.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号