运行时异常:在Activity执行doInBackground时出错
Posted
技术标签:
【中文标题】运行时异常:在Activity执行doInBackground时出错【英文标题】:Runtime Exception:An error occurred while executing doInBackground at Activity 【发布时间】:2015-01-27 08:54:49 【问题描述】:我收到此运行时错误:
java.lang.RuntimeException: 执行时出错 做背景()。应用程序上下文为空。你必须 使用前调用 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);
【问题讨论】:
【参考方案1】:你必须打电话
Parse.initialize(context, PARSE_APPLICATION_ID, PARSE_CLIENT_KEY);
在您的Application
或Activity
中
有关更多信息,请参阅此 SO post
【讨论】:
让我知道我必须在哪里打电话? @Naruto 尝试建议的链接 再次非常感谢您:)【参考方案2】:你必须调用 Parse.initialize(context, applicationId, clientKey) 在使用 Parse 库之前。
表示您没有在 Application
类中使用应用程序上下文、app_key 和 clientkey 调用 initialize
。
要在项目中使用Parse
库,您需要在项目中添加Application
类,并在Application
类的onCreate
方法中调用Parse 库的initialize
方法,然后再调用其他方法:
// Initialize the Parse SDK.
Parse.initialize(this, "XXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXX");
查看Android’s Application Class 示例以在项目中添加Application
类
【讨论】:
以上是关于运行时异常:在Activity执行doInBackground时出错的主要内容,如果未能解决你的问题,请参考以下文章
关于Fragment.onCreate()中引用Activity.onCreate()创建的对象时的一个异常
setContentView(R.layout.activity_main)的运行时错误