Java 使用AsyncTask在主活动的单独类中获取控件

Java 使用AsyncTask在主活动的单独类中获取控件,java,android,xml,android-asynctask,Java,Android,Xml,Android Asynctask,我需要使用控件来更改布局中的值。但是通过该类的上下文参数,它没有找到在xml中声明的控件,这些控件对于所做的每个转换都返回null 我尝试恢复此数据的类扩展了异步任务: public class WSCatalogo91 extends AsyncTask<Void, ProgressAux, Boolean> private ProgressBar mProgressBar; private TextView mProgressText; public WSCatalogo9

我需要使用控件来更改布局中的值。但是通过该类的上下文参数,它没有找到在xml中声明的控件,这些控件对于所做的每个转换都返回null

我尝试恢复此数据的类扩展了异步任务:

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();
    }
公共类WSCatalogo91扩展异步任务
私人ProgressBar mProgressBar;
私有文本查看mProgressText;
公共WSCatalogo91(WSCatalogo91Listener侦听器)引发SQLException{
this.listener=listener;
dh=新数据库助手((活动)侦听器);
confDao=newconfiguradao(dh.getConnectionSource());
pDao=新的ProdutoDao(dh.getConnectionSource());
mProgressBar=(ProgressBar)((活动)侦听器).findViewById(R.id.ProgressBar);
mProgressText=(TextView)((活动)侦听器).findViewById(R.id.progressBarTextView);
auxProgresso=newprogressaux();
}
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:

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 {
            ...
        }
    }
公共类WSCatalogo91扩展异步任务{
公共WSCatalogo91(WSCatalogo91Listener侦听器)引发SQLException{
this.listener=listener;
dh=新数据库助手((活动)侦听器);
confDao=newconfiguradao(dh.getConnectionSource());
pDao=新的ProdutoDao(dh.getConnectionSource());
mProgressBar=(ProgressBar)((活动)侦听器).findViewById(R.id.ProgressBar);
mProgressText=(TextView)((活动)侦听器).findViewById(R.id.progressBarTextView);
auxProgresso=newprogressaux();
}
私有位图baixarImagens(字符串urlpuble、字符串tDownload、int totalImages){
...
}
@凌驾
受保护的void onProgressUpdate(ProgressAux…参数){
if(参数[0].getTexto()!=null)
mProgressText.setText(参数[0].getTexto());
setProgressBar(参数[0].getProgresso());
}
私有字符串SaveImagesIntenternalStorage(位图位图位图、字符串imgName){
...
}
@凌驾
受保护的void onPreExecute(){
...
}
私有void saveChanges(){
...
}
@凌驾
受保护的布尔doInBackground(无效…路径){
...
}
私有AtualizarJSON反序列化咨询ATUALIZACAO(字符串resultadoJSON)抛出JSONException{
...
}
私有列表反序列化协商(类tipo,字符串resultadoJSON)抛出JSONException{
...
}
@凌驾
受保护的void onPostExecute(布尔结果){
...
}
公共接口WSCatalogo91Listener{
...
}
}

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

我知道您所说的,但我需要同样地操作onProgressUpdate(),但需要在构造函数中获取要操作的字段,因为在这个过程中会使用多次,并进行多次更改。我这样做是为了在下载进度条和文本视图反馈上进行更新。所以不要每次输入onProgressUpdate()时都重做搜索和强制转换。。。没有办法做我正在做的事情,并且必须使用此表单?是的,您应该能够做一些类似于您所编码的事情,但这会在您的界面和您的活动之间造成不必要的紧密耦合(否定界面的好处)。在生命周期和布局膨胀方面,也会存在潜在的竞争条件,这会使问题变得复杂。请给出解决方案,我是Android新手,我认为保持类之间的分离很重要,但如果没有其他方法,请告诉我如何做。请参阅上文:使用您现有的侦听器实现(回调接口
活动
实现)来操作UI,而不是使用此强制转换方法。