提问人:Black Blouse 提问时间:11/28/2018 更新时间:11/29/2018 访问量:97
我被困住了......数据库。。。
I’m so stuck…Databases…
问:
我尝试将应用程序的插入数据存储到数据库中。所以我选择了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是否是最佳选择 因为我希望该应用程序能够存储公司水质测量数据并生成很长一段时间内的图表(例如在股票相关应用程序中)(一周测量图、月、年)......
有没有人对特殊问题的解决方案有任何想法,或者一般来说,有一种更好的方法来存储此类数据?请。。。(我编译累了,绝望了,哈哈)
答:
-1赞
Globe Admin
11/28/2018
#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 ,因此抛出空指针异常。
评论
0赞
Black Blouse
11/28/2018
好的,谢谢你。。。因此,我必须将单击的列表项的特定 Uri 分配给 mCurrentContainerUri。你能帮我用代码写这个吗?
0赞
Globe Admin
11/28/2018
没问题。首先尝试分配值并让我知道。如果仍然不起作用,请创建一个 git 项目并共享 url,我会看看。
评论
mCurrentContainerUri