Fyruz
Fyruz

Reputation: 97

Dialog TextView in AsyncTask always null

i've got a Dialog in my AsyncTask to show the loading of some images. In the DIalog there is a TextView that need to be updated for every image that get loaded but it always return a NullPointerException.

Can somebody help me please?

Here's the code of the AsyncTask class:

private class addCartRealms extends AsyncTask<Void, Integer, Void> {

        AlertDialog.Builder mBuilder = new AlertDialog.Builder(mainMenu.this);
        AlertDialog dialog;
        View mView = getLayoutInflater().inflate(R.layout.download_loading_dialog, null);
        TextView numberP = findViewById(R.id.loading_number);
        ProgressBar downloadProgress = findViewById(R.id.download_progress_bar);

        @Override
        protected Void doInBackground(Void... params) {

            final OkHttpClient client = new OkHttpClient();
            Realm myRealm = Realm.getDefaultInstance();

            for (int i = 0; i < downloadedCardList.size(); i++) {

                publishProgress(i);

                myRealm.beginTransaction();
                //ADDING CARDS TO REALM
                Card card = myRealm.createObject(Card.class);
                card.setDankness(downloadedCardList.get(i).getDankness());
                card.setExpansion(downloadedCardList.get(i).getExpansion());
                card.setId(downloadedCardList.get(i).getId());
                card.setImgpath(downloadedCardList.get(i).getImgpath());
                card.setLitness(downloadedCardList.get(i).getLitness());
                card.setName(downloadedCardList.get(i).getName());
                card.setRarity(downloadedCardList.get(i).getRarity());
                card.setValue(downloadedCardList.get(i).getValue());

                Request request = new Request.Builder()
                        .url(downloadedCardList.get(i).getImgpath())
                        .build();

                Response response = null;
                Bitmap image = null;
                try {
                    response = client.newCall(request).execute();

                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (response.isSuccessful()) {

                    image = BitmapFactory.decodeStream(response.body().byteStream());
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    image.compress(Bitmap.CompressFormat.PNG, 100, stream);
                    byte[] byteArray = stream.toByteArray();
                    card.setCardImage(byteArray);

                }

                myRealm.commitTransaction();

                //cardImgUrl.add(new cardImagesAndLink(downloadedCardList.get(i).getImgpath(), downloadedCardList.get(i).getId()));

            }


            return null;
        }
        @Override
        protected void onPostExecute(Void result) {

            dialog.dismiss();
            Toast.makeText(mainMenu.this, "Download completato, scaricate " + downloadedCardList.size() + " carte.", Toast.LENGTH_LONG).show();

        }

        @Override
        protected void onPreExecute() {

            mBuilder.setView(mView);
            dialog = mBuilder.create();
            dialog.show();

        }

        @Override
        protected void onProgressUpdate(Integer... values) {

            numberP.setText(values[0] + " / "  + downloadedCardList.size()); // HERE IS THE ERROR

        }



    }

And the error is this one:

Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

Upvotes: 0

Views: 45

Answers (2)

codeFreak
codeFreak

Reputation: 353

Utilize the dialogs findViewById like this:

TextView numberP = mView.findViewById(R.id.loading_number);
ProgressBar downloadProgress = mView.findViewById(R.id.download_progress_bar);

Upvotes: 1

pleft
pleft

Reputation: 7905

Instead of

TextView numberP = findViewById(R.id.loading_number);

You should use the view of you dialog

TextView numberP = mView.findViewById(R.id.loading_number);

And probably do the same for the ProgressBar view if it is included in the dialogs layout

Upvotes: 1

Related Questions