简体   繁体   中英

Call AsyncTask From Different Activities

Consider the code in mainActivity, we use AsyncTask like this

new SearchTask(SearchPage.this).execute("1", query);

after getting result and show them in a list to user, user clicks one item, and migrate to another Activity. In the second Activity we have to show full data of selected item, and to retrieve data from internet we have to use these lines of code:

new SearchTask(DetailsPage.this).execute("2", id);
new SearchTask(DetailsPage.this).execute("3", id);

but the problem is these lines won't work :/ and even app crashes or not showing data.... how do I solve this?

UPDATE here is my log cat cause those lines of code not produce any data back, app crashes throw NPE

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.shahroozevsky.myfirstapp/com.shahroozevsky.myfirstapp.DetailsPage}: java.lang.NullPointerException
                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
                                                                              at android.app.ActivityThread.access$600(ActivityThread.java:141)
                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                              at android.os.Looper.loop(Looper.java:137)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:5041)
                                                                              at java.lang.reflect.Method.invokeNative(Native Method)
                                                                              at java.lang.reflect.Method.invoke(Method.java:511)
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
                                                                              at dalvik.system.NativeStart.main(Native Method)
                                                                           Caused by: java.lang.NullPointerException
                                                                              at com.shahroozevsky.myfirstapp.DetailsPage.onCreate(DetailsPage.java:51)
                                                                              at android.app.Activity.performCreate(Activity.java:5104)
                                                                              at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
                                                                              at android.app.ActivityThread.access$600(ActivityThread.java:141) 
                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
                                                                              at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                              at android.os.Looper.loop(Looper.java:137) 
                                                                              at android.app.ActivityThread.main(ActivityThread.java:5041) 
                                                                              at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                              at java.lang.reflect.Method.invoke(Method.java:511) 
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
                                                                              at dalvik.system.NativeStart.main(Native Method) 

It appears that your actual problem is lack of good code design here:

Some Ideas

  • Use Events to keep track of the first AsyncTask then trigger the second call.
  • To know which call to make, pass around an index that you increments each time;
  • You will need to create an event class that takes an argument for index;
  • You can also try calling your AsyncTask on a SERIAL_EXECUTOR_THREAD ;

Just a small effort on this code should save you the headache;

EventBus is a simple library for Android you can find here .

Good luck!

Use IntentService + Event Bus (such as Otto )

You start the IntentService from any activity and it does your calculations in background. After it finishes you send an event containing the results using the event bus.

The problem is in your DetailsPage activity onCreate method (Line 51). check it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM