In the GetInboxMails class, I change the messages variable and then in the FragmentInbox class, it already becomes null.

public class FragmentInbox extends Fragment { private ArrayList<Message> messages; GetInboxMails getInboxMails = new GetInboxMails(); getInboxMails.execute(); try { Log.i("EmailClient", "В listView.setAdapter будет передана переменная message, равная: " + messages); //////здесь messages = null listView.setAdapter(new MailItemAdapter(getContext(), R.layout.show_email_list, R.id.mail_list, messages)); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //showSingleEmail(getContext(), messages.get(position)); } }); } catch (Exception e) { Log.e("EmailClient", "Error in listView.setAdapter ", e); } return view; } public class GetInboxMails extends AsyncTask<Object, String, Boolean> { @Override protected void onPreExecute() { WaitingDialog = ProgressDialog.show(context, "Reading data", "Fetching mails...", true); } @Override protected void onPostExecute(Boolean result) { WaitingDialog.dismiss(); Toast.makeText(context, "To fetching is complite!!!", Toast.LENGTH_LONG).show(); } @Override protected Boolean doInBackground(Object... params) { try { Mail inbox = new Mail(user, password, host, port, auth); messages = new ArrayList<Message>(); Message[] arrayMessage; arrayMessage = inbox.getMessages("INBOX"); for (int i = 10; i > 0; i--) messages.add(arrayMessage[i]); Log.i("EmailClient", "Отработал метод doInBackground с переменными: " + user + " " + password + " " + host + " " + port + " " + auth + " " + messages); } catch (Exception e) { Log.e("EmailClient", "Error fetching email in function doInBackground! ", e); } return null; } } 

}

  • one
    An interesting story, although the code and a bit much. And what is your question? - VladD
  • @VladD when changing the variable messages in the class GetInboxMails then in the class FragmentInbox it is already null. Checked by logs! - Dzmitry
  • @VlaD; in the FragmentInbox class, I cannot use extends AsyncTask <Object, String, Boolean>, because already using extends Fragment. Therefore, it was decided to resort to the creation of a folded class. - Dzmitry
  • one
    1) The question should be placed in the question itself (edit it), no one reads the comments anyway. 2) Are you sure that all this code is needed to illustrate the problem? The more code, the less chance of an answer, the more a good answer. - VladD
  • @VladD, edited - Dzmitry

1 answer 1

You do not understand how AsyncTask works and multithreading in general. The fact that you called the execute() method does not mean that AsyncTask fully worked. This means only that the task is in the queue, and when it works out to the end is not known. For such situations, there is an onPostExecute method in onPostExecute , which is called guaranteed after the doInBackground() method, plus it is called in the UI thread. Therefore, it is necessary to perform any actions with messages in this method. Those. The code should be like this.

 public class FragmentInbox extends Fragment { private ArrayList<Message> messages; public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState { GetInboxMails getInboxMails = new GetInboxMails(); getInboxMails.execute(); // на этом этапе messages еще не заполнена //остально код return view; } } public class GetInboxMails extends AsyncTask<Object, String, Boolean> { @Override protected void onPostExecute(Boolean result) { //здесь doInBackground уже отработал, messages заполнена listView.setAdapter(new MailItemAdapter(getContext(), R.layout.show_email_list, R.id.mail_list, messages)); //остальной код } @Override protected Boolean doInBackground(Object... params) { // какой то код } } }