0

我对 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)
4

1 回答 1

2

你得到空指针异常,因为:

1. mContext为空,ImageAdapter 因为您使用 ImageAdapter 的单参数构造函数来创建对象。所以传递活动上下文也为:

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));

全码

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;
 }
}
}

快照

在此处输入图像描述

于 2013-05-23T17:56:04.103 回答