我得到一个NullPointerException,当我试图从数据库中获取一些图像时,我认为我的getter和setter方法是错误的,非常感谢任何帮助。
这是我的活动
public class ViewMoreDetailsImages extends Activity{
ProgressBar progressbar_01;
ListView lv_images;
ImageAdapter imageAdapter;
List<ImageInformation> imageInformationList = new ArrayList<ImageInformation>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_more_details_images);
this.setTitle("LandDev Images");
lv_images = (ListView)findViewById(R.id.lv_images);
progressbar_01 = (ProgressBar) findViewById(R.id.progress_bar_01);
displayImage();
imageAdapter = new ImageAdapter(this, imageInformationList);
lv_images.setAdapter(imageAdapter);
}
private class viewMoreImages extends AsyncTask<String, Void, List<String>>{
Statement st;
ResultSet rs;
Connection con;
byte[] img;
Bitmap theImage;
@Override
protected void onPreExecute(){
lv_images.setVisibility(View.GONE);
progressbar_01.setVisibility(View.VISIBLE);
super.onPreExecute();
}
@Override
protected List<String> doInBackground(String... params) {
List<String> result = new ArrayList<String>();
try{
String query_image = "SELECT Image FROM tbl_task_image WHERE Task_Id ='"+ ListOfTask.task_id +"'";
con = MyConnection.connect();
st = con.createStatement();
rs = st.executeQuery(query_image);
while(rs.next()){
img = rs.getBytes(1);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
options.inPurgeable = true;// is deprecated
theImage = Bitmap.createScaledBitmap(BitmapFactory.decodeByteArray(img, 0, img.length, options), 200, 200, true);
ByteArrayOutputStream buf = new ByteArrayOutputStream();
theImage.compress(Bitmap.CompressFormat.PNG, 0, buf);
ImageInformation imageInformation = new ImageInformation(theImage);
imageInformationList.add(imageInformation);
}
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
@Override
protected void onPostExecute(List<String> result){
lv_images.setVisibility(View.VISIBLE);
progressbar_01.setVisibility(View.GONE);
super.onPostExecute(result);
}
}
public void displayImage() {
viewMoreImages vmi = new viewMoreImages();
vmi.execute();
}
}
这是我的构造函数:
public class ImageInformation {
private Bitmap img;
public ImageInformation(Bitmap img){
this.img = img;
}
public Bitmap getImg() {
return img;
}
public void setImg(Bitmap img) {
this.img = img;
}
这是我的适配器:
public class ImageAdapter extends ArrayAdapter<ImageInformation> {
List<ImageInformation> imageInformationList;
public ImageAdapter(Context context, List<ImageInformation> imageInformationList) {
super(context, R.layout.more_details_images, imageInformationList);
}
static class ImageHolder{
ImageView more_details_images;
}
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
ImageHolder holder = null;
View v = convertView;
if(v == null) {
LayoutInflater inflater = LayoutInflater.from(getContext());
v = inflater.inflate(R.layout.more_details_images, parent, false);
holder = new ImageHolder();
holder.more_details_images = (ImageView) v.findViewById(R.id.images);
v.setTag(holder);
}else{
holder = (ImageHolder) v.getTag();
}
holder.more_details_images.setImageBitmap(imageInformationList.get(position).getImg()); // causing NullPointerException
holder.more_details_images.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((ListView) parent).performItemClick(v, position,0);
}
});
return v;
}
}
这是我的logcat
6-22 21:43:04.012 18024-18024/com.juanito.itdepartment.landdevtaskmonitoring E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.juanito.itdepartment.landdevtaskmonitoring, PID: 18024
java.lang.NullPointerException
at com.juanito.itdepartment.landdevtaskmonitoring.ImageAdapter.getView(ImageAdapter.java:45)
at android.widget.AbsListView.obtainView(AbsListView.java:2263)
at android.widget.ListView.makeAndAddView(ListView.java:1790)
at android.widget.ListView.fillDown(ListView.java:691)
at android.widget.ListView.fillFromTop(ListView.java:752)
at android.widget.ListView.layoutChildren(ListView.java:1630)
at android.widget.AbsListView.onLayout(AbsListView.java:2091)
at android.view.View.layout(View.java:14817)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1660)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1436)
at android.view.View.layout(View.java:14817)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:14817)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:374)
at android.view.View.layout(View.java:14817)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:14817)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1987)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1744)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
看起来您只需要在imageInformationList
类的构造函数中设置ImageAdapter
的本地引用。
由于您从未将其设置为传递给构造函数的引用,因此当您在imageInformationList.get(position).getImg()
内调用getView()
时,它仍为null。
要修复它,只需将本地引用分配给传递给构造函数的引用(并重命名参数,以便在同一范围内没有多个同名变量)。
public class ImageAdapter extends ArrayAdapter<ImageInformation> {
List<ImageInformation> imageInformationList;
//rename parameter name:
public ImageAdapter(Context context, List<ImageInformation> imageInformationListParameter) {
super(context, R.layout.more_details_images, imageInformationListParameter);
imageInformationList = imageInformationListParameter; //added
}
//.................