Android.OS.NetworkOnMainThreadException中

时间:2015-12-04 14:03:32

标签: java android runtime-error

我正在尝试获取用户的Facebook个人资料图片及其缩略图,并在下载后将其添加到解析课程中。看看

@Override
protected String doInBackground(String[] Params) 
{

    String accessToken = AccessToken ;
    String email = EMAIL;
    profile =  com.facebook.Profile.getCurrentProfile();
    User.put("displayName", profile.getName());
    User.put("facebookId", profile.getId());
    User.setUsername(profile.getId());
    try
    {
            User.put("FBaccessToken", accessToken);
            User.setEmail(email);
    }
    catch (Exception e){

        Log.e("ERROR","Failed");
    }
    User.setPassword(profile.getId() + profile.getId());
    ParseQuery<ParseObject> query = ParseQuery.getQuery("User");
    query.whereEqualTo("username", profile.getId());
    query.findInBackground(new FindCallback<ParseObject>() {
        @Override
        public void done(List<ParseObject> list, ParseException e)
        {
            if (e == null)
            {
                //brace the lord no error
                if (list == null)
                {
                    parseSignUp() ;
                }
                else
                {
                    if  (list.isEmpty())
                    {
                        parseSignUp() ;
                    }
                    else
                    {
                        login(profile.getId(), (profile.getId() + profile.getId()));
                    }
                    for (ParseObject str : list)
                    {
                            Log.e("ERROR", "what ?");
                    }
                }


            }
            else
            {
                Log.e("ERROR", "not");

            }
        }
    });
    return "done";
   }

Login.java

   SendFeedBackJob newJob = new SendFeedBackJob();

                                    newJob.progressBar = progressBar ;


                                    newJob.EMAIL = email ;
                                    newJob.AccessToken = accessToken.getToken();

                                    newJob.invisible = View.INVISIBLE ;

                                    newJob.show = View.VISIBLE ;
                                    newJob.execute();

即使网络内容仍在AsyncTask中,此代码仍然会抛出此错误

  12-04 19:02:27.773    4263-4263/com.socketmill.thisismohit.vine       E/ERROR﹕android.os.NetworkOnMainThreadException
  12-04 19:02:27.803    4263-4263/com.socketmill.thisismohit.vine E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
        at com.parse.ParseFile.<init>(ParseFile.java:188)
        at com.parse.ParseFile.<init>(ParseFile.java:218)
        at com.socketmill.thisismohit.vine.SendFeedBackJob.parseSignUp(SendFeedBackJob.java:207)
        at com.socketmill.thisismohit.vine.SendFeedBackJob$1.done(SendFeedBackJob.java:90)
        at com.socketmill.thisismohit.vine.SendFeedBackJob$1.done(SendFeedBackJob.java:75)
        at com.parse.ParseTaskUtils$2$1.run(ParseTaskUtils.java:115)
        at android.os.Handler.handleCallback(Handler.java:615)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4921)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
        at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:2)

仅仅因为它在doInBackground中启动,并不意味着内部的整个代码都在后台线程中运行。也许您正在使用的库正在发送使用UI线程提供回调。

阅读documentationquery.findInBackground(new FindCallback<ParseObject>() {})方法将在用户界面线程上运行public void done(List<ParseObject> list, ParseException e)方法。

这意味着您的parseSignUp()方法以及您的geByteArrayFromURL方法也在UI线程上运行。

事实上,考虑到Parse Library为您处理这一切的事实。您应该完全从AsyncTask中删除Parse Library。因为它会在库完成执行之前转到onPostExecute()(在doInBackground内启动一个新线程。

答案 1 :(得分:2)

FindInBackground的完成回调方法在UI线程上运行,而不是在异步线程上运行