0

我试图从我在 android 上的 sqlite 数据库中加载值,但我在 dataAdapter 上一直为空。我使用此方法在代码中的许多地方加载值。但是在这里我一直为空,我无法找出原因。希望你能帮助我的人。我确信这是一个小错误,但已经过了几天,我似乎找不到它。我在 dataAdapter1 变量的最后几行得到空错误。我把星星放在哪里。

我的数据库是:

public long insertDefaultDistanceUnit(String dUnit, int usedBoolean) {
    ContentValues initialValues = new ContentValues();
    initialValues.put(MY_CURRENT_DISTANCE, dUnit);
    initialValues.put(MY_CURRENT_DISTANCE_BOOLEAN, usedBoolean);
    return db.insert(MY_CURRENT_DISTANCE_UNIT_TABLE, null, initialValues);
}

public Cursor getAllDistanceUnits() {
    return db.query(MY_CURRENT_DISTANCE_UNIT_TABLE,
            new String[] { MY_CURRENT_DISTANCE }, null, null, null, null,
            null);
}

public Cursor getCurrentDistanceUnit() throws SQLException {
    Cursor mCursor = db.query(true, MY_CURRENT_DISTANCE_UNIT_TABLE,
            new String[] { MY_CURRENT_DISTANCE },
            MY_CURRENT_DISTANCE_BOOLEAN + "='1'", null, null, null, null,
            null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}

在打开要加载值的类之前,我将值放入表中的设置类:

    db.open();
    Cursor c2 = db.getAllDistanceUnits();
    distanceList = new ArrayList<String>();
    if (c2.moveToFirst()) {
        do {
            distanceList.add(c2.getString(c2
                    .getColumnIndex(DBAdapter.MY_CURRENT_DISTANCE)));
        } while (c2.moveToNext());
    }
    if (distanceList.size() > 0) {
        distanecExist = 1;
    }
    db.close();

    if (distanecExist == 0) {
        currentDistanceUnit = new ArrayList<String>();
        currentDistanceUnit.add("km" + " Kilometers");
        currentDistanceUnit.add("m" + " Miles");
        db.open();
        for (int i = 0; i <= currentDistanceUnit.size() - 1; i++) {
            if (i == 0) {
                db.insertDefaultDistanceUnit(currentDistanceUnit.get(i)
                        .toString(), 1);
            } else {
                db.insertDefaultDistanceUnit(currentDistanceUnit.get(i)
                        .toString(), 0);
            }
        }
        db.close();
    }

和我想加载值的班级:

    currentDistance = (TextView) findViewById(R.id.currentDistance);
    distanceSpinner = (Spinner) findViewById(R.id.distanceSpinner);
    setNewDistanceButton = (Button) findViewById(R.id.setNewDistanceButton);

    db.open();
    Cursor c2 = db.getCurrentDistanceUnit();
    currentDistanceList2 = new ArrayList<String>();
    String distanceUnit = "";
    if (c2.getCount() == 1) {
        distanceUnit = c2.getString(c2
                .getColumnIndex(DBAdapter.MY_CURRENT_DISTANCE));
    }
    currentDistance.setText(distanceUnit);
    db.close();

    db.open();
    Cursor c1 = db.getAllDistanceUnits();
    currentDistanceList = new ArrayList<String>();
    if (c1.moveToFirst()) {
        do {
            currentDistanceList.add(c1.getString(c1
                    .getColumnIndex(DBAdapter.MY_CURRENT_DISTANCE)));
        } while (c1.moveToNext());
    }
    db.close();
    dataAdapter1 = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, currentDistanceList);
    dataAdapter1
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    distanceSpinner.setAdapter(**dataAdapter1**);
    distanceSpinner.setSelection(0);

这里也是 logcat 错误:

02-11 09:33:46.870: E/AndroidRuntime(27105): FATAL EXCEPTION: main
02-11 09:33:46.870: E/AndroidRuntime(27105):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
02-11 09:33:46.870: E/AndroidRuntime(27105):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
02-11 09:33:46.870: E/AndroidRuntime(27105):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
02-11 09:33:46.870: E/AndroidRuntime(27105):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
02-11 09:33:46.870: E/AndroidRuntime(27105):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-11 09:33:46.870: E/AndroidRuntime(27105):    at android.os.Looper.loop(Looper.java:137)
02-11 09:33:46.870: E/AndroidRuntime(27105):    at android.app.ActivityThread.main(ActivityThread.java:4898)
02-11 09:33:46.870: E/AndroidRuntime(27105):    at java.lang.reflect.Method.invokeNative(Native Method)
02-11 09:33:46.870: E/AndroidRuntime(27105):    at java.lang.reflect.Method.invoke(Method.java:511)
02-11 09:33:46.870: E/AndroidRuntime(27105):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
02-11 09:33:46.870: E/AndroidRuntime(27105):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
02-11 09:33:46.870: E/AndroidRuntime(27105):    at dalvik.system.NativeStart.main(Native Method)
02-11 09:33:46.870: E/AndroidRuntime(27105): Caused by: java.lang.NullPointerException
02-11 09:33:46.870: E/AndroidRuntime(27105):    at com.example.mycarfuel.ChangeDistanceActivity.onCreate(ChangeDistanceActivity.java:41)
02-11 09:33:46.870: E/AndroidRuntime(27105):    at android.app.Activity.performCreate(Activity.java:5206)
02-11 09:33:46.870: E/AndroidRuntime(27105):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
02-11 09:33:46.870: E/AndroidRuntime(27105):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
4

1 回答 1

0

如果我们尝试 setAdapter(null),就不会出现崩溃,也不会出现空指针异常。如果您的崩溃在线

distanceSpinner.setAdapter(**dataAdapter1**);

distanceSpinner 为 null 而不是您的 dataAdapter。

于 2013-02-11T08:04:30.213 回答