在我的应用程序中有很多数据库操作,所以我创建了许多游标来检索数据,并且Async Task中也使用了很少的游标。现在我的问题是关闭光标和数据库的位置。
当我在异步操作后关闭onCreate
中的游标时我得到Null Pointer Exception
,如果我在onPause
关闭,我仍然得到Null pointer exception
,所以我有关闭了onDestroy
中的游标和数据库。
现在我很困惑这种方法是否正确..我想了解在活动中关闭游标和数据库的最佳方法是什么,而不会对数据库操作产生任何影响。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.stockdetail);
//Helptext.setVisibility(View.INVISIBLE);
Declerations();
d=Database.getInstance(getApplicationContext()).getWritableDatabase();
new popdailydata().execute();
// Above is async task so I have closed after above statement, but if I close cursor getting Null pointer exception
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
selectdate.close();
Database.getInstance(getApplicationContext()).close();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
//selectdate.close(); --- At this point also I am getting nullpointer exception
Database.getInstance(getApplicationContext()).close();
}
04-01 17:03:48.045: D/dalvikvm(1288): Late-enabling CheckJNI
04-01 17:03:50.021: D/libEGL(1288): loaded /system/lib/egl/libEGL_genymotion.so
04-01 17:03:50.113: D/(1288): HostConnection::get() New Host Connection established 0xb90bb5c8, tid 1288
04-01 17:03:50.177: D/libEGL(1288): loaded /system/lib/egl/libGLESv1_CM_genymotion.so
04-01 17:03:50.181: D/libEGL(1288): loaded /system/lib/egl/libGLESv2_genymotion.so
04-01 17:03:50.773: W/EGL_genymotion(1288): eglSurfaceAttrib not implemented
04-01 17:03:50.797: E/OpenGLRenderer(1288): Getting MAX_TEXTURE_SIZE from GradienCache
04-01 17:03:50.861: E/OpenGLRenderer(1288): Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
04-01 17:03:50.865: D/OpenGLRenderer(1288): Enabling debug mode 0
04-01 17:03:54.645: D/dalvikvm(1288): GC_FOR_ALLOC freed 102K, 1% free 16948K/17072K, paused 89ms, total 96ms
04-01 17:03:54.709: D/AndroidRuntime(1288): Shutting down VM
04-01 17:03:54.725: W/dalvikvm(1288): threadid=1: thread exiting with uncaught exception (group=0xa4c45648)
04-01 17:03:54.745: E/AndroidRuntime(1288): FATAL EXCEPTION: main
04-01 17:03:54.745: E/AndroidRuntime(1288): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.portfoliomanager/com.example.StockDirector.stockmanager}: java.lang.NullPointerException
04-01 17:03:54.745: E/AndroidRuntime(1288): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
04-01 17:03:54.745: E/AndroidRuntime(1288): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
04-01 17:03:54.745: E/AndroidRuntime(1288): at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-01 17:03:54.745: E/AndroidRuntime(1288): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
04-01 17:03:54.745: E/AndroidRuntime(1288): at android.os.Handler.dispatchMessage(Handler.java:99)
04-01 17:03:54.745: E/AndroidRuntime(1288): at android.os.Looper.loop(Looper.java:137)
04-01 17:03:54.745: E/AndroidRuntime(1288): at android.app.ActivityThread.main(ActivityThread.java:5103)
04-01 17:03:54.745: E/AndroidRuntime(1288): at java.lang.reflect.Method.invokeNative(Native Method)
04-01 17:03:54.745: E/AndroidRuntime(1288): at java.lang.reflect.Method.invoke(Method.java:525)
04-01 17:03:54.745: E/AndroidRuntime(1288): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
04-01 17:03:54.745: E/AndroidRuntime(1288): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-01 17:03:54.745: E/AndroidRuntime(1288): at dalvik.system.NativeStart.main(Native Method)
04-01 17:03:54.745: E/AndroidRuntime(1288): Caused by: java.lang.NullPointerException
04-01 17:03:54.745: E/AndroidRuntime(1288): at com.example.StockDirector.stockmanager.onCreate(stockmanager.java:81)
04-01 17:03:54.745: E/AndroidRuntime(1288): at android.app.Activity.performCreate(Activity.java:5133)
04-01 17:03:54.745: E/AndroidRuntime(1288): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-01 17:03:54.745: E/AndroidRuntime(1288): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
04-01 17:03:54.745: E/AndroidRuntime(1288): ... 11 more
04-01 17:03:57.889: I/Process(1288): Sending signal. PID: 1288 SIG: 9
答案 0 :(得分:1)
对于android数据库操作的整体操作,这里是使用内容提供程序的一个很好的信息来源: http://www.vogella.com/tutorials/AndroidSQLite/article.html
这是一个如何实现LoaderManager的示例。您可以在此处找到各种示例:http://developer.android.com/reference/android/app/LoaderManager.html
对于一个基本的实现思路,它应该是这样的:
public class MainActivity extends Activity implements LoaderManager.LoaderCallbacks<Cursor> {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return new CursorLoader(this,
ObjectEntity.URI, // Database uri
new String[]{"id"}, // Columns to select
"id=?", // 'where' field
new String[]{"3"}, // Values for 'where' '?' field
null);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
// Do anything you want here with the cursor
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
}
}
答案 1 :(得分:0)
您应该尽快关闭Database / Cursor连接,这意味着一旦从中提取数据,您应该关闭所有内容,以免锁定数据库。
保持数据库连接是不好的做法,特别是对于多线程应用程序