AsyncTask的Andr​​oid的进度条不显示[可能重复] [英] AsyncTask Android ProgressBar not showing [Possible Duplicate]

查看:407
本文介绍了AsyncTask的Andr​​oid的进度条不显示[可能重复]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先我想说的是,我知道有上S.O他们夫妇类似的问题,但不回答我...

首先让我告诉你我的code:

我有一个片段,当它加载它发送到服务器的请求,解析JSON服务器返回,然后返回解析的JSON作为对象。

我在初始化的方法onCreateView一个进度条:

 巴=(进度)mainView.findViewById(R.id.progressBar);

然后我创建AsyncTask的扩展类的一个实例:

  PolisotAsyncInfo任务=新PolisotAsyncInfo();

,然后调用execute方法,之后调用get函数:

  task.execute(新PolisotAsyncParameter(ClientMainActivity.mToken,ClientMainActivity.mUserID));
对象o = task.get();


  

我觉得这是我做错了,但不知道,我需要使用
  get()方法,因为我需要的数据,该doInBackround
  回报....


在这一点上,编译器进到上preExecute方法那里我设置栏可见

  bar.setVisibility(View.VISIBLE);

后的doInBackround方法运行,并完成所有种类的东西,是不相关的。

,然后onPostExecute方法被调用那里我设置栏GONE:

  bar.setVisibility(View.GONE);

任何帮助将是AP preciated家伙,我有点丢在这里...

谢谢!


解决方案

我相信这行是你的问题,因为你怀疑,

 对象o = task.get();

的get()块中的 UI线程。你有不同的选择,虽然根据您的具体需求和配置来获取数据。


  1. 您可以调用活动从 onPostExecute ()方法做对数据进行处理。如何你做到这一点取决于如果一个内部类与否。如果是则没有问题。如果它是一个独立的文件,那么你可以传递一个活动引用您的的AsyncTask 的构造,并用它来调用活动方法。 这些所谓的答案显示了如何做到这一点


  2. 您也可以使用接口时,你的的AsyncTask 已完成创建回调。 <一href=\"http://stackoverflow.com/questions/12575068/how-to-get-the-result-of-onpostexecute-to-main-activity-because-asynctask-is-a\">This SO回答盖这样


first of all I want to say that i know there a couple of similar question on S.O but non of them answered my question...

First of all let me show you my code:

I have a fragment that when it loads it sends a request to a server, parses the JSON the server returns and then returns the parsed JSON as an object.

I initialize a progress bar in the onCreateView method:

bar = (ProgressBar) mainView.findViewById(R.id.progressBar);

and then I create an instance of the Asynctask extended class:

PolisotAsyncInfo task = new PolisotAsyncInfo();

and then call the execute method and after that call the get function:"

task.execute(new PolisotAsyncParameter(ClientMainActivity.mToken,ClientMainActivity.mUserID));
Object o = task.get();

I think this is what i'm doing wrong but not sure, i need to use the get() method because i need the data that the doInBackround returns....

at this point the compiler goes to the onPreExecute method there i set the bar to visible:

bar.setVisibility(View.VISIBLE);

after the the doInBackround methods run and does all kind of stuff that is not relevant

and then the onPostExecute method is called there I set the bar to GONE:

bar.setVisibility(View.GONE);

Any help would be appreciated guys, I'm kind of lost here...

thanks!!

解决方案

I believe this line is your problem as you suspected,

Object o = task.get();

get() blocks the UI Thread. You have different options though to get the data depending on your exact needs and configuration.

  1. You can call an Activity method from onPostExecute() to do something with the data. How you do that depends on if its an inner class or not. If it is then there is no problem. If it is a separate file then you can pass an Activity reference to your AsyncTasks constructor and use that to call the Activity method. These SO answers show how to do this

  2. You can also use an interface to create a callback when your AsyncTask has finished. This SO answer covers doing this

这篇关于AsyncTask的Andr​​oid的进度条不显示[可能重复]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆