[英]App getting error while trying to report error
所以我正在使用androidhive教程为我的应用程序制作服务器并连接到它。 我有它所以服务器将发回不同的消息取决于发送的内容但我收到错误,我无法弄清楚原因。 以下是发生错误的类:
class CreateNewSpot extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(NewSpotActivity.this);
pDialog.setMessage("Creating Spot..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* Creating product
* */
protected String doInBackground(String... args) {
String name = inputName.getText().toString();
String longitude = inputLong;
String latitude = inputLat;
String pavement = spinner_pavement.getSelectedItem().toString();
String traffic = spinner_traffic.getSelectedItem().toString();
String environment = spinner_enviro.getSelectedItem().toString();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name", name));
params.add(new BasicNameValuePair("longitude", longitude));
params.add(new BasicNameValuePair("latitude", latitude));
params.add(new BasicNameValuePair("pavement", pavement));
params.add(new BasicNameValuePair("traffic", traffic));
params.add(new BasicNameValuePair("environment", environment));
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_create,
"POST", params);
// check log cat fro response
Log.d("Create Response", json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
switch(success){
case 0:
//name is empty!
break;
case 1:
// successfully created product
Intent i = new Intent(getApplicationContext(),
AllSpotsActivity.class);
startActivity(i);
// closing this screen
finish();
break;
case 2:
//name has been taken
Toast.makeText(getApplicationContext(), "Name for spot has already been taken.", Toast.LENGTH_LONG).show();
break;
case 3:
//server error
Toast.makeText(getApplicationContext(), "A server error has occurred.", Toast.LENGTH_LONG).show();
break;
default:
Toast.makeText(getApplicationContext(), "An unknown error has occurred.", Toast.LENGTH_LONG).show();
//just an unknown error
break;
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
}
}
现在我故意发送数据以获得成功== 2但它告诉我我的应用程序意外错误已经发生。 为什么是这样? 是因为pDialog还在开放吗? 我试过把pDialog.dismiss(); 上面但我仍然得到错误。 对不起,如果这是一个简单的问题,请提前感谢您。
泰勒
编辑:Logcat:
class CreateNewSpot extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(NewSpotActivity.this);
pDialog.setMessage("Creating Spot..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* Creating product
* */
protected String doInBackground(String... args) {
String name = inputName.getText().toString();
String longitude = inputLong;
String latitude = inputLat;
String pavement = spinner_pavement.getSelectedItem().toString();
String traffic = spinner_traffic.getSelectedItem().toString();
String environment = spinner_enviro.getSelectedItem().toString();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name", name));
params.add(new BasicNameValuePair("longitude", longitude));
params.add(new BasicNameValuePair("latitude", latitude));
params.add(new BasicNameValuePair("pavement", pavement));
params.add(new BasicNameValuePair("traffic", traffic));
params.add(new BasicNameValuePair("environment", environment));
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_create,
"POST", params);
// check log cat fro response
Log.d("Create Response", json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
switch(success){
case 0:
//name is empty!
break;
case 1:
// successfully created product
Intent i = new Intent(getApplicationContext(),
AllSpotsActivity.class);
startActivity(i);
// closing this screen
finish();
break;
case 2:
//name has been taken
error_msg = 0;
break;
case 3:
//server error
error_msg = 1;
break;
default:
error_msg = 2;
//just an unknown error
break;
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
switch(error_msg){
case 0:
Toast.makeText(getApplicationContext(), "Name for spot has already been taken.", Toast.LENGTH_LONG).show();
break;
case 1:
Toast.makeText(getApplicationContext(), "A server error has occurred.", Toast.LENGTH_LONG).show();
break;
case 2:
Toast.makeText(getApplicationContext(), "An unknown error has occurred.", Toast.LENGTH_LONG).show();
break;
default:
break;
}
pDialog.dismiss();
}
}
您将收到意外错误,因为您正在从doInBackground()显示Toast,这是您无法做到的。 您永远不会在AsyncTask中从后台处理您的UI。 只是删除从doInBackground()的try-catch块来onPostExecute(),它会工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.