我希望适配器更改微调器上的textview并更改主活动的背景imageview。如果我可以制作一个适配器来接收具有这两种品质的物体(宠物),但我不确定如何。 pet对象有一个字符串用于pet的名称和url用于downloadImage asyncTask我现在的问题是我得到一个空指针异常。构造函数也让我感到困惑。我只是不认为适配器正在做它应该做的事情。这就是我现在这样做的方式:
public class CustomAdapter extends ArrayAdapter<String> {
//for layouts
LayoutInflater myInflater;
private ArrayList<String> list;
private ArrayList<String> names;
//
public static class ViewHolder {
ImageView img1;
TextView tv1;
String url;
Bitmap bitmap;
}
//generate data we use
// private final MainActivity mainActivity;
String tempUrl;
public CustomAdapter(Context context, int textViewResourceId, ArrayList<String> names, ArrayList<String> pets) {
super(context, textViewResourceId, pets);
myInflater = ((Activity) context).getLayoutInflater();
this.names = names;
list = pets;
tempUrl = "http://tetonsoftware.com/pets/";
}
@Override
public int getCount() {
return list.size();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = myInflater.inflate(R.layout.spinner_item, null);
holder = new ViewHolder();
holder.img1 = (ImageView) convertView.findViewById(R.id.imageView);
holder.tv1 = (TextView) convertView.findViewById(R.id.textview);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
//set the text
holder.tv1.setText(names.get(position));
holder.url = tempUrl.concat(list.get(position));
//start a thread to download image
new DownloadImageTask(holder).execute(holder.url);
//new ImageDownloader().execute(holder);
return convertView;
}
}
public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
private static final String TAG = "DownloadImageTask";
private static final int DEFAULTBUFFERSIZE = 50;
private static final int NODATA = -1;
private final CustomAdapter.ViewHolder myVH;
private String url;
private ImageView imageView;
private Drawable placeholder;
public DownloadImageTask(CustomAdapter.ViewHolder myVH) {
this.myVH = myVH;
this.url = myVH.url;
}
@Override
protected Bitmap doInBackground(String... params) {
// site we want to connect to
String url = params[0];
// note streams are left willy-nilly here because it declutters the
// example
try {
URL url1 = new URL(url);
// this does no network IO
HttpURLConnection connection = (HttpURLConnection) url1.openConnection();
// can further configure connection before getting data
// cannot do this after connected
// connection.setRequestMethod("GET");
connection.setReadTimeout(500);
connection.setConnectTimeout(500);
// this opens a connection, then sends GET & headers
connection.connect();
// lets see what we got make sure its one of
// the 200 codes (there can be 100 of them
// http_status / 100 != 2 does integer div any 200 code will = 2
int statusCode = connection.getResponseCode();
if (statusCode / 100 != 2) {
return null;
}
// get our streams, a more concise implementation is
// BufferedInputStream bis = new
// BufferedInputStream(connection.getInputStream());
InputStream is = connection.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
// the following buffer will grow as needed
ByteArrayOutputStream baf = new ByteArrayOutputStream(DEFAULTBUFFERSIZE);
//ByteArrayBuffer baf = new ByteArrayBuffer(DEFAULTBUFFERSIZE);
int current = 0;
// wrap in finally so that stream bis is sure to close
try {
while ((current = bis.read()) != NODATA) {
baf.write((byte) current);
}
// convert to a bitmap
byte[] imageData = baf.toByteArray();
return BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
} finally {
// close resource no matter what exception occurs
bis.close();
}
} catch (IOException exc) {
return null;
}
}
@Override
protected void onPostExecute(Bitmap result) {
Log.d(TAG, "Result for " + url);
if (result != null) {
if (url == this.myVH.url) {
Log.d(TAG, "SUCCESS for " + url);
this.myVH.img1.setImageBitmap(result);
}
else
Log.d(TAG, "FAIL url != this.myVH.url for " + url);
} else
Log.d(TAG, "NULL for " + url);
}
}
这是错误消息:
04-02 19:55:37.252 10941-10941 / com.example.arya.project3 E / AndroidRuntime:FATAL EXCEPTION:main 处理:com.example.arya.project3,PID:10941 java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' at com.example.arya.project3.DownloadImageTask.onPostExecute(DownloadImageTask.java:106) 在com.example.arya.project3.DownloadImageTask.onPostExecute(DownloadImageTask.java:17) 在android.os.AsyncTask.finish(AsyncTask.java:636) 在android.os.AsyncTask.access $ 500(AsyncTask.java:177) 在android.os.AsyncTask $ InternalHandler.handleMessage(AsyncTask.java:653) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:135) 在android.app.ActivityThread.main(ActivityThread.java:5468) at java.lang.reflect.Method.invoke(Native Method) 在java.lang.reflect.Method.invoke(Method.java:372) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:976) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771) 04-02 19:55:37.253 10941-10941 / com.example.arya.project3 I / AndroidRuntime:向activityManagerService报告FATAL 04-02 19:55:37.364 10941-10941 / com.example.arya.project3 I / AndroidRuntime:完成向activityManagerService报告FATAL 04-02 19:55:37.364 10941-10941 / com.example.arya.project3 I / Process:发送信号。 PID:10941 SIG:9