我有一个android AsyncTask
类来尝试访问SQLite数据库。一切都运转正常,但我有错误,我不知道如何解决它们。
这是我的班级:
protected HomeActivity doInBackground(HomeActivity... params) {
Cursor cursor = null;
// dbhandler.addShares(update_list());
cursor = dbhandler.getShares();
while (cursor.moveToNext()) {
String name, price, cash_change, percentage_change;
name = cursor.getString(cursor.getColumnIndex(KEY_NAME));
price = cursor.getString(cursor.getColumnIndex(KEY_PRICE));
cash_change = cursor.getString(cursor
.getColumnIndex(KEY_CASH_CHANGE));
percentage_change = cursor.getString(cursor
.getColumnIndex(KEY_PERCENTAGE_CHANGE));
SharePrices sharePrices = new SharePrices();
sharePrices.setName(name);
sharePrices.setPrice(price);
sharePrices.setPercentageChange(percentage_change);
sharePrices.setCashChange(cash_change);
shares.add(sharePrices);
}
if (null != cursor) {
cursor.close();
}
return null;
}
堆栈跟踪:
09-26 17:15:54.590: E/AndroidRuntime(2069): FATAL EXCEPTION: AsyncTask #1
09-26 17:15:54.590: E/AndroidRuntime(2069): java.lang.RuntimeException: An error occured while executing doInBackground()
09-26 17:15:54.590: E/AndroidRuntime(2069): at android.os.AsyncTask$3.done(AsyncTask.java:278)
09-26 17:15:54.590: E/AndroidRuntime(2069): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
09-26 17:15:54.590: E/AndroidRuntime(2069): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
09-26 17:15:54.590: E/AndroidRuntime(2069): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
09-26 17:15:54.590: E/AndroidRuntime(2069): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-26 17:15:54.590: E/AndroidRuntime(2069): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
09-26 17:15:54.590: E/AndroidRuntime(2069): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
09-26 17:15:54.590: E/AndroidRuntime(2069): at java.lang.Thread.run(Thread.java:856)
09-26 17:15:54.590: E/AndroidRuntime(2069): Caused by: java.lang.NullPointerException
09-26 17:15:54.590: E/AndroidRuntime(2069): at com.intelligent.stocktrader.HomeActivity$getShares.doInBackground(HomeActivity.java:106)
09-26 17:15:54.590: E/AndroidRuntime(2069): at com.intelligent.stocktrader.HomeActivity$getShares.doInBackground(HomeActivity.java:1)
09-26 17:15:54.590: E/AndroidRuntime(2069): at android.os.AsyncTask$2.call(AsyncTask.java:264)
09-26 17:15:54.590: E/AndroidRuntime(2069): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-26 17:15:54.590: E/AndroidRuntime(2069): ... 4 more
09-26 17:15:55.331: E/WindowManager(2069): Activity com.intelligent.stocktrader.HomeActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41190a88 that was originally added here
09-26 17:15:55.331: E/WindowManager(2069): android.view.WindowLeaked: Activity com.intelligent.stocktrader.HomeActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41190a88 that was originally added here
09-26 17:15:55.331: E/WindowManager(2069): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:343)
09-26 17:15:55.331: E/WindowManager(2069): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:245)
09-26 17:15:55.331: E/WindowManager(2069): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:193)
09-26 17:15:55.331: E/WindowManager(2069): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:118)
09-26 17:15:55.331: E/WindowManager(2069): at android.view.Window$LocalWindowManager.addView(Window.java:537)
09-26 17:15:55.331: E/WindowManager(2069): at android.app.Dialog.show(Dialog.java:274)
09-26 17:15:55.331: E/WindowManager(2069): at com.intelligent.stocktrader.HomeActivity$getShares.onPreExecute(HomeActivity.java:81)
09-26 17:15:55.331: E/WindowManager(2069): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561)
09-26 17:15:55.331: E/WindowManager(2069): at android.os.AsyncTask.execute(AsyncTask.java:511)
09-26 17:15:55.331: E/WindowManager(2069): at com.intelligent.stocktrader.HomeActivity.onCreate(HomeActivity.java:71)
09-26 17:15:55.331: E/WindowManager(2069): at android.app.Activity.performCreate(Activity.java:4465)
09-26 17:15:55.331: E/WindowManager(2069): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-26 17:15:55.331: E/WindowManager(2069): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
09-26 17:15:55.331: E/WindowManager(2069): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
09-26 17:15:55.331: E/WindowManager(2069): at android.app.ActivityThread.access$600(ActivityThread.java:122)
09-26 17:15:55.331: E/WindowManager(2069): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
09-26 17:15:55.331: E/WindowManager(2069): at android.os.Handler.dispatchMessage(Handler.java:99)
09-26 17:15:55.331: E/WindowManager(2069): at android.os.Looper.loop(Looper.java:137)
09-26 17:15:55.331: E/WindowManager(2069): at android.app.ActivityThread.main(ActivityThread.java:4340)
09-26 17:15:55.331: E/WindowManager(2069): at java.lang.reflect.Method.invokeNative(Native Method)
09-26 17:15:55.331: E/WindowManager(2069): at java.lang.reflect.Method.invoke(Method.java:511)
09-26 17:15:55.331: E/WindowManager(2069): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-26 17:15:55.331: E/WindowManager(2069): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-26 17:15:55.331: E/WindowManager(2069): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
您必须使用startManagingCursor()/ cursor.close()