【发布时间】:2013-05-24 02:30:57
【问题描述】:
我对 Android 和 AsyncTask 很陌生,希望您能提供帮助...我正在尝试刷新 ImageView 适配器,以便它在 UI 上更新...有人告诉我使用 notifyDataSetChanged(),但我只能得到它可以工作...我已经设置了一个异步任务,但我得到的唯一结果是 NullPointerException...我需要在将新元素添加到我的 nextColorArray 时进行更新em>..请看我的代码,我真的不知道我是否以正确的方式使用我的异步任务,或者我什至关闭了?!..cheers
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;
}
}
Logcat
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)
【问题讨论】:
-
发布您的 logcat,以便我们查看您在哪里获得
NPE。但看起来你甚至不需要AsyncTask。您可以创建一个函数来完成所有这些工作 -
E/AndroidRuntime: 致命异常: android.view.ViewConfiguration.get(ViewConfiguration.java:332) 上的主要 java.lang.NullPointerException 在 android.view.View.
(View.java :3254) 在 android.widget.ImageView. (ImageView.java:105) -
public int getCount() { int a = 10; return a; }真的吗? -
public int getCount() { return aL.size(); } public Object getItem(int position) { return aL.get(position); } public long getItemId(int position) { /** You can use the array index as a unique id. */ return position; } -
@AlejandroColorado 你是对的。我测试了。你会得到 ArrayIndexOutofBoundException。需要按照您的建议进行更改。
标签: android android-asynctask android-adapter