运行时异常:在Activity处执行doInBackground时发生错误

时间:2015-01-27 08:54:49

标签: android

我收到此运行时错误:

  

java.lang.RuntimeException:执行时发生错误   doInBackground()。 applicationContext为null。你必须   在使用之前调用Parse.initialize(context,applicationId,clientKey)   Parse库。

当我点击设置图标时,应用程序会抛出这样的运行时错误。

我发布了代码,并指出了下面的错误行:

堆栈跟踪:

01-27 08:43:32.524: E/AndroidRuntime(3323): FATAL EXCEPTION: AsyncTask #1
01-27 08:43:32.524: E/AndroidRuntime(3323): Process: com.qrme.quranmadeeasy, PID: 3323
01-27 08:43:32.524: E/AndroidRuntime(3323): java.lang.RuntimeException: An error occured while executing doInBackground()
01-27 08:43:32.524: E/AndroidRuntime(3323):     at android.os.AsyncTask$3.done(AsyncTask.java:300)
01-27 08:43:32.524: E/AndroidRuntime(3323):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
01-27 08:43:32.524: E/AndroidRuntime(3323):     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
01-27 08:43:32.524: E/AndroidRuntime(3323):     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
01-27 08:43:32.524: E/AndroidRuntime(3323):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
01-27 08:43:32.524: E/AndroidRuntime(3323):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
01-27 08:43:32.524: E/AndroidRuntime(3323):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
01-27 08:43:32.524: E/AndroidRuntime(3323):     at java.lang.Thread.run(Thread.java:841)
01-27 08:43:32.524: E/AndroidRuntime(3323): Caused by: java.lang.RuntimeException: applicationContext is null. You must call Parse.initialize(context, applicationId, clientKey) before using the Parse library.
01-27 08:43:32.524: E/AndroidRuntime(3323):     at com.parse.Parse.checkContext(Parse.java:518)
01-27 08:43:32.524: E/AndroidRuntime(3323):     at com.parse.Parse.getParseDir(Parse.java:235)
01-27 08:43:32.524: E/AndroidRuntime(3323):     at com.parse.ParseObject.getDiskObject(ParseObject.java:382)
01-27 08:43:32.524: E/AndroidRuntime(3323):     at com.parse.ParseObject.getFromDisk(ParseObject.java:486)
01-27 08:43:32.524: E/AndroidRuntime(3323):     at com.parse.ParseInstallation.getCurrentInstallation(ParseInstallation.java:78)
01-27 08:43:32.524: E/AndroidRuntime(3323):     at com.qrme.quranmadeeasy.SettingsActivity.refreshUserProfile(SettingsActivity.java:485)
01-27 08:43:32.524: E/AndroidRuntime(3323):     at com.qrme.quranmadeeasy.SettingsActivity.access$0(SettingsActivity.java:484)
01-27 08:43:32.524: E/AndroidRuntime(3323):     at com.qrme.quranmadeeasy.SettingsActivity$checkforupdate.doInBackground(SettingsActivity.java:629)
01-27 08:43:32.524: E/AndroidRuntime(3323):     at com.qrme.quranmadeeasy.SettingsActivity$checkforupdate.doInBackground(SettingsActivity.java:1)
01-27 08:43:32.524: E/AndroidRuntime(3323):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
01-27 08:43:32.524: E/AndroidRuntime(3323):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
01-27 08:43:32.524: E/AndroidRuntime(3323):     ... 4 more

SettingsActivity.java:

public class SettingsActivity extends Activity {
static ProgressDialog pd;

    Dialog dialog;
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

    }

........
........

private void refreshUserProfile() { --->484th line
        ParseInstallation.getCurrentInstallation().refreshInBackground(  --->485th line
                new RefreshCallback() {

                    @Override
                    public void done(ParseObject object, ParseException e) {

                        if (e == null) {

                            displayUserProfile();

                        }  
                    }
                });
    }

    public class checkforupdate extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub

            // TODO Auto-generated method stub

            refreshUserProfile();   ---->629th line

            return null;
        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();

        }

        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            // pd.dismiss();
            // finish();
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            // TODO Auto-generated method stub
            super.onProgressUpdate(values);
        }

    }

2 个答案:

答案 0 :(得分:2)

你必须致电

  Parse.initialize(context, PARSE_APPLICATION_ID, PARSE_CLIENT_KEY);

ApplicationActivity

有关详细信息,请参阅此SO post

答案 1 :(得分:1)

  

你必须调用Parse.initialize(context,applicationId,clientKey)   在使用Parse库之前。

表示您没有使用initialize类中的应用程序上下文,app_key和clientkey调用Application

要在项目中使用Parse库,您需要在项目中添加Application类,并在onCreate方法中调用Application类调用initialize方法。库之前调用其他方法:

// Initialize the Parse SDK.
  Parse.initialize(this, "XXXXXXXXXXXXXXXXXXX",    "XXXXXXXXXXXXXXXXXXX"); 

请参阅Android’s Application Class示例,在项目中添加Application