简体   繁体   English

应用程序在尝试报告错误时收到错误

[英]App getting error while trying to report error

so I'm using the androidhive tutorial to make a server for my app and connect to it. 所以我正在使用androidhive教程为我的应用程序制作服务器并连接到它。 I have it so the server will send back different messages depending on what was sent in but I'm getting an error with it and I can't figure out why. 我有它所以服务器将发回不同的消息取决于发送的内容但我收到错误,我无法弄清楚原因。 Here is the class that the error occurs in: 以下是发生错误的类:

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();
    }
}

Now I'm purposely sending in data to get success==2 but it tells me my app unexpected error has occurred. 现在我故意发送数据以获得成功== 2但它告诉我我的应用程序意外错误已经发生。 Why is this? 为什么是这样? Is it because of the pDialog is still open? 是因为pDialog还在开放吗? I tried putting pDialog.dismiss(); 我试过把pDialog.dismiss(); above but I still get the error. 上面但我仍然得到错误。 Sorry if this is a simple question and thank you in advance. 对不起,如果这是一个简单的问题,请提前感谢您。

Tyler 泰勒

EDIT: Logcat: 编辑: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();

    }
 }

You are getting unexpected error because you are showing Toast from doInBackground(), which you can't do. 您将收到意外错误,因为您正在从doInBackground()显示Toast,这是您无法做到的。 You never handle your UI from background in AsyncTask. 您永远不会在AsyncTask中从后台处理您的UI。 Just remove your try-catch block from doInBackground() to onPostExecute() and it will work. 只是删除从doInBackground()的try-catch块来onPostExecute(),它会工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM