Android 在调用web服务期间关闭应用程序
我正在编写一个应用程序,它必须从web服务获取一些json对象。 它运转得很好。但是,我想找到一种方法避免用户在无法下载这些json对象的情况下使用。 所以,我使用了一个错误的URL。 现在,我的应用程序强制关闭而不避开用户 电话:Android 在调用web服务期间关闭应用程序,android,Android,我正在编写一个应用程序,它必须从web服务获取一些json对象。 它运转得很好。但是,我想找到一种方法避免用户在无法下载这些json对象的情况下使用。 所以,我使用了一个错误的URL。 现在,我的应用程序强制关闭而不避开用户 电话: /* * Async call to fill the category spinner */ private class CallCategory extends AsyncTask<Void,Void,Void>{ private Progres
/*
* Async call to fill the category spinner
*/
private class CallCategory extends AsyncTask<Void,Void,Void>{
private ProgressDialog dialog;
protected void onPreExecute() {
this.dialog = ProgressDialog.show(ApPictureActivity.this, "Chargement", "Récupération des catégories...", true);
}
protected void onPostExecute(Void v) {
dialog.cancel();
Log.i("fill the spiner", _ListCategory.toString());
if(!_ListCategory.isEmpty())
FillSpinner();
else
{
AlertDialog alertDialog = new AlertDialog.Builder(ApPictureActivity.this).create();
alertDialog.setTitle(getResources().getString(R.string.errorCategoryTitle));
alertDialog.setMessage(getResources().getString(R.string.errorCategoryMessage));
//add "Ok button"
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
} });
alertDialog.show();
//log
Log.i("List category empty", "No categories");
//ApPictureActivity.this.finish();
}
}
protected Void doInBackground(Void... params) {
try{
_ListCategory = ServerCall.GetCategory();}
catch (Exception e) {
Log.i("load categories error",e.toString());
}
return null;
}
}
我确信这段代码使用的是正确的URL。所以这不是一个实现问题。可能是一个例外没有被抓住
尊敬。我跟随了dj aquell adivies,并通过以下方式更改了onPostExecute方法:
protected void onPostExecute(Void v) {
dialog.cancel();
if(_ListCategory != null){
Log.i("fill the spiner", _ListCategory.toString());
FillSpinner();}
else
{
AlertDialog alertDialog = new AlertDialog.Builder(ApPictureActivity.this).create();
alertDialog.setTitle(getResources().getString(R.string.errorCategoryTitle));
alertDialog.setMessage(getResources().getString(R.string.errorCategoryMessage));
//add "Ok button"
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//log
Log.i("List category empty", "No categories");
ApPictureActivity.this.finish();
} });
alertDialog.show();
}
}
异常的Logcat输出@David ApPictureActivity.java文件中的第299行是哪一行?@djaqeel Log.i(“填充spiner”,_ListCategory.toString())@djaqeel位于AppictureActivity顶部(CallCategory是AppictureActivity文件中的一个实习生类)。我也试着抑制这一行,但这并没有改变问题代码>方法返回null!!!由于任何原因。
07-09 09:37:49.446: E/AndroidRuntime(4322): Uncaught handler: thread main exiting due to uncaught exception
07-09 09:37:49.455: E/AndroidRuntime(4322): java.lang.NullPointerException
07-09 09:37:49.455: E/AndroidRuntime(4322): at ApPicture.Android.ApPictureActivity$CallCategory.onPostExecute(ApPictureActivity.java:299)
07-09 09:37:49.455: E/AndroidRuntime(4322): at ApPicture.Android.ApPictureActivity$CallCategory.onPostExecute(ApPictureActivity.java:1)
07-09 09:37:49.455: E/AndroidRuntime(4322): at android.os.AsyncTask.finish(AsyncTask.java:417)
07-09 09:37:49.455: E/AndroidRuntime(4322): at android.os.AsyncTask.access$300(AsyncTask.java:127)
07-09 09:37:49.455: E/AndroidRuntime(4322): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
07-09 09:37:49.455: E/AndroidRuntime(4322): at android.os.Handler.dispatchMessage(Handler.java:99)
07-09 09:37:49.455: E/AndroidRuntime(4322): at android.os.Looper.loop(Looper.java:123)
07-09 09:37:49.455: E/AndroidRuntime(4322): at android.app.ActivityThread.main(ActivityThread.java:4363)
07-09 09:37:49.455: E/AndroidRuntime(4322): at java.lang.reflect.Method.invokeNative(Native Method)
07-09 09:37:49.455: E/AndroidRuntime(4322): at java.lang.reflect.Method.invoke(Method.java:521)
07-09 09:37:49.455: E/AndroidRuntime(4322): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-09 09:37:49.455: E/AndroidRuntime(4322): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-09 09:37:49.455: E/AndroidRuntime(4322): at dalvik.system.NativeStart.main(Native Method)
protected void onPostExecute(Void v) {
dialog.cancel();
if(_ListCategory != null){
Log.i("fill the spiner", _ListCategory.toString());
FillSpinner();}
else
{
AlertDialog alertDialog = new AlertDialog.Builder(ApPictureActivity.this).create();
alertDialog.setTitle(getResources().getString(R.string.errorCategoryTitle));
alertDialog.setMessage(getResources().getString(R.string.errorCategoryMessage));
//add "Ok button"
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//log
Log.i("List category empty", "No categories");
ApPictureActivity.this.finish();
} });
alertDialog.show();
}
}