Re: [android-developers] Re: AsyncTask null exception for the activity reference in the doInBackground method while rotating.
Yes, that's the more usual pattern. You'll still likely need an activity reference to show progress and display results, but there are special methods in AsyncTask for that, which are automatically called on the UI thread - thus, you'll be avoiding multiple threads trying to access and modify the activity member variable (the original issue with your code). -- Kostya 27.10.2011 15:33, Bluemercury пишет: What about extending the async task and then implement the logic directly in the doInbackground method? -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -- Kostya Vasilyev -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
Re: [android-developers] Re: AsyncTask null exception for the activity reference in the doInBackground method while rotating.
Yes i have a indeterminate progressBar in the header, the spinner one, but im using broadcast to all the activities which have the spinner to show or hide it if there's a task running in the background : @Override protected void onPreExecute() { Logger.write(QuadrosMobileActivityTask , AsyncTask pre execution + Current Activity: +activity.getClass().getName(), Logger.INFO); Intent broadcast = new Intent(); broadcast.setAction(SHOW_PROGRESSBAR); mApp.sendBroadcast(broadcast); mApp.setProgressBarInitialState(ProgressBar.VISIBLE); } @Override protected Object doInBackground(Void... params) { *do{* * SystemClock.sleep(50); * * }while(activity==null);* * * * Logger.write(QuadrosMobileActivityTask , AsyncTask background initialized +Current Activity: +activity.getClass().getName(), Logger.INFO);* * return activity.doBackGroundRequest();//method implemented by all sub activities* } @Override protected void onPostExecute(Object result) { if(activity!=null){ Logger.write(QuadrosMobileActivityTask , AsyncTask post execution +Current Activity: +activity.getClass().getName(), Logger.INFO); //update results activity.updateResultsInUi(result); mApp.getAsyncTasks().remove(this); //check if there's more tasks in the collection if(mApp.getAsyncTasks().isEmpty()){ Intent broadcast = new Intent(); broadcast.setAction(HIDE_PROGRESSBAR); mApp.sendBroadcast(broadcast); mApp.setProgressBarInitialState(ProgressBar.GONE); } } } the bold text is the one that gives problems.but in the pre and post there's no problem having the activity reference... -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
Re: [android-developers] Re: AsyncTask null exception for the activity reference in the doInBackground method while rotating.
You still have access to a member variable (activity) inside code that runs on the non-UI (worker) thread. Either: - Do not reference the activity from doInBackground, moving the actual code here from activity.doBackgroundRequest - Or learn the basics the thread synchronization, so that your UI thread doesn't change the value of activity member variable while it's being accessed by the background thread. -- Kostya 2011/10/27 Bluemercury joao.ro...@gmail.com Yes i have a indeterminate progressBar in the header, the spinner one, but im using broadcast to all the activities which have the spinner to show or hide it if there's a task running in the background : @Override protected void onPreExecute() { Logger.write(QuadrosMobileActivityTask , AsyncTask pre execution + Current Activity: +activity.getClass().getName(), Logger.INFO); Intent broadcast = new Intent(); broadcast.setAction(SHOW_PROGRESSBAR); mApp.sendBroadcast(broadcast); mApp.setProgressBarInitialState(ProgressBar.VISIBLE); } @Override protected Object doInBackground(Void... params) { *do{* * SystemClock.sleep(50); * * }while(activity==null);* * * * Logger.write(QuadrosMobileActivityTask , AsyncTask background initialized +Current Activity: +activity.getClass().getName(), Logger.INFO);* * return activity.doBackGroundRequest();//method implemented by all sub activities* } @Override protected void onPostExecute(Object result) { if(activity!=null){ Logger.write(QuadrosMobileActivityTask , AsyncTask post execution +Current Activity: +activity.getClass().getName(), Logger.INFO); //update results activity.updateResultsInUi(result); mApp.getAsyncTasks().remove(this); //check if there's more tasks in the collection if(mApp.getAsyncTasks().isEmpty()){ Intent broadcast = new Intent(); broadcast.setAction(HIDE_PROGRESSBAR); mApp.sendBroadcast(broadcast); mApp.setProgressBarInitialState(ProgressBar.GONE); } } } the bold text is the one that gives problems.but in the pre and post there's no problem having the activity reference... -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
Re: [android-developers] Re: AsyncTask null exception for the activity reference in the doInBackground method while rotating.
Hi there! Yes i removed the code from activity.doBackgroundRequest to the extending Async Tasks doInBackground, i think it might solve this, BUT i will se the thread synchronization topic. Thanks for the attention. -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
Re: [android-developers] Re: AsyncTask null exception for the activity reference in the doInBackground method while rotating.
On Wed, Oct 26, 2011 at 9:22 AM, Streets Of Boston flyingdutc...@gmail.com wrote: First, i would suggest trying not to use an Activity reference in your doInBackground at all. Maybe you can just use a Context (activity.getApplicationContext()) instead. Or, get the data you need out of the activity in the constructor or onPreExecute(), so you can use the strings or numbers or whatever safely. -- Mark Murphy (a Commons Guy) http://commonsware.com | http://github.com/commonsguy http://commonsware.com/blog | http://twitter.com/commonsguy _Android Programming Tutorials_ Version 4.0 Available! -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
Re: [android-developers] Re: AsyncTask null exception for the activity reference in the doInBackground method while rotating.
This happens during orientation changes, correct? What you have is two threads accessing and modifying the same variable at the same time. Take some time to learn about multithreaded programming in Java: concurrent access and modification of a variable is one of the things that fall under this topic. One easy way to ensure correct access / modification is to use this helper class built into Java (and Android): http://developer.android.com/reference/java/util/concurrent/atomic/AtomicReference.html -- Kostya 26.10.2011 19:56, Bluemercury ?: yes, but how can i get the method from the activity that contains the logic to do in the background from the context, is this possible? im getting null exception for the activity after verifying if the activity is not null: if(activity!=null){ return activity.doBackGroundRequest(); }else{ Logger.write(QuadrosMobileActivityTask , AsyncTask finished while no Activity was attached., Logger.INFO); return null;} } -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -- Kostya Vasilyev -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
Re: [android-developers] Re: AsyncTask null exception for the activity reference in the doInBackground method while rotating.
On Wed, Oct 26, 2011 at 11:31 AM, Bluemercury joao.ro...@gmail.com wrote: Well the thing is, this Asynctask is binded to a parent activity, that the subactivities use when they want to do some background work. They each implement the method you see in the code i've posted, the activity.doBackGroundRequest(); I've actually had errors even after the line if(activity!=null)...so how can i synchronized the code... Move the logic out of the activities and into other objects that will remain stable during configuration changes, such as the AsyncTask itself. -- Mark Murphy (a Commons Guy) http://commonsware.com | http://github.com/commonsguy http://commonsware.com/blog | http://twitter.com/commonsguy Android Training in NYC: http://marakana.com/training/android/ -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
Re: [android-developers] Re: AsyncTask null exception for the activity reference in the doInBackground method while rotating.
How so? each activity launches its own async task with its own specific logicwhen you rotate you just detach the current activity from the async task, the activity gets destroyed, a new one is created and attached to the SAME async task -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en