我很困...数据库...

时间:2018-11-28 13:50:01

标签: java sqlite nullpointerexception sql-update android-cursorloader

我尝试将应用程序插入的数据存储到数据库中。所以我选择了SQLiteDatabase,因为那就是 我已经在我的纳米学位中看到了。现在我要在ListView上点击一个项目, 去EditorActivity。当我在EditorActivity.java中对 CursorLoader 进行编码以显示插入的数据并更新它们时,应用程序甚至在启动前就崩溃了……

编辑器活动(此处称为MainActivity,但此处是EditText字段):

public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor>{

private static final int EXISTING_CONTAINER_LOADER = 0;

DexamenesCursorAdapter mCursorAdapter;
private Uri mCurrentContainerUri;

 (...)

@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
// Since the editor shows all container attributes, define a projection that contains
// all columns from the container table
String[] projection = {
        DexameniEntry._ID,
        DexameniEntry.COLUMN_a,
        DexameniEntry.COLUMN_b,
        DexameniEntry.COLUMN_c,};


// This loader will execute the ContentProvider's query method on a background thread
return new CursorLoader(this,
        mCurrentContainerUri,
        projection,
        null,
        null,
        null);

}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data){

if (data.moveToFirst()) {
    if (data.moveToFirst()) {

        // Find the columns of pet attributes that we're interested in
        int aColumnIndex = data.getColumnIndex(DexameniEntry.COLUMN_a);
        int bColumnIndex = data.getColumnIndex(DexameniEntry.COLUMN_b);
        int cColumnIndex = data.getColumnIndex(DexameniEntry.COLUMN_c);


        // Extract out the value from the Cursor for the given column index
        int a = data.getInt(aColumnIndex);
        int b = data.getInt(bColumnIndex);
        int c = data.getInt(cColumnIndex);

        // Update the views on the screen with the values from the database
        mEditX1a.setText(Integer.toString(a));
        mEditX1b.setText(Integer.toString(b));
        mEditX1c.setText(Integer.toString(c));
    }

    mCursorAdapter.swapCursor(data);
}
}

@Override
public void onLoaderReset(Loader<Cursor> loader) {
mCursorAdapter.swapCursor(null);
}

这是 logcat

2018-11-28 15:51:44.853 26449-26470/com.example.android.deyalcalculator 
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: com.example.android.deyalcalculator, PID: 26449
java.lang.RuntimeException: An error occurred while executing 
doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:318)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:760)
Caused by: java.lang.NullPointerException: uri
at com.android.internal.util.Preconditions.checkNotNull(Preconditions.java:111)
at android.content.ContentResolver.query(ContentResolver.java:515)
at android.content.CursorLoader.loadInBackground(CursorLoader.java:64)
at android.content.CursorLoader.loadInBackground(CursorLoader.java:56)
at android.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:312)
at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:69)
at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:66)
at android.os.AsyncTask$2.call(AsyncTask.java:304)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:760)
2018-11-28 15:51:44.891 26449-26449/com.example.android.deyalcalculator E/HAL: PATH3 /odm/lib64/hw/gralloc.qcom.so
 2018-11-28 15:51:44.891 26449-26449/com.example.android.deyalcalculator E/HAL: PATH2 /vendor/lib64/hw/gralloc.qcom.so
2018-11-28 15:51:44.891 26449-26449/com.example.android.deyalcalculator E/HAL: PATH1 /system/lib64/hw/gralloc.qcom.so
 2018-11-28 15:51:44.891 26449-26449/com.example.android.deyalcalculator E/HAL: 
 PATH3 /odm/lib64/hw/gralloc.msm8953.so
2018-11-28 15:51:44.891 26449-26449/com.example.android.deyalcalculator E/HAL: PATH2 /vendor/lib64/hw/gralloc.msm8953.so
 2018-11-28 15:51:44.891 26449-26449/com.example.android.deyalcalculator E/HAL: PATH1 /system/lib64/hw/gralloc.msm8953.so

我不知道如何解决此问题。我什至不知道SQLiteDatabase是否是诚实的最佳选择 因为我希望该应用在很长一段时间内(如与股票相关的应用中)存储公司水质测量数据生成图表(例如在与Stocks相关的应用中)测量,月,年)…

是否有人对解决特殊问题有任何想法,或者总体上来说,有更好的方法来存储此类数据?请…(我对编译大声笑感到疲倦和绝望)

1 个答案:

答案 0 :(得分:-1)

似乎此字段是mCurrentContainerUri为空值或未初始化。

if you see the source code for the class com.android.internal.util.Preconditions

您可以在第111行看到

/**
 * Ensures that an object reference passed as a parameter to the calling
 * method is not null.
 *
 * @param reference an object reference
 * @return the non-null reference that was validated
 * @throws NullPointerException if {@code reference} is null
 */
public static @NonNull <T> T checkNotNull(final T reference) {
    if (reference == null) {
        throw new NullPointerException();
    }
    return reference;
}

在您的情况下uri为null并在其某处调用checkNotNull方法,检查uri是否为null,因此抛出了空指针异常。