空指针异常

时间:2013-12-13 09:13:02

标签: android android-listview nullpointerexception

我想显示来自网址的图片我使用了位图,但是图片没有显示但是得到空指针异常请帮助我。我在列表视图中显示文本正确显示但只显示图像

  public class CustomListViewAdapter extends ArrayAdapter<RowItem> {

    Context context;

    public CustomListViewAdapter(Context context, int resourceId,
            List<RowItem> items) {
        super(context, resourceId, items);
        this.context = context;
    }

    /*private view holder class*/
    private class ViewHolder {
        ImageView imageView;
       // TextView txtTitle;
        TextView name;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        RowItem rowItem = getItem(position);

        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
          convertView = mInflater.inflate(R.layout.list_item, null);
            holder = new ViewHolder();
            holder.name = (TextView) convertView.findViewById(R.id.name);
            /*holder.txtTitle = (TextView) convertView.findViewById(R.id.title);*/
            holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
            convertView.setTag(holder);
        } else
            holder = (ViewHolder) convertView.getTag();

        holder.name.setText(rowItem.getName());
     //   holder.txtTitle.setText(rowItem.getTitle());


        //holder.imageView.setImageResource(rowItem.getImageId());

        Bitmap bitMap=downloadImage(rowItem.getImageUrl());
        holder.imageView.setImageBitmap(bitMap);
//        holder.imageView.setVisibility(ImageView.VISIBLE);
        return convertView;
    }



    private Bitmap downloadImage(String url) {
        Bitmap bitmap = null;
        InputStream stream = null;
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        bmOptions.inSampleSize = 1;

        try {
            stream = getHttpConnection(url);
            bitmap = BitmapFactory.
                    decodeStream(stream, null, bmOptions);
            stream.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        return bitmap;
    }

    // Makes HttpURLConnection and returns InputStream
    private InputStream getHttpConnection(String urlString)
            throws IOException {
        InputStream stream = null;
        URL url = new URL(urlString);
        URLConnection connection = url.openConnection();

        try {
            HttpURLConnection httpConnection = (HttpURLConnection) connection;
            httpConnection.setRequestMethod("GET");
            httpConnection.connect();

            if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                stream = httpConnection.getInputStream();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return stream;
    }
}

记录猫

12-13 04:17:09.349: E/AndroidRuntime(3236): FATAL EXCEPTION: main
12-13 04:17:09.349: E/AndroidRuntime(3236): java.lang.NullPointerException
12-13 04:17:09.349: E/AndroidRuntime(3236):     at com.example.gdirectory.CustomListViewAdapter.downloadImage(CustomListViewAdapter.java:80)
12-13 04:17:09.349: E/AndroidRuntime(3236):     at com.example.gdirectory.CustomListViewAdapter.getView(CustomListViewAdapter.java:60)
12-13 04:17:09.349: E/AndroidRuntime(3236):     at android.widget.AbsListView.obtainView(AbsListView.java:2177)
12-13 04:17:09.349: E/AndroidRuntime(3236):     at android.widget.ListView.makeAndAddView(ListView.java:1840)
12-13 04:17:09.349: E/AndroidRuntime(3236):     at android.widget.ListView.fillDown(ListView.java:675)
12-13 04:17:09.349: E/AndroidRuntime(3236):     at android.widget.ListView.fillFromTop(ListView.java:736)
12-13 04:17:09.349: E/AndroidRuntime(3236):     at android.widget.ListView.layoutChildren(ListView.java:1655)
12-13 04:17:09.349: E/AndroidRuntime(3236):     at android.widget.AbsListView.onLayout(AbsListView.java:2012)
12-13 04:17:09.349: E/AndroidRuntime(3236):     at android.view.View.layout(View.java:14289)
12-13 04:17:09.349: E/AndroidRuntime(3236):     at android.view.ViewGroup.layout(ViewGroup.java:4562)
12-13 04:17:09.349: E/AndroidRuntime(3236):     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076)
12-13 04:17:09.349: E/AndroidRuntime(3236):     at android.view.View.layout(View.java:14289)
12-13 04:17:09.349: E/AndroidRuntime(3236):     at android.view.ViewGroup.layout(ViewGroup.java:4562)
12-13 04:17:09.349: E/AndroidRuntime(3236):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
12-13 04:17:09.349: E/AndroidRuntime(3236):     at android.view.View.layout(View.java:14289)
12-13 04:17:09.349: E/AndroidRuntime(3236):     at android.view.ViewGroup.layout(ViewGroup.java:4562)
12-13 04:17:09.349: E/AndroidRuntime(3236):     at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:349)
12-13 04:17:09.349: E/AndroidRuntime(3236):     at android.view.View.layout(View.java:14289)
12-13 04:17:09.349: E/AndroidRuntime(3236):     at android.view.ViewGroup.layout(ViewGroup.java:4562)
12-13 04:17:09.349: E/AndroidRuntime(3236):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
12-13 04:17:09.349: E/AndroidRuntime(3236):     at android.view.View.layout(View.java:14289)
12-13 04:17:09.349: E/AndroidRuntime(3236):     at android.view.ViewGroup.layout(ViewGroup.java:4562)
12-13 04:17:09.349: E/AndroidRuntime(3236):     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1976)
12-13 04:17:09.349: E/AndroidRuntime(3236):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1730)
12-13 04:17:09.349: E/AndroidRuntime(3236):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
12-13 04:17:09.349: E/AndroidRuntime(3236):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
12-13 04:17:09.349: E/AndroidRuntime(3236):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
12-13 04:17:09.349: E/AndroidRuntime(3236):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
12-13 04:17:09.349: E/AndroidRuntime(3236):     at android.view.Choreographer.doFrame(Choreographer.java:532)
12-13 04:17:09.349: E/AndroidRuntime(3236):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
12-13 04:17:09.349: E/AndroidRuntime(3236):     at android.os.Handler.handleCallback(Handler.java:730)
12-13 04:17:09.349: E/AndroidRuntime(3236):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-13 04:17:09.349: E/AndroidRuntime(3236):     at android.os.Looper.loop(Looper.java:137)
12-13 04:17:09.349: E/AndroidRuntime(3236):     at android.app.ActivityThread.main(ActivityThread.java:5103)
12-13 04:17:09.349: E/AndroidRuntime(3236):     at java.lang.reflect.Method.invokeNative(Native Method)
12-13 04:17:09.349: E/AndroidRuntime(3236):     at java.lang.reflect.Method.invoke(Method.java:525)
12-13 04:17:09.349: E/AndroidRuntime(3236):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-13 04:17:09.349: E/AndroidRuntime(3236):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-13 04:17:09.349: E/AndroidRuntime(3236):     at dalvik.system.NativeStart.main(Native Method)

4 个答案:

答案 0 :(得分:0)

可能我们的URL无效,getHttpConnection返回null。然后,当您尝试关闭InputStreem时,您会收到NullPointerException。

您应该发布在getHttpConnection methot上打印的错误日志。那我们就可以说出错了。

答案 1 :(得分:0)

请将其更改为

public class CustomListViewAdapter extends ArrayAdapter<RowItem> {

    Context context;
    private List<RowItem> items;
    public CustomListViewAdapter(Context context, int resourceId,
            List<RowItem> items) {
        super(context, resourceId, items);
        this.context = context;
        this.items= items;
    }

    /*private view holder class*/
    private class ViewHolder {
        ImageView imageView;
       // TextView txtTitle;
        TextView name;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;


        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
          convertView = mInflater.inflate(R.layout.list_item, null);
            holder = new ViewHolder();
            holder.name = (TextView) convertView.findViewById(R.id.name);
            /*holder.txtTitle = (TextView) convertView.findViewById(R.id.title);*/
            holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
            convertView.setTag(holder);
        } else
            holder = (ViewHolder) convertView.getTag();

        holder.name.setText(items.get(position).getName());

        Bitmap bitMap=downloadImage(items.get(position).getImageUrl());
        holder.imageView.setImageBitmap(bitMap);
        return convertView;
    }



    private Bitmap downloadImage(String url) {
        Bitmap bitmap = null;
        InputStream stream = null;
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        bmOptions.inSampleSize = 1;

        try {
            stream = getHttpConnection(url);
            bitmap = BitmapFactory.
                    decodeStream(stream, null, bmOptions);
            stream.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        return bitmap;
    }

    // Makes HttpURLConnection and returns InputStream
    private InputStream getHttpConnection(String urlString)
            throws IOException {
        InputStream stream = null;
        URL url = new URL(urlString);
        URLConnection connection = url.openConnection();

        try {
            HttpURLConnection httpConnection = (HttpURLConnection) connection;
            httpConnection.setRequestMethod("GET");
            httpConnection.connect();

            if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                stream = httpConnection.getInputStream();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return stream;
    }
}

答案 2 :(得分:0)

您已经输入了if语句并更改了流的值,但是如果false stream的条件为null if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { stream = httpConnection.getInputStream(); }。因此,添加else语句并将stream的值从null更改为值。

希望它有所帮助!祝你的项目好运!

答案 3 :(得分:-1)

nullpointer出现在downloadImage()方法的某处。在第80行,在那里放置一个断点,你将对这种情况有更多的了解。该URL可能无效或为空。