Reputation: 67
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)
Upvotes: 0
Views: 73
Reputation: 527
The problem is in your DetailsPage
activity onCreate
method (Line 51). check it
Upvotes: 0
Reputation: 48262
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.
Upvotes: 0
Reputation: 5626
It appears that your actual problem is lack of good code design here:
Some Ideas
Events
to keep track of the first AsyncTask
then trigger the second call.event
class that takes an argument for index;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!
Upvotes: 1