空指针异常Sql数据库

时间:2014-06-23 17:15:30

标签: java android database sqlite

嘿如果我想这样开始另一项活动我得到了这些例外

06-23 19:07:44.022: E/AndroidRuntime(1271): FATAL EXCEPTION: main
06-23 19:07:44.022: E/AndroidRuntime(1271): Process: food.manager, PID: 1271
06-23 19:07:44.022: E/AndroidRuntime(1271): java.lang.RuntimeException: Unable to start       activity ComponentInfo{food.manager/food.manager.VerlaufEssen}: java.lang.NullPointerException
06-23 19:07:44.022: E/AndroidRuntime(1271):     at        .app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
 06-23 19:07:44.022: E/AndroidRuntime(1271):    at    android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2653)
   06-23 19:07:44.022: E/AndroidRuntime(1271):  at android.app.ActivityThread.access$800(ActivityThread.java:156)
  06-23 19:07:44.022: E/AndroidRuntime(1271):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
 06-23 19:07:44.022: E/AndroidRuntime(1271):    at android.os.Handler.dispatchMessage(Handler.java:102)
 06-23 19:07:44.022: E/AndroidRuntime(1271):    at android.os.Looper.loop(Looper.java:157)
 06-23 19:07:44.022: E/AndroidRuntime(1271):    at android.app.ActivityThread.main(ActivityThread.java:5872)
 06-23 19:07:44.022: E/AndroidRuntime(1271):    at java.lang.reflect.Method.invokeNative(Native Method)
 06-23 19:07:44.022: E/AndroidRuntime(1271):    at java.lang.reflect.Method.invoke(Method.java:515)
 06-23 19:07:44.022: E/AndroidRuntime(1271):    at  com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
 06-23 19:07:44.022: E/AndroidRuntime(1271):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
 06-23 19:07:44.022: E/AndroidRuntime(1271):    at dalvik.system.NativeStart.main(Native Method)
 06-23 19:07:44.022: E/AndroidRuntime(1271): Caused by: java.lang.NullPointerException
 06-23 19:07:44.022: E/AndroidRuntime(1271):    at food.manager.VerlaufEssen.populateListViewFromDB(VerlaufEssen.java:99)
 06-23 19:07:44.022: E/AndroidRuntime(1271):    at food.manager.VerlaufEssen.onCreate(VerlaufEssen.java:40)
06-23 19:07:44.022: E/AndroidRuntime(1271):     at android.app.Activity.performCreate(Activity.java:5312)
 06-23 19:07:44.022: E/AndroidRuntime(1271):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
 06-23 19:07:44.022: E/AndroidRuntime(1271):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2552)
06-23 19:07:44.022: E/AndroidRuntime(1271):     ... 11 more

我想在创建模式下打开一个Sql数据库

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_verlauf_essen);
getActionBar().setDisplayHomeAsUpEnabled(true);
appContext = getApplicationContext();
openDB();

感谢您的帮助:) 我的开放方法:

myDb = new DBAdapter(this);
myDb.open();

我的数据库适配器:

    public DBAdapter(Context ctx) {
    this.context = ctx;
    myDBHelper = new DatabaseHelper(context);
}

// Open the database connection.
public DBAdapter open() {
    db = myDBHelper.getWritableDatabase();
    return this;
}

// Close the database connection.
public void close() {
    myDBHelper.close();
}

这是我的傀儡方法

private void populateListViewFromDB() {
Cursor cursor = myDb.getAllRows();

// Allow activity to manage lifetime of the cursor.
// DEPRECATED! Runs on the UI thread, OK for small/short queries.
startManagingCursor(cursor);

// Setup mapping from cursor to view fields:
String[] fromFieldNames = new String[] 
        {DBAdapter.KEY_KALO, DBAdapter.KEY_HYDRATE, DBAdapter.KEY_FAT, DBAdapter.KEY_PROTEIN ,DBAdapter.KEY_CAL};
int[] toViewIDs = new int[]
        {R.id.item_Kalorien,     R.id.item_KG_in_g,           R.id.item_F_in_g,     R.id.item_P_in_g,R.id.item_cal};

// Create adapter to may columns of the DB onto elemesnt in the UI.
SimpleCursorAdapter myCursorAdapter = 
        new SimpleCursorAdapter(
                this,       // Context
                R.layout.item_tagebuch, // Row layout template
                cursor,                 // cursor (set of DB records to map)
                fromFieldNames,         // DB Column names
                toViewIDs               // View IDs to put information in
                );

// Set the adapter for the list view
ListView myList = (ListView) findViewById(R.id.listViewTagebuch);
myList.setAdapter(myCursorAdapter);

GetallRows方法>

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

0 个答案:

没有答案