我正在尝试从url获取图像,并通过listviewadapter类在listview中进行设置。我在NULLPOINTEREXCEPTION
处获得openConnection()
在最后的getBitmapFromUrl方法中。
这是我的代码:
ListViewAdapter.java
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class ListViewAdapter extends BaseAdapter {
public String msgType[] = new String[8];
public String msgData[] = new String[8];
public String msgTimeStamp[] = new String[8];
Bitmap[] myBitmap = new Bitmap[8];
public Activity context;
int positionX;
URL urlX;
public LayoutInflater inflater;
public ListViewAdapter(Activity context, String[] type, String[] data,
String[] timestamp) {
super();
this.context = context;
this.msgType = type;
this.msgData = data;
this.msgTimeStamp = timestamp;
this.inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return msgData.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public static class ViewHolder {
ImageView imgView;
TextView txtType;
TextView txtData;
TextView txtTimestamp;
Bitmap myBitmap;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.list_item, null);
holder.imgView = (ImageView) convertView.findViewById(R.id.image);
holder.txtType = (TextView) convertView.findViewById(R.id.type);
holder.txtData = (TextView) convertView.findViewById(R.id.data);
holder.txtTimestamp = (TextView) convertView
.findViewById(R.id.timestamp);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
// holder.imgView.setImageBitmap(myBitmap[position]);
holder.txtType.setText(msgType[position]);
holder.txtTimestamp.setText(msgTimeStamp[position]);
if (msgType[position].equals("0"))
holder.txtData.setText(msgData[position]);
else {
holder.txtData.setText("");
URL url = null;
try {
url = new URL(msgData[position]);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
urlX=url;
positionX=position;
try {
Thread t = new Thread(new Runnable() {
public void run(){
myBitmap[positionX] = getBitmapFromUrl(urlX);
}
});
t.start();
t.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
holder.imgView.setImageBitmap(myBitmap[position]);
}
return convertView;
}
public Bitmap getBitmapFromUrl(URL url1) {
Bitmap Bitmap = null;
try {
HttpURLConnection connection = (HttpURLConnection) url1
.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap = BitmapFactory.decodeStream(input);
} catch (IOException e) {
e.printStackTrace();
}
return Bitmap;
}
}
编辑:
Thread t = new Thread(new Runnable() {
public void run(){
myBitmap[positionX] = getBitmapFromUrl(urlX);
}
});
t.start();
try {
t.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
LOGCAT(编辑后):
04-03 12:01:26.860: E/AndroidRuntime(11944): FATAL EXCEPTION: Thread-1155
04-03 12:01:26.860: E/AndroidRuntime(11944): java.lang.NullPointerException
04-03 12:01:26.860: E/AndroidRuntime(11944): at com.appquest.flatcassign.ListViewAdapter.getBitmapFromUrl(ListViewAdapter.java:127)
04-03 12:01:26.860: E/AndroidRuntime(11944): at com.appquest.flatcassign.ListViewAdapter$1.run(ListViewAdapter.java:107)
04-03 12:01:26.860: E/AndroidRuntime(11944): at java.lang.Thread.run(Thread.java:856)
最佳答案
嗨,我更新了您的基本适配器类,并看到了毕加索库实现的打击,您只需要复制和替换代码,一定会对您有所帮助。不要忘记添加jar文件。
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class ListViewAdapter extends BaseAdapter {
public String msgType[] = new String[8];
public String msgData[] = new String[8];
public String msgTimeStamp[] = new String[8];
public Activity context;
public LayoutInflater inflater;
public ListViewAdapter(Activity context, String[] type, String[] data,
String[] timestamp) {
super();
this.context = context;
this.msgType = type;
this.msgData = data;
this.msgTimeStamp = timestamp;
this.inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return msgData.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public static class ViewHolder {
ImageView imgView;
TextView txtType;
TextView txtData;
TextView txtTimestamp;
Bitmap myBitmap;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.list_item, null);
holder.imgView = (ImageView) convertView.findViewById(R.id.image);
holder.txtType = (TextView) convertView.findViewById(R.id.type);
holder.txtData = (TextView) convertView.findViewById(R.id.data);
holder.txtTimestamp = (TextView) convertView
.findViewById(R.id.timestamp);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
// holder.imgView.setImageBitmap(myBitmap[position]);
holder.txtType.setText(msgType[position]);
holder.txtTimestamp.setText(msgTimeStamp[position]);
if (msgType[position].equals("0"))
holder.txtData.setText(msgData[position]);
else {
holder.txtData.setText("");
Picasso.with(context).load(msgData[position]).into(holder.imgView);
}
return convertView;
}
}
谢谢。