AsyncTask nullPointerException pressing return button

This is my situation. I have this AsyncTask:

private class Logo extends AsyncTask<Void, Void, Void> { @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected Void doInBackground(Void... params) { try { // Connect to the web site Document document = Jsoup.connect(BLOG_URL).get(); // Using Elements to get the class data // Locate the src attribute for(Element img : document.select("div.col-1-1 .image img[src]")) { String ImgSrc = img.attr("src"); // Download image from URL InputStream is = new java.net.URL(ImgSrc).openStream(); //add Bitmap to an array bitmap.add(BitmapFactory.decodeStream(is)); } } catch (IOException e) { e.printStackTrace(); Log.e("ESEMPIO", "ERRORE NEL PARSING DELLE IMMAGINI"); } return null; } @Override protected void onPostExecute(Void result) { ParsingAdapterCategorie adapter = new ParsingAdapterCategorie(getActivity(), titoli, bitmap, data); lista.setAdapter(adapter); } } 

If I click the back button, a log error will fail, for example:

 FATAL EXCEPTION: main java.lang.NullPointerException at android.widget.ArrayAdapter.init(ArrayAdapter.java:310) at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:166) 

This is because the result of the activity is zero, I think. What can I do?

+1
source share
5 answers

Basically, you should put the onPostExecute () code inside if (getActivity ()! = Null) ... to prevent things like screen rotation.

BUT, where do you start executing AsyncTask ()? You must respect the life cycle of the fragment ... for example, you should call it Fragment.onActivityCreated () OR Fragment.onResume () .. NOT on Fragment.onCreate () , because at this point the activity is not yet set ... therefore getActivity () will always be NULL .

+2
source

Discard it in onDestroy

 @Override public void onDestroy() { if (asynchtask!=null) { asynchtask.cancel(true); } super.onDestroy(); } 
0
source

Instead of using AsyncTask, you may need to use IntentService

Take it out

http://developer.android.com/training/run-background-service/create-service.html

0
source

AsyncTask runs in the background while the action is complete. So getActivity () returns null. You should modify your code as follows and pass the context in the constructor of your AsyncTask.

 private class Logo extends AsyncTask<Void, Void, Void> { private Context context; public Logo(Context context) { this.context = context; } @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected Void doInBackground(Void... params) { try { // Connect to the web site Document document = Jsoup.connect(BLOG_URL).get(); // Using Elements to get the class data // Locate the src attribute for(Element img : document.select("div.col-1-1 .image img[src]")) { String ImgSrc = img.attr("src"); // Download image from URL InputStream is = new java.net.URL(ImgSrc).openStream(); //add Bitmap to an array bitmap.add(BitmapFactory.decodeStream(is)); } } catch (IOException e) { e.printStackTrace(); Log.e("ESEMPIO", "ERRORE NEL PARSING DELLE IMMAGINI"); } return null; } @Override protected void onPostExecute(Void result) { ParsingAdapterCategorie adapter = new ParsingAdapterCategorie(context, titoli, bitmap, data); lista.setAdapter(adapter); } } 

Then you can call your AsyncTask in Activity as follows:

  Logo logo = new Logo(this); logo.execute(); 

Hope this solved your problem !; -)

Sincerely.

0
source

It will help you ...

if (null! = getActivity) {// Insert ur code

}

-1
source

Source: https://habr.com/ru/post/1485059/


All Articles