从不同的活动中调用AsyncTask(Call AsyncTask From Different Activities)

编程入门 行业动态 更新时间:2024-10-28 11:22:50
从不同的活动中调用AsyncTask(Call AsyncTask From Different Activities)

考虑mainActivity中的代码,我们像这样使用AsyncTask

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

获取结果并将其显示在用户列表中后,用户单击一个项目,然后迁移到另一个活动。 在第二个Activity中,我们必须显示所选项目的完整数据,并从Internet检索数据,我们必须使用以下代码行:

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

但问题是这些行不起作用:/甚至应用程序崩溃或不显示数据....我该如何解决这个问题?

UPDATE这里是我的日志cat导致那些代码行没有产生任何数据,应用程序崩溃抛出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) 

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) 

最满意答案

看来您的实际问题是缺少良好的代码设计:

一些想法

使用Events跟踪第一个AsyncTask然后触发第二个调用。 要知道要进行哪个调用,请传递每次递增的索引; 您需要创建一个带索引参数的event类; 您也可以尝试在SERIAL_EXECUTOR_THREAD上调用AsyncTask ;

只需在这段代码上付出一点努力就可以省去头痛;

EventBus是一个简单的Android库,你可以在这里找到。

祝你好运!

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!

更多推荐

本文发布于:2023-07-28 22:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1309767.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:活动中   AsyncTask   Activities   Call

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!