【问题标题】:Android update view from async task来自异步任务的 Android 更新视图
【发布时间】:2014-01-13 02:18:30
【问题描述】:

这是一个小的 XML 动画,当应用程序从服务器获取 JSON 时,我正在尝试启动它。获取所有数据大约需要 2 秒,所以我尝试展示一些加载动画。

我遇到的问题是图像不会改变,直到一切都完成并且 onPostExecute() 关闭。

我需要将视图传递给 AsyncTask 还是其他什么?

最后一件事 在此 AsyncTask 完成后。如果你再运行它,它就会顺利运行。

XML

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/loadingtext1" android:duration="100" />
<item android:drawable="@drawable/loadingtext2" android:duration="100" />
<item android:drawable="@drawable/loadingtext3" android:duration="100" />
<item android:drawable="@drawable/loadingtext4" android:duration="100" />
</animation-list>

代码

private class LOGMEIN extends AsyncTask<String, Integer, String> {

            @Override
            protected String doInBackground(String... params) {
                CRY.CInit(DI, DU, DP);
                publishProgress(0);
                while(data != "whatineed"){
                    try {
                        Thread.sleep(450);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                completecheck = 1;
                return "complete";
            }

            @Override
            protected void onPreExecute() {
                LOADINGTEXT.setBackgroundResource(R.drawable.animation);
                AnimationDrawable LT = (AnimationDrawable) LOADINGTEXT.getBackground();
                LT.start();
            }

            @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);
                if (result.contentEquals("complete") == true) {
                    domorework();
                } else {
                    finish();
                }
            }
}

【问题讨论】:

    标签: android animation asynchronous view android-asynctask


    【解决方案1】:

    您是否在 AsyncTask 中设置了 onPreExecute()?

    受保护的 void onPreExecute() {

    //在此处显示您的动画加载器...
    进度.show();

    }

    我在 onCreate() 中使用它:

    初始化:public ProgressDialog 进度;

    progress = new ProgressDialog(this);
    progress.setCancelable(true);
    progress.setInverseBackgroundForced(false);
    progress.setCanceledOnTouchOutside(true);
    progress.setMessage("Loading.....");
    

    【讨论】:

      【解决方案2】:

      我不知道这是否是唯一的问题,但您在您的loop 中比较您的Strings 错误。

      while (data != "whatineed")
      

      应该是

      while (!data.equals("whatineed"))
      

      甚至更好

      while (!"whatineed".equals(data))
      

      比较值而不是对象引用

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多