после this нити, я попытался сделать строку переменного состояния с этим кодом:андроид: установить видимость из AsyncTask
private int[] loadingElementIDs;
private void initLoadingBar() {
final DisplayMetrics displayMetrics=getResources().getDisplayMetrics();
final float screenWidthInDp = displayMetrics.widthPixels/displayMetrics.density;
final int elementAmount = (int) (Math.floor(screenWidthInDp * 0.5f/30) * 5);
//set margins
LinearLayout container = (LinearLayout)findViewById(R.id.loading_outer);
...
container.requestLayout();
//declare length
loadingElementIDs = new int[elementAmount];
LayoutParams LLParams = new LayoutParams(0, LayoutParams.MATCH_PARENT);
LLParams.weight = 1f;
LinearLayout element;
for (int i=0; i<elementAmount; i++) {
int id = generateViewId(); //creates unique id
element = new LinearLayout(this);
element.setVisibility(View.INVISIBLE);
element.setLayoutParams(LLParams);
element.setBackgroundDrawable(getResources().getDrawable(R.drawable.loading_inner));
element.setId(id);
element.requestLayout();
container.addView(element);
loadingElementIDs[i] = id;
}
}
это работает хорошо для меня, но теперь я хочу, чтобы вычислить н с AsyncTask и сделать код видимым (код в пределах моего класса деятельности):
private class PrefetchData extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@SuppressWarnings("static-access")
@Override
protected Void doInBackground(Void... arg0) {
try {
int step = 0;
float totalSteps = 100f;
while (...) {
step++;
// ...................
//show status
setLoadingStatus(step/totalSteps);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
finish();
}
}
public void setLoadingStatus(float percentage) {
int max = (int) Math.min(Math.floor(percentage * loadingElementIDs.length),
for (int d=0; d<max; d++) {
((LinearLayout)findViewById(loadingElementIDs[d])).setVisibility(View.VISIBLE);
LinearLayout el = (LinearLayout)this.findViewById(loadingElementIDs[d]);
el.setVisibility(LinearLayout.VISIBLE);
}
}
И это не работает. если я звоню setLoadingStatus(20f);
из onCreate, он работает отлично, но не в AsyncTask. Конечно, я начинаю initLoadingBar();
и new PrefetchData().execute();
в действиях onCreate().
Знаете ли вы, что я делаю неправильно?
Не может вызывать функции пользовательского интерфейса от doInBackground(), это отдельный поток (то есть всей цель AsnycTasks). Вам нужно сделать это в onProgressUpdate() или onPostExecute() – zgc7009