Android AsyncTask以更新适配器UI
我对Android和Android AsyncTask以更新适配器UI,android,android-asynctask,android-adapter,Android,Android Asynctask,Android Adapter,我对Android和AsyncTask相当陌生,希望你能帮我…我正在尝试刷新ImageView适配器,以便它在UI上更新。。。我被告知要使用notifyDataSetChanged()但我只能让它工作…我设置了一个asynctask,但我得到的唯一结果是NullPointerException…我需要在我的NextColorary中添加新元素时进行更新…请查看我的代码,我真的不知道我是否以正确的方式使用了异步任务,或者我是否接近了?!。。干杯 public class GameScreen ex
AsyncTask
相当陌生,希望你能帮我…我正在尝试刷新ImageView适配器,以便它在UI上更新。。。我被告知要使用notifyDataSetChanged()
但我只能让它工作…我设置了一个asynctask,但我得到的唯一结果是NullPointerException…我需要在我的NextColorary中添加新元素时进行更新…请查看我的代码,我真的不知道我是否以正确的方式使用了异步任务,或者我是否接近了?!。。干杯
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;
}
}
公共类游戏屏幕扩展活动{
int黄色=0xFFFF66;
int绿色=0xff00EE76;
int red=0xffff4342;
int蓝色=0xff42c3ff;
紫色整数=0xff9932CC;
int白色=0xFFFFFF;
整数总计数=0;
int colorPosition=0;//下一个颜色位置
int colorPickerStart=0;
ArrayListNextColorary;
图像视图图像视图;
图像适配器;
私有静态最终字符串标记=GameScreen.class.getSimpleName();//日志
ArrayListcolorPicker=新的ArrayList();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.game_屏幕);
新建upDateNextColor().execute();
}
类upDateNextColor扩展了AsyncTask{
GridView GridView 2;
@凌驾
受保护的空位背景(空位…空位){
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
nextColorary.add(蓝色);
nextColorary.add(绿色);
nextColorary.add(红色);
nextColorary.add(黄色);
添加(紫色);
收藏。洗牌(NextColorary);
}
@凌驾
受保护的void onPreExecute(){
NextColorary=新阵列列表(10);
gridview2=(GridView)findViewById(R.id.colorNext);
ia=新的图像适配器(NextColorray);
gridview2.setAdapter(ia);
如果(总计数>10){
添加(0,白色);
ia.notifyDataSetChanged();
}
}
}
公共类ImageAdapter扩展了BaseAdapter{
私有上下文;
私人ArrayList aL;
公共ImageAdapter(上下文c、ArrayListaL){
mContext=c;
this.aL=aL;
}
公共图像适配器(ArrayListaL){
this.aL=aL;
}
公共图像适配器(上下文c){
mContext=c;
}
public int getCount(){
返回10;
}
公共对象getItem(int位置){
返回null;
}
公共长getItemId(int位置){
返回0;
}
公共视图getView(int位置、视图转换视图、视图组父视图){
if(convertView==null){
imageView=新的imageView(mContext);
setLayoutParams(新的GridView.LayoutParams(50,50));
setBackgroundColor(nextcolorray.get(colorPosition));
if(colorPosition<9)colorPosition++;
}否则{
imageView=(imageView)convertView;
}
返回图像视图;
}
}
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)
E/AndroidRuntime:致命异常:main java.lang.NullPointerException
在android.view.ViewConfiguration.get(ViewConfiguration.java:332)
在android.view.view.(view.java:3254)
在android.widget.ImageView上
您将获得空指针异常,因为:
1.mContext
在ImageAdapter
中为空,因为您正在使用ImageAdapter的单参数构造函数来创建对象。因此将活动上下文也传递为:
ia = new ImageAdapter(GameScreen.this,nextColorArray);
2.使用
gridview2 = (GridView)GameScreen.this. findViewById(R.id.colorNext);
用于在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;
}
}
}
公共类MainActivity扩展活动{
int黄色=0xFFFF66;
int绿色=0xff00EE76;
int red=0xffff4342;
int蓝色=0xff42c3ff;
紫色整数=0xff9932CC;
int白色=0xFFFFFF;
整数总计数=0;
int colorPosition=0;//下一个颜色位置
int colorPickerStart=0;
ArrayListNextColorary;
图像视图图像视图;
图像适配器;
私有静态最终字符串标记=MainActivity.class.getSimpleName();//日志
ArrayListcolorPicker=新的ArrayList();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
新建upDateNextColor().execute();
}
类upDateNextColor扩展了AsyncTask{
GridView GridView 2;
@凌驾
受保护的空位背景(空位…空位){
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
nextColorary.add(蓝色);
nextColorary.add(绿色);
nextColorary.add(红色);
nextColorary.add(黄色);
添加(紫色);
收藏。洗牌(NextColorary);
}
@凌驾
受保护的void onPreExecute(){
NextColorary=新阵列列表(10);
gridview2=(GridView)findViewById(R.id.GridView);
ia=新的图像适配器(NextColorray);
gridview2.setAdapter(ia);
添加(0,白色);
ia.notifyDataSetChanged();
}
}
公共类ImageAdapter扩展了BaseAdapter{
私有上下文;
私人ArrayList aL;
公共ImageAdapter(上下文c、ArrayListaL){
mContext=c;
this.aL=aL;
}
公共图像适配器(ArrayListaL){
this.aL=aL;
}
公共图像适配器(上下文c){
mContext=c;
}
public int getCount(){
//INTA=10;
返回aL.size();
}
公共对象getItem(int位置){
返回null;
}
公共长getItemId(int位置){
返回0;
}
公共视图getView(int位置、视图转换视图、视图组父视图){
if(convertView==null){
imageView=新的imageView(MainActivity.this);
setLayoutParams(新的GridView.LayoutParams(50,50));
setBackgroundColor(nextcolorray.get(position));
if(colorPosition<9)colorPosition++;
}否则{
imageView=(imageView)convertView;
}
返回图像视图;
}
}
}
快照
post y