我需要获取控件来更改布局中的值。但是通过类的上下文参数,它没有找到在 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:
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 组件,尤其是在 onPostExecute
或 onProgressUpdate
回调之外。由于您已经在使用 Activity
实现的回调监听器接口(interface),因此让 Activity
在回调时操作它的 UI 组件会更简单。确保涵盖生命周期案例,因为 AsyncTask
不知道 Activity
生命周期,需要适当取消。
关于java - 使用 AsyncTask 在主 Activity 的单独类中获取控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27236088/