java - 使用 AsyncTask 在主 Activity 的单独类中获取控件

标签 java android xml android-asynctask

我需要获取控件来更改布局中的值。但是通过类的上下文参数,它没有找到在 xml 中声明的控件,每次转换都返回 null。

我尝试恢复此数据的类扩展了 AsyncTask:

public class WSCatalogo91 extends AsyncTask<Void, ProgressAux, Boolean> 

private ProgressBar mProgressBar;
private TextView mProgressText;

public WSCatalogo91(WSCatalogo91Listener listener) throws SQLException {
        this.listener = listener;
        dh = new DatabaseHelper((Activity) listener);
        confDao = new ConfiguracaoDao(dh.getConnectionSource());
        pDao = new ProdutoDao(dh.getConnectionSource());

        mProgressBar = (ProgressBar) ((Activity) listener).findViewById(R.id.progressBar);
        mProgressText = (TextView) ((Activity) listener).findViewById(R.id.progressBarTextView);
        auxProgresso = new ProgressAux();
    }

mProgressBar 和 mProgressText 为空,但我的上下文不为空,因此它可以完美地用于队列中。遵循声明 id 的 xml 代码。

<TextView android:text="@string/progressBar" android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:id="@+id/progressBarTextView"
        android:maxWidth="400dp"
        android:maxHeight="400dp"/>

    <ProgressBar
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar"
        android:indeterminate="false"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="165dp"
        android:minWidth="300dp" />

这表明有一个progressBar和progressBarTextView: This shows that there is a progressBar nor progressBarTextView

WSCatalogo91 类:

  public class WSCatalogo91 extends AsyncTask<Void, ProgressAux, Boolean> {

      public WSCatalogo91(WSCatalogo91Listener listener) throws SQLException {
            this.listener = listener;
        dh = new DatabaseHelper((Activity) listener);
        confDao = new ConfiguracaoDao(dh.getConnectionSource());
        pDao = new ProdutoDao(dh.getConnectionSource());

        mProgressBar = (ProgressBar) ((Activity)listener).findViewById(R.id.progressBar);
        mProgressText = (TextView) ((Activity) listener).findViewById(R.id.progressBarTextView);
        auxProgresso = new ProgressAux();
        }

        private Bitmap baixarImagens(String urlComplement, String tDownload, int totalImages) {
            ...
        }

        @Override
        protected void onProgressUpdate(ProgressAux... params) {
            if(params[0].getTexto() != null)
            mProgressText.setText(params[0].getTexto());

            mProgressBar.setProgress(params[0].getProgresso());
        }

        private String saveImagesIntoInternalStorage(Bitmap bitmapImage, String imgName) {
            ...
        }

        @Override
        protected void onPreExecute() {
            ...
        }

        private void saveChanges() {
            ...
        }

        @Override
        protected Boolean doInBackground(Void... paths) {
            ...
        }

        private AtualizarJSON DeserializaConsultaAtualizacao(String resultadoJSON) throws JSONException {
           ...
        }

        private <T> List<T> DeserializaConsulta(Class<T[]> tipo, String resultadoJSON) throws JSONException {
            ...
        }

        @Override
        protected void onPostExecute(Boolean result) {
           ...
        }

        public interface WSCatalogo91Listener {
            ...
        }
    }

最佳答案

您的 AsyncTask 不应直接操作 UI 组件,尤其是在 onPostExecuteonProgressUpdate 回调之外。由于您已经在使用 Activity 实现的回调监听器接口(interface),因此让 Activity 在回调时操作它的 UI 组件会更简单。确保涵盖生命周期案例,因为 AsyncTask 不知道 Activity 生命周期,需要适当取消。

关于java - 使用 AsyncTask 在主 Activity 的单独类中获取控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27236088/

相关文章:

java - wsimport 生成的客户端如何工作?

javascript - 是否可以从后退按钮事件调用ons-sliding-menu组件的closeMenu方法?

php - 使用PHP和Xpath解析XML时无法访问子节点

java - 如何在 Java 中从 XML 创建 PDF?

java - 为什么 Java 似乎不尊重我的类路径?

java - 如何判断 Proguard 是否完成了它的工作

android - 在 Firemonkey 中获取 Android 设备和操作系统信息

java - Camel 的重新投递政策

java - 大小为 0 的 CursorIndexOutOfBoundsException : Index 0 requested,

java - 无法保存我的分数 - Android