ASyncTask上的Android应用程序退出

时间:2014-06-17 11:16:34

标签: android android-asynctask exit

我有一个应用程序在asynktask完成时关闭。

asynctask的代码是

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

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }

    @Override
    protected String doInBackground(String... args) {
        // TODO Auto-generated method stub
        // Check for success tag
        int success;

        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();

        // ID HOTELU DO ZMIANY
        params.add(new BasicNameValuePair("hId", "3"));

        // Posting user data to script
        JSONObject json = jsonParser.makeHttpRequest(DESC_URL, "POST",
                params);
        try {
            // json success element
            success = json.getInt(TAG_SUCCESS);
            if (success == 1) {
                Log.d("Desc Downloaded!", json.toString());
                finish();
                // return json.getString(TAG_MESSAGE);
                OpisHotelu = json.getString(TAG_MESSAGE);

            } else {
                Log.d("Download Failure!", json.getString(TAG_MESSAGE));
                // return json.getString(TAG_MESSAGE);

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;

    }

    protected void onPostExecute(String file_url) {
        if (file_url != null) {
            Log.d("opis_hotelu", OpisHotelu);
            tvOpisHotelu.setText(OpisHotelu);

        }
    }
}

我的活动只有OnCreate和这个asynk ......

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_screen);

    // Buttons
    btnViewProducts = (Button) findViewById(R.id.btnViewProducts);

    // view products click event
    btnViewProducts.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            // Launching All products Activity
            Intent i = new Intent(getApplicationContext(),
                    AllProductsActivity.class);
            startActivity(i);

        }
    });

    tvOpisHotelu = (TextView) findViewById(R.id.textView1);

    ttt = (Button) findViewById(R.id.tttt);
    ttt.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            new PobieranieOpisuHotelu().execute();

        }
    });

}

我叫新的PobieranieOpisuHotelu()。execute();从一个按钮完成后,我可以看到LogCat中没有错误,但应用程序已关闭。

06-17 13:13:39.430: D/request!(2265): starting
06-17 13:13:39.610: D/dalvikvm(2265): GC_FOR_MALLOC freed 4457 objects / 205112 bytes in 55ms
06-17 13:13:41.730: D/Login attempt(2265): {"message":"Logowanie zakończone pomyślnie!","success":1}
06-17 13:13:41.730: D/Login Successful!(2265): {"message":"Logowanie zakończone pomyślnie!","success":1}
06-17 13:13:45.130: D/Desc Downloaded!(2265): {"message":"testowy opis hotelu aasddwqrqw","success":1}

1 个答案:

答案 0 :(得分:0)

从这些行中删除finish():

if (success == 1) {
            Log.d("Desc Downloaded!", json.toString());
            finish();
            // return json.getString(TAG_MESSAGE);
            OpisHotelu = json.getString(TAG_MESSAGE);

        }

没有错误,因为您的代码是以inteneded方式工作的......