简体   繁体   English

从不同的活动调用AsyncTask

[英]Call AsyncTask From Different Activities

Consider the code in mainActivity, we use AsyncTask like this 考虑mainActivity中的代码,我们像这样使用AsyncTask

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: 在第二个活动中,我们必须显示所选项目的完整数据,并且要从Internet检索数据,我们必须使用以下代码行:

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 UPDATE这里是我的日志猫,导致这些代码行不返回任何数据,应用崩溃会抛出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. 使用Events来跟踪第一个AsyncTask然后触发第二个调用。
  • 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; 您将需要创建一个接受索引参数的event类;
  • You can also try calling your AsyncTask on a SERIAL_EXECUTOR_THREAD ; 您也可以尝试在SERIAL_EXECUTOR_THREAD上调用AsyncTask

Just a small effort on this code should save you the headache; 只需花一点时间就可以使您免于头痛。

EventBus is a simple library for Android you can find here . EventBus是一个简单的Android库,您可以在这里找到。

Good luck! 祝好运!

Use IntentService + Event Bus (such as Otto ) 使用IntentService +事件总线(例如Otto

You start the IntentService from any activity and it does your calculations in background. 您可以从任何活动启动IntentService,并且它会在后台进行计算。 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). 问题出在您的DetailsPage活动onCreate方法(第51行)上。 check it 核实

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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