而AsyncTask的运行黑屏 [英] Black screen while AsyncTask is running

查看:157
本文介绍了而AsyncTask的运行黑屏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来发问这里,所以有关的任何建议将AP preciated ...但我的问题:

I'm new to asking questions here so any advice about that will be appreciated...but to my problem:

我试图切换从一个活动到另一个,在那里我将展示来自服务器的一些数据。我拉着通过的AsyncTask从服务器中的数据,但有活动之间显示黑屏,当下载更长。我已经尝试过把从的onCreate()来调用onStart(在AsyncTask的),但没有改变。

I'm trying to switch from one activity to another, where I'll show some data from the server. I'm pulling the data from server via AsyncTask but there is black screen showing between the activities, when the download is longer. I already tried to put the AsyncTask from onCreate() to onStart() but no change there.

新活动:

public class SalesMenu extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setContentView(R.layout.sales_menu);        
    setProgressBarIndeterminateVisibility(true);
    super.onCreate(savedInstanceState);
}

@Override
protected void onStart() {
    List<HashMap<String, Object>> result = null;
    AsyncTask<Void, Boolean, List<HashMap<String, Object>>> aTask = new AsyncSearchRead(
            "sale.order", 
            new Object[0], 
            new String[] {"name"}, 
            this).execute();

    try {
        result = aTask.get();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }
    Log.d("test", (result!=null ? result.toString() : "nothing"));
    super.onStart();
}

}

我的AsyncTask:

My AsyncTask:

public class AsyncSearchRead extends AsyncTask<Void, Boolean, List<HashMap<String, Object>>> {
String mModel; 
boolean mCount; 
Integer mOffset;
Integer mLimit; 
String mOrder; 
boolean mReverseOrder; 
Object[] mConditions;
String[] mFields;
Activity parentActivity;

public AsyncSearchRead(String model, boolean count, Integer offset, Integer limit, 
                    String order, boolean reverseOrder, Object[] conditions, String[] fields,
                    Activity activity) {
    mModel = model;
    mCount = count;
    mOffset = offset;
    mLimit = limit;
    mOrder = order;
    mReverseOrder = reverseOrder;
    mConditions = conditions;
    mFields = fields;
    parentActivity = activity;
}

@Override
protected List<HashMap<String, Object>> doInBackground(Void... params) {
    Long[] ids;
    List<HashMap<String, Object>> results;
            //Downloading the data
    ids = SetServer.connection.search(mModel, mCount, mOffset, mLimit, mOrder, mReverseOrder, mConditions);
    results = SetServer.connection.read(mModel, ids, mFields);

    return results;
}

@Override
protected void onPostExecute(List<HashMap<String, Object>> result) {
    parentActivity.setProgressBarIndeterminateVisibility(false);
}

}

当我没有把知名度关闭onPostExecute它一直显示活动后旋转,但它装载后的才会显示。

When I did not turn visibility off onPostExecute it kept spinning after showing the activity but it's only shown after the loading.

我怎样才能得到它显示了活动,并只启动加载数据并显示一些进度条/加载器?在

How can I get it to show the activity and only after that start to load the data and show some progress bar/loader?

感谢您的帮助。

推荐答案

这是因为你使用的<$c$c>.get(). 获得()基本上使一个 的AsyncTask 同步(使主线程等待,直到它完成),所以它基本上就像根本不运行的AsyncTask

It's because you're using .get(). .get() essentially makes an AsyncTask synchronous (makes the main thread wait until it's done), so it's basically like not running the AsyncTask at all.

我的建议如下:


  1. 列表&LT;&HashMap的LT;弦乐,对象&gt;&GT;结果= NULL; 是一个成员变量

  2. 删除整个的try / catch 语句(带有获得()在里面),以及来电登录。*

  3. 将您的的AsyncTask 是一个内部类的 SalesMenu

  4. this.result =结果 onPostExecute(),以及任何其他处理你需要做的就可以了。

  1. Move List<HashMap<String, Object>> result = null; to be a member variable.
  2. Remove the whole try/catch statement (the one with .get() in it), and the call to Log.*.
  3. Move your AsyncTask to be an inner class of SalesMenu.
  4. Do this.result = result in onPostExecute(), as well as whatever other processing you need to do on it.

这篇关于而AsyncTask的运行黑屏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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