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

问题描述 投票:0回答:1

我尝试将我的应用程序的插入数据存储到数据库中。所以我选择了SQLiteDatabase,因为这是我在Nanodegree中看到的。现在我想要点击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是否是最诚实的选择,因为我希望应用程序存储公司水质测量数据并在很长一段时间内生成图表(如股票相关应用程序)(图表一周的测量,月,年)...

有没有人对特定问题的解决方案有任何想法,或者一般来说存储这类数据的更好方法?请...(我很累,绝望编译大声笑)

java sqlite nullpointerexception sql-update android-cursorloader
1个回答
-1
投票

看来这个字段是mCurrentContainerUri是null值还是没有初始化。

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,因此抛出空指针异常。

© www.soinside.com 2019 - 2024. All rights reserved.