异步任务Android中的异常

时间:2014-03-29 06:29:09

标签: android

我正在尝试从parse.com数据库获取数据,但我的应用程序崩溃了。

这是我的代码

     private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
        protected void onPreExecute() {
            super.onPreExecute();
            // Create a progress dialog
            mProgressDialog = new ProgressDialog(CategoryList.this);

            // mProgressDialog.setCancelable(false);
            mProgressDialog.setIndeterminate(false);
            // Show progress dialog
            mProgressDialog.show();
        }

        @Override
        protected Void doInBackground(Void... arg0) {
            // TODO Auto-generated method stub

            mylatlng = new LatLng(lat,lng);
            ParseGeoPoint latlng = new ParseGeoPoint(mylatlng.latitude,
                    mylatlng.longitude);
            try {
                ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
                        tableName);
                ParseQuery<ParseObject> userQuery = new ParseQuery<ParseObject>(
                        "_User");
                query.whereNear("shopLatLng", latlng);
                results = query.find();
                for (ParseObject obj : results) {
                    validityDate = obj.getDate("validity");
                    if (validityDate.compareTo(dateToday) > 0) {
                        Log.d("results", obj.toString());
                        map = new CategoryGridItem();
                        ParseFile itemImage = (ParseFile) obj.get("itemImage");
                        map.setItemImage(itemImage.getUrl());
                        map.setTitle((String) obj.get("couponTitle"));
                        Log.e("title", (String) obj.get("couponTitle"));
                        shopObjectId = (String) obj.get("userObject");
                        shopLatLng = (ParseGeoPoint) obj.get("shopLatLng");
                        map.setShopLatLng(new LatLng(shopLatLng.getLatitude(),
                                shopLatLng.getLongitude()));
                        map.setValidityDate((Date) validityDate);
                        Log.e("date", validityDate.toString());
                        userQuery.getInBackground(shopObjectId,new GetCallback<ParseObject>() {
                        @Override
                        public void done(ParseObject object,ParseException e) {
                        // TODO Auto-generated method stub
                        ParseFile icon = (ParseFile) object.get("logo");
                        map.setShopName((String) object.get("shopName"));
                        Log.e("shopName",(String) object.get("shopName"));
                        map.setShopAddr((String) object.get("shopAddr"));
                        map.setIcon(icon.getUrl());
                        }
                        });
                        categoryItemList.add(map);
                    }
                }

            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }

        protected void onPostExecute(Void result) {
            adapter = new CategoryGridAdapter(CategoryList.this,categoryItemList);
            gridView.setAdapter(adapter);
            mProgressDialog.dismiss();
        }
}

这是我的崩溃日志

03-29 05:56:46.742: E/AndroidRuntime(1541): FATAL EXCEPTION: AsyncTask #1
03-29 05:56:46.742: E/AndroidRuntime(1541): java.lang.RuntimeException: An error occured while         executing doInBackground()
03-29 05:56:46.742: E/AndroidRuntime(1541):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
03-29 05:56:46.742: E/AndroidRuntime(1541):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
03-29 05:56:46.742: E/AndroidRuntime(1541):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
03-29 05:56:46.742: E/AndroidRuntime(1541):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
03-29 05:56:46.742: E/AndroidRuntime(1541):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-29 05:56:46.742: E/AndroidRuntime(1541):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
03-29 05:56:46.742: E/AndroidRuntime(1541):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
03-29 05:56:46.742: E/AndroidRuntime(1541):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
03-29 05:56:46.742: E/AndroidRuntime(1541):     at java.lang.Thread.run(Thread.java:856)
03-29 05:56:46.742: E/AndroidRuntime(1541): Caused by: java.lang.NullPointerException: println needs a message
03-29 05:56:46.742: E/AndroidRuntime(1541):     at android.util.Log.println_native(Native Method)
03-29 05:56:46.742: E/AndroidRuntime(1541):     at android.util.Log.e(Log.java:231)
03-29 05:56:46.742: E/AndroidRuntime(1541):     at fragments.CategoryList$RemoteDataTask.doInBackground(CategoryList.java:157)
03-29 05:56:46.742: E/AndroidRuntime(1541):     at fragments.CategoryList$RemoteDataTask.doInBackground(CategoryList.java:1)
03-29 05:56:46.742: E/AndroidRuntime(1541):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
03-29 05:56:46.742: E/AndroidRuntime(1541):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-29 05:56:46.742: E/AndroidRuntime(1541):     ... 5 more 

03-29 05:56:52.531: E/WindowManager(1541): Activity fragments.CategoryList has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41035788 that was originally added here
03-29 05:56:52.531: E/WindowManager(1541): android.view.WindowLeaked: Activity fragments.CategoryList has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41035788 that was originally added here
03-29 05:56:52.531: E/WindowManager(1541):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:374)
03-29 05:56:52.531: E/WindowManager(1541):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:292)
03-29 05:56:52.531: E/WindowManager(1541):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
03-29 05:56:52.531: E/WindowManager(1541):  at android.view.WindowManagerImplCompatModeWrapper.addView(WindowManagerImpl.java:149)
03-29 05:56:52.531: E/WindowManager(1541):  at android.view.Window$LocalWindowManager.addView(Window.java:547)
03-29 05:56:52.531: E/WindowManager(1541):  at android.app.Dialog.show(Dialog.java:277)
03-29 05:56:52.531: E/WindowManager(1541):  at fragments.CategoryList$RemoteDataTask.onPreExecute(CategoryList.java:86)
03-29 05:56:52.531: E/WindowManager(1541):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
03-29 05:56:52.531: E/WindowManager(1541):  at android.os.AsyncTask.execute(AsyncTask.java:534)
03-29 05:56:52.531: E/WindowManager(1541):  at fragments.CategoryList.onCreate(CategoryList.java:57)
03-29 05:56:52.531: E/WindowManager(1541):  at android.app.Activity.performCreate(Activity.java:5008)
03-29 05:56:52.531: E/WindowManager(1541):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
03-29 05:56:52.531: E/WindowManager(1541):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
03-29 05:56:52.531: E/WindowManager(1541):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
03-29 05:56:52.531: E/WindowManager(1541):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
03-29 05:56:52.531: E/WindowManager(1541):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
03-29 05:56:52.531: E/WindowManager(1541):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-29 05:56:52.531: E/WindowManager(1541):  at android.os.Looper.loop(Looper.java:137)
03-29 05:56:52.531: E/WindowManager(1541):  at android.app.ActivityThread.main(ActivityThread.java:4745)
03-29 05:56:52.531: E/WindowManager(1541):  at java.lang.reflect.Method.invokeNative(Native Method)
03-29 05:56:52.531: E/WindowManager(1541):  at java.lang.reflect.Method.invoke(Method.java:511)
03-29 05:56:52.531: E/WindowManager(1541):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-29 05:56:52.531: E/WindowManager(1541):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-29 05:56:52.531: E/WindowManager(1541):  at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

更改此行...

Log.e("title", (String) obj.get("couponTitle"));

到此......

Log.e("title", obj.get("couponTitle").toString());

在尝试转换为String的位置执行相同的更改。

答案 1 :(得分:0)

试试这个..

抓取

中删除Log.e("Error", e.getMessage());
catch (Exception e) {
      e.printStackTrace();
 }