【问题标题】:Main thread crash after AsyncTask is finishedAsyncTask 完成后主线程崩溃
【发布时间】:2013-07-11 22:01:40
【问题描述】:

我正在使用 AsyncTask 从我的服务器获取字符串。

我的问题是,在 AsyncTask 完成后,我的主线程崩溃了,而应用程序是

加载上一个窗口。

奇怪的是,我的 logcat 中没有收到任何错误代码。

感谢您的帮助。

这是我的代码和 logcat:

07-12 00:58:51.162: I/ActivityManager(1834): START u0 {cmp=com.tomer.workoutlog/com.example.workoutlog.AddWorkOutPage (has extras)} from pid 28701
07-12 00:58:51.182: D/AudioHardwareMSM7X30(1489): do_aic3254_control device: 1 mode: 0 record: 0
07-12 00:58:51.182: D/AudioHardwareMSM7X30(1489): aic3254_ioctl: new_aic_rxmode 13 cur_aic_rx 29
07-12 00:58:51.182: D/AudioHardwareMSM7X30(1489): aic3254_ioctl()
07-12 00:58:51.182: D/AudioHardwareMSM7X30(1489): aic3254_ioctl: try ioctl 0x40047313 with arg 13
07-12 00:58:51.192: D/AudioHardwareMSM7X30(1489): aic3254_ioctl: new_aic_txmode 29 cur_aic_tx 29
07-12 00:58:51.192: D/AudioHardwareMSM7X30(1489): value of device and enable is 6 1 ALSA dev id:6
07-12 00:58:51.232: D/AudioHardwareMSM7X30(1489): updateACDB: (11, 6, 0, 607) 
07-12 00:58:51.232: I/HTC Acoustic(1489): update ACDB id: (tx, rx, tx_acdb, rx_acdb) = (11, 6, 0, 607)
07-12 00:58:51.232: D/AudioHardwareMSM7X30(1489): msm_route_stream(PCM_PLAY,5,6,1)
07-12 00:58:51.652: I/ActivityManager(1834): Displayed com.tomer.workoutlog/com.example.workoutlog.AddWorkOutPage: +474ms
07-12 00:58:52.033: W/InputMethodManagerService(1834): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@41948370 attribute=null, token = android.os.BinderProxy@415ff880

代码:

   class GetKey extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(AddWorkOutPage.this);
            pDialog.setMessage("loading...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        /**
         * Creating product
         * */
        protected String doInBackground(String... args) {

                JSONParser jsonParser = new JSONParser();
                JSONObject json = null;
                // Building Parameters
                List<NameValuePair> params = new ArrayList<NameValuePair>();

                params.add(new BasicNameValuePair("exercise", exercise));

                json = jsonParser.makeHttpRequest(url_get_key,"GET", params);

                try {
                    base64EncodedPublicKey = json.getString("key");
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            // check for success tag
            try {
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    // successfully created product

                    //Log.d("ok", json.toString());
                    // closing this screen
                    finish();
                } else {
                    // failed to create product
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            pDialog.dismiss();

        }

    }

【问题讨论】:

  • 你为什么告诉onPostExecute()接受String却从doInBackground()返回null
  • 你怎么知道你崩溃了? stacktrace 中是否有异常或任何其他指示崩溃的消息?
  • 我可以删除字符串,你认为这是造成问题的原因吗?我知道它崩溃了,因为我在我的设备上看到它
  • 我不知道,但请查看我的答案,看看是否有帮助

标签: android android-asynctask


【解决方案1】:

两个潜在问题:

  1. 在我的评论中,您告诉onPostExecute() 接受String,但在doInBackground() 中返回null。我不确定这会导致问题,但我从未尝试过。如果您不想退回任何东西,您可以更改为

    protected void onPostExecute(Void response)

并将您的类声明更改为

 class GetKey extends AsyncTask<String, Void, Void> {

假设您在调用任务时在execute() 中传递了String

2 您在doInBackground() 中调用finish(),但AsyncTask 没有finish() 方法,因此您要么完成Activity,要么在那里出错。

【讨论】:

  • 谢谢。我删除了完成并更改为 AsyncTask 并且它正在工作
  • 顺便说一句,返回中的 null 不是问题(但它没用)。 :)
  • @Enrichman 我认为这不会导致崩溃,但我认为我不妨建议将其删除。谢谢
【解决方案2】:

问题是您从 doInBackground() 方法调用 finish(),因此您正在以某种方式修改您的上下文并获得异常。

从结果中返回整数而不是字符串,然后在 onPostExecute() 中完成()您的活动。

  class GetKey extends AsyncTask<String, String, Integer> {
    /**
     * Before starting background thread Show Progress Dialog
     * */

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(AddWorkOutPage.this);
        pDialog.setMessage("loading...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    /**
     * Creating product
     * */
    protected Integer doInBackground(String... args) {
        JSONParser jsonParser = new JSONParser();
        JSONObject json = null;
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();

        params.add(new BasicNameValuePair("exercise", exercise));

        json = jsonParser.makeHttpRequest(url_get_key,"GET", params);

        try {
            base64EncodedPublicKey = json.getString("key");
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // check for success tag
        int success = 0;
        try {
            success = json.getInt(TAG_SUCCESS);                
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return success;
    }

    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(Integer success) {
        pDialog.dismiss();
        if (success == 1) {
           // successfully created product
           //Log.d("ok", json.toString());
           // closing this screen
           finish();
        } else {
           // failed to create product
        }
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多