我对 Android 很陌生,AsyncTask
希望您能提供帮助...我正在尝试刷新 ImageView 适配器,以便在 UI 上更新...我被告知要使用notifyDataSetChanged()
,但我只能让它工作...我已经设置一个异步任务,但我得到的唯一结果是NullPointerException ...我需要在将新元素添加到我的nextColorArray时进行更新....请查看我的代码,我什至不知道我是否在使用我的异步任务正确的方式,或者如果我什至接近?!..干杯
public class GameScreen extends Activity{
int yellow = 0xffffff66;
int green = 0xff00EE76;
int red = 0xffff4342;
int blue = 0xff42c3ff;
int purple = 0xff9932CC;
int white = 0xFFFFFFFF;
int total_Count = 0;
int colorPosition = 0;//nextColor position
int colorPickerStart = 0;
ArrayList<Integer>nextColorArray;
ImageView imageView;
ImageAdapter ia;
private static final String TAG = GameScreen.class.getSimpleName();//log
ArrayList<Integer>colorPicker = new ArrayList<Integer>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game_screen);
new upDateNextColor().execute();
}
class upDateNextColor extends AsyncTask<Void, Void, Void> {
GridView gridview2;
@Override
protected Void doInBackground(Void... voids) {
return null;
}
@Override
protected void onPostExecute(Void result) {
nextColorArray.add(blue);
nextColorArray.add(green);
nextColorArray.add(red);
nextColorArray.add(yellow);
nextColorArray.add(purple);
Collections.shuffle(nextColorArray);
}
@Override
protected void onPreExecute() {
nextColorArray = new ArrayList<Integer>(10);
gridview2 = (GridView) findViewById(R.id.colorNext);
ia = new ImageAdapter(nextColorArray);
gridview2.setAdapter(ia);
if(total_count > 10){
nextColorArray.add(0, white);
ia.notifyDataSetChanged();
}
}
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
private ArrayList aL;
public ImageAdapter(Context c, ArrayList<Integer>aL) {
mContext = c;
this.aL = aL;
}
public ImageAdapter(ArrayList<Integer>aL) {
this.aL = aL;
}
public ImageAdapter(Context c){
mContext = c;
}
public int getCount() {
return 10;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(50, 50));
imageView.setBackgroundColor(nextColorArray.get(colorPosition));
if(colorPosition < 9) colorPosition++;
} else {
imageView = (ImageView) convertView;
}
return imageView;
}
}
日志猫
E/AndroidRuntime: FATAL EXCEPTION: main java.lang.NullPointerException
at android.view.ViewConfiguration.get(ViewConfiguration.java:332)
at android.view.View.<init>(View.java:3254)
at android.widget.ImageView.<init>(ImageView.java:105)