简体   繁体   English

我的片段中的 ASYNCTASK 有问题

[英]I am having trouble with ASYNCTASK in my fragment

I'm new to fragments.I am trying to call search API in my fragment using AsyncTask.我是片段的新手。我正在尝试使用 AsyncTask 在我的片段中调用搜索 API。 Json Parsing is done in MainActivity, by the way I'm not in my main activity when I use this fragment, it's connected to another activity. Json 解析是在 MainActivity 中完成的,顺便说一下,当我使用这个片段时我不在我的主要活动中,它连接到另一个活动。 It works, but I'm having trouble passing the parsed info that I stored into a bundle to a new fragment.它有效,但我无法将存储到包中的解析信息传递给新片段。 To be specific it crashes the moment I try to open a new fragment.具体来说,它在我尝试打开一个新片段的那一刻崩溃。 I have the parsed information and I've set the arguments for my fragment, it's just the moment I use .commit() it crashes.我有解析的信息,我已经为我的片段设置了参数,就在我使用 .commit() 的那一刻它崩溃了。

It would be great if you could post the piece of your code so we can check what is the issue.如果您可以发布您的代码片段,以便我们检查问题所在,那就太好了。

Although I have deduced from your explanation that you might be saving the information in doInBackground which runs on the background thread.尽管我从您的解释中推断出您可能将信息保存在后台线程上运行的doInBackground中。 Instead, you could pass that info to the onPostExecute and save it into SharedPreferences since onPostExecute runs on the UI thread.相反,您可以将该信息传递给onPostExecute并将其保存到SharedPreferences因为onPostExecute在 UI 线程上运行。

Let me know if this works.让我知道这个是否奏效。

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

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