【问题标题】:AsyncTask to update adapter UIAsyncTask 更新适配器 UI
【发布时间】: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


【解决方案1】:

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

1. mContextImageAdapter 中为空,因为您使用 ImageAdapter 的单参数构造函数来创建对象。所以将 Activity Context 也传递为:

ia = new ImageAdapter(GameScreen.this,nextColorArray);

2.使用

gridview2 = (GridView)GameScreen.this. findViewById(R.id.colorNext);

用于在onPreExecute中初始化GridView

编辑:我在代码中进行了这些更改。

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

快照

【讨论】:

  • 上下文是我刚刚执行代码的一个问题,我也得到了 arrayindexoutofbound 异常。我仍然不确定发布代码。我要编辑您的帖子并发布代码吗??
  • 自从您先回答后,我已经编辑了您的答案。我更改了 gridview 的类名和 id,因为我在我的 xml 中对它进行了不同的定义。由于 asynctask 是内部类,因此我使用了活动上下文。我还附上了快照。 +1
  • 为回来干杯,非常感谢@Raghunandan...我没有问题显示适配器...那部分没问题...当我向 nextColorArray 添加元素时,我需要 UI刷新并显示数组中的新元素...我可以从我的异步任务中获得这个吗???
  • @ronanc 您可以通过让 onPosexecute() 中的按钮单击侦听器将颜色添加到列表并在适配器上调用 notifyDataSetChanged() 来做到这一点。参考我之前的回答@stackoverflow.com/questions/16652812/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-02
  • 2021-04-08
  • 2016-04-13
  • 2021-04-04
  • 2017-08-02
  • 1970-01-01
相关资源
最近更新 更多