我对Android和AsyncTask
都很陌生,希望你能帮忙...我试图刷新一个ImageView适配器,以便在UI上更新......我被告知要使用notifyDataSetChanged()
但是我只是可以让它工作...我已经设置了一个asynctask但我得到的唯一结果是 NullPointerException ...我需要在将新元素添加到我的 nextColorArray ....请看我的代码,我甚至不知道我是以正确的方式使用我的asynctask还是即使我关闭了?!..欢呼
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)
答案 0 :(得分:2)
你得到Null Pointer Exception,因为:
1。 mContext
在ImageAdapter
中为空,因为您正在使用ImageAdapter的单参数构造函数来创建对象。所以将Activity Context也传递给:
ia = new ImageAdapter(GameScreen.this,nextColorArray);
2。使用
gridview2 = (GridView)GameScreen.this. findViewById(R.id.colorNext);
用于初始化GridView
onPreExecute
编辑:我在代码中进行了这些更改。
return aL.size(); // change this in getCount()
imageView.setBackgroundColor(nextColorArray.get(position));
FullCode
public class MainActivity 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 = MainActivity.class.getSimpleName();//log
ArrayList<Integer>colorPicker = new ArrayList<Integer>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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.gridview);
ia = new ImageAdapter(nextColorArray);
gridview2.setAdapter(ia);
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() {
//int a = 10;
return aL.size();
}
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(MainActivity.this);
imageView.setLayoutParams(new GridView.LayoutParams(50, 50));
imageView.setBackgroundColor(nextColorArray.get(position));
if(colorPosition < 9) colorPosition++;
} else {
imageView = (ImageView) convertView;
}
return imageView;
}
}
}
快照