我需要获取控件来更改布局中的值。但是通过类的context参数,它找不到在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为null,但我的上下文不为null,因此它正在排队使用。遵循声明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 {
...
}
}
答案 0 :(得分:1)
您的AsyncTask
不应直接操纵UI组件,尤其是在onPostExecute
或onProgressUpdate
回调之外。由于您已经在使用Activity
实现的回调侦听器接口,因此让Activity
在回调时操纵它的UI组件会更简单。请确保涵盖生命周期案例,因为AsyncTask
不了解Activity
生命周期,需要适当取消。