AsyncTask-da catch () funktsiyasi tomonidan xatolik yuzaga kelsa, boshqa faoliyatga o'ting

Bu mening AsyncTask:

public class GetDataTask extends AsyncTask {

        @Override
        protected void onPreExecute() {
           //[...]
        }

        @Override
        protected void onPostExecute(JSONArray result) {

            int result_length = 0;

            try {
                result_length = result.length();
            } catch (NullPointerException e) {
                e.printStackTrace();

               //Redirect the user back to the original activity
                Intent intent = new Intent(ResultActivity.this, MainActivity.class);
                startActivity(intent);
            }


            for (int i = 0; i < result_length; i++) {
               //[...]
            }
        }

        @Override
        protected JSONArray doInBackground(Void... params) {
           //[...]
        }
}

result null bo'lsa, .length() bir NullPointerExceptionni otadi. Shu sababli, urlni MainActivity-ga qayta yo'naltirishni xohlayman.

try {
    result_length = result.length();
} catch (NullPointerException e) {
    e.printStackTrace();

   //Redirect the user back to the original activity
    Intent intent = new Intent(ResultActivity.this, MainActivity.class);
    startActivity(intent);

The problem is that it doesn't work. I get e.printStackTrace() but the activity doesn't change. It's like Intent is not "fast" enough to change activity, and the code below (for (int i = 0; i < result_length; i++) {}) still keeps running.
What can I do? Is my solution wrong? Thank you in advance.

0
O'ylaymanki, async vazifasi - bu ish zarrachadir, shuning uchun for loopi istisnoga duch kelganidan so'ng ham amalga oshiriladi. Shunday qilib, xatolik yuzaga kelsa (masalan, catch blokida) AsyncTask ni bekor qilishingiz kerak.
qo'shib qo'ydi muallif Kaushal28, manba
O'ylaymanki, async vazifasi - bu ish zarrachadir, shuning uchun for loopi istisnoga duch kelganidan so'ng ham amalga oshiriladi. Shunday qilib, xatolik yuzaga kelsa (masalan, catch blokida) AsyncTask ni bekor qilishingiz kerak.
qo'shib qo'ydi muallif Kaushal28, manba

6 javoblar

Niyat nishonni

faoliyatini o'zgartirish uchun "tez" emas

Agar startActivity() orqali faoliyatni boshlasangiz, siz bu amalni zudlik bilan yumshatmaysiz. Siz uchun belgilangan faoliyatni boshlash uchun sizdan faqat xayr-ehson qilyapsiz. Va bu startActivity() dan so'ng quyidagi kodni chaqirmasligini anglatmaydi. Ijrochi kodni ijro etish muddati tugagunga qadar davom ettiradi. Keyinchalik bajarishni to'xtatish uchun startActivity() dan so'ng darhol return kerak.

0
qo'shib qo'ydi

Niyat nishonni

faoliyatini o'zgartirish uchun "tez" emas

Agar startActivity() orqali faoliyatni boshlasangiz, siz bu amalni zudlik bilan yumshatmaysiz. Siz uchun belgilangan faoliyatni boshlash uchun sizdan faqat xayr-ehson qilyapsiz. Va bu startActivity() dan so'ng quyidagi kodni chaqirmasligini anglatmaydi. Ijrochi kodni ijro etish muddati tugagunga qadar davom ettiradi. Keyinchalik bajarishni to'xtatish uchun startActivity() dan so'ng darhol return kerak.

0
qo'shib qo'ydi

Shaxsan catch (NullPointerException) har doim noto'g'ri. Bundan tashqari, if (variable == null) ham tezroq bo'lishi mumkin.

AsyncTask should just return; or stop, not necessarily start anything.

Boshqa taklif: doInBackground null o'rniga bo'sh JSONArray qaytarishini tekshiring.

and the code below (for (int i = 0; i < result_length; i++) {}) still keeps running

Albatta, chunki for loop try blokida emas ...

try {
    result_length = result.length();
} catch (NullPointerException e) {
    e.printStackTrace();

   //Redirect the user back to the original activity
    Intent intent = new Intent(ResultActivity.this, MainActivity.class);
    startActivity(intent);
}

// The result is still null here...

Osonroq tekshiruv bilan hamma narsalarni almashtiring va faqat pastadir kiritilishiga yo'l qo'ymang.

@Override
protected void onPostExecute(JSONArray result) {

    int result_length = result == null ? 0 : result.length();

    for (int i = 0; i < result_length; i++) {
       //This loop won't run when the result is null... 
    }

Agar chindan ham istasangiz, if (result_length == 0) ni tekshiring, lekin yana bir bor, natijalar tugagach, faoliyat qayta chaqirilishi kerak va keyin boshqa faoliyat.

Ref: How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class?

0
qo'shib qo'ydi

Shaxsan catch (NullPointerException) har doim noto'g'ri. Bundan tashqari, if (variable == null) ham tezroq bo'lishi mumkin.

AsyncTask should just return; or stop, not necessarily start anything.

Boshqa taklif: doInBackground null o'rniga bo'sh JSONArray qaytarishini tekshiring.

and the code below (for (int i = 0; i < result_length; i++) {}) still keeps running

Albatta, chunki for loop try blokida emas ...

try {
    result_length = result.length();
} catch (NullPointerException e) {
    e.printStackTrace();

   //Redirect the user back to the original activity
    Intent intent = new Intent(ResultActivity.this, MainActivity.class);
    startActivity(intent);
}

// The result is still null here...

Osonroq tekshiruv bilan hamma narsalarni almashtiring va faqat pastadir kiritilishiga yo'l qo'ymang.

@Override
protected void onPostExecute(JSONArray result) {

    int result_length = result == null ? 0 : result.length();

    for (int i = 0; i < result_length; i++) {
       //This loop won't run when the result is null... 
    }

Agar chindan ham istasangiz, if (result_length == 0) ni tekshiring, lekin yana bir bor, natijalar tugagach, faoliyat qayta chaqirilishi kerak va keyin boshqa faoliyat.

Ref: How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class?

0
qo'shib qo'ydi

AsyncTask kontstruktorida Faoliyat ma'lumotnomasini taqdim eting.

private Activity activity;

public GetDataTask (Activity activity) {
   this.activity = activity;
}

siz uni shunday chaqira olasiz

activity.startActivity(new Intent(activity, MainActivity.class));
0
qo'shib qo'ydi
Bu xotiraning bekor qilinishini kutmoqda ...
qo'shib qo'ydi muallif cricket_007, manba

AsyncTask kontstruktorida Faoliyat ma'lumotnomasini taqdim eting.

private Activity activity;

public GetDataTask (Activity activity) {
   this.activity = activity;
}

siz uni shunday chaqira olasiz

activity.startActivity(new Intent(activity, MainActivity.class));
0
qo'shib qo'ydi
Bu xotiraning bekor qilinishini kutmoqda ...
qo'shib qo'ydi muallif cricket_007, manba