异常调度输入事件。和LruCache

时间:2014-09-07 01:56:28

标签: java android apk-expansion-files

滚动ListView时遇到问题。过了一会儿,我在logcat中得到了错误,但我知道问题是什么。

这是我的代码:

public class ListFeedAdapter extends BaseAdapter {

    private ArrayList<Feed> feeds;
    private LruCache<String, Bitmap> mMemoryCache;

    private LayoutInflater inflater;

    public ListFeedAdapter(Context context,ArrayList<Feed> feeds) {
        inflater = LayoutInflater.from(context);
        this.feeds = feeds;
         // Bitmap Cache
        final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
        final int cacheSize = maxMemory / 8;
        mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
            @Override
            protected int sizeOf(String key, Bitmap bitmap) {
                return bitmap.getByteCount() / 1024;
            }

        };

    }


    public int getCount() {
        return feeds.size();
    }


     // Permet de retourner un objet contenu dans la liste


    public Object getItem(int index) {
        return feeds.get(index);
    }


    public long getItemId(int index) {
        return this.feeds.get(index).getId();

    }



     // * Voici la partie la plus complexe de la classe




    public View getView(int position, View convertView, ViewGroup parent){

        FeedView fv=null;

    if (convertView == null) {
            fv = new FeedView();

            convertView = inflater.inflate(R.layout.feed_view, null);
             fv.image=(ImageView)convertView.findViewById(R.id.imageview);
            fv.description = (TextView)convertView.findViewById(R.id.description);            
            fv.title = (TextView)convertView.findViewById(R.id.title);
            fv.pubDate = (TextView)convertView.findViewById(R.id.pub_date);
            convertView.setTag(fv);

        } else {
            fv = (FeedView) convertView.getTag();
        }    

     Bitmap bitmap = mMemoryCache.get(feeds.get(position).getImage());

     if (bitmap != null) {
         fv.image.setImageBitmap(bitmap);
     } else {

         ThumbnailTask getbitmap=new ThumbnailTask(fv.image, feeds.get(position).getImage(), position);
         getbitmap.execute();


     }



        fv.description.setText(feeds.get(position).getDescription());
        fv.pubDate.setText(feeds.get(position).getPubDate());
        fv.title.setText(feeds.get(position).getTitle());

        return convertView;
    }




    public static int getSizeInBytes(Bitmap bitmap) {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            return bitmap.getByteCount();
        } else {
            return bitmap.getRowBytes() * bitmap.getHeight();
        }
    }



    public class ThumbnailTask extends AsyncTask<Void,Void,Bitmap> {


         private  ImageView bmImage;
         private String url;
         private int pst;

         public ThumbnailTask(ImageView image,String url,int pst) {
                this.bmImage=image; 
                this.url=url;
                this.pst=pst;
                bmImage.setTag(pst);
                bmImage.setImageBitmap(null);
          }



        @Override
        protected Bitmap doInBackground(Void... arg0) {



             URL url=null;
             HttpURLConnection connection;
             InputStream input;
            Bitmap img=null;

                try {
                      url = new URL(this.url);
                    connection = (HttpURLConnection) url.openConnection();
                    connection.connect();
                     input = connection.getInputStream();
                   img = BitmapFactory.decodeStream(input);
                    input.close();

                } catch (IOException e) {
                    Log.e("img", "Error getting bitmap"); 
                }           
                mMemoryCache.put(this.url, img);

            return img;
        }

        protected void onPostExecute(Bitmap bitmap) {
             super.onPostExecute(bitmap);
             if(bitmap != null && ((Integer)this.bmImage.getTag()) == this.pst)
                         this.bmImage.setImageBitmap(bitmap);
            }    
}

这是LogCat数据:

09-06 21:29:17.139: E/InputEventReceiver(2216): Exception dispatching input event.
09-06 21:29:17.209: D/AndroidRuntime(2216): Shutting down VM
09-06 21:29:17.259: W/dalvikvm(2216): threadid=1: thread exiting with uncaught exception (group=0xb2a9fba8)
09-06 21:29:17.449: E/AndroidRuntime(2216): FATAL EXCEPTION: main
09-06 21:29:17.449: E/AndroidRuntime(2216): Process: com.test.fragmenttest, PID: 2216
09-06 21:29:17.449: E/AndroidRuntime(2216): java.lang.NullPointerException: key == null
09-06 21:29:17.449: E/AndroidRuntime(2216):     at android.util.LruCache.get(LruCache.java:113)
09-06 21:29:17.449: E/AndroidRuntime(2216):     at com.test.fragmenttest.ListFeedAdapter.getView(ListFeedAdapter.java:113)
09-06 21:29:17.449: E/AndroidRuntime(2216):     at android.widget.AbsListView.obtainView(AbsListView.java:2240)
09-06 21:29:17.449: E/AndroidRuntime(2216):     at android.widget.ListView.makeAndAddView(ListView.java:1790)
09-06 21:29:17.449: E/AndroidRuntime(2216):     at android.widget.ListView.fillDown(ListView.java:691)
09-06 21:29:17.449: E/AndroidRuntime(2216):     at android.widget.ListView.fillGap(ListView.java:655)
09-06 21:29:17.449: E/AndroidRuntime(2216):     at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5136)
09-06 21:29:17.449: E/AndroidRuntime(2216):     at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3236)
09-06 21:29:17.449: E/AndroidRuntime(2216):     at android.widget.AbsListView.onTouchMove(AbsListView.java:3580)
09-06 21:29:17.449: E/AndroidRuntime(2216):     at android.widget.AbsListView.onTouchEvent(AbsListView.java:3424)
09-06 21:29:17.449: E/AndroidRuntime(2216):     at android.view.View.dispatchTouchEvent(View.java:7706)
09-06 21:29:17.449: E/AndroidRuntime(2216):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2210)
09-06 21:29:17.449: E/AndroidRuntime(2216):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1945)
09-06 21:29:17.449: E/AndroidRuntime(2216):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
09-06 21:29:17.449: E/AndroidRuntime(2216):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
09-06 21:29:17.449: E/AndroidRuntime(2216):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
09-06 21:29:17.449: E/AndroidRuntime(2216):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
09-06 21:29:17.449: E/AndroidRuntime(2216):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
09-06 21:29:17.449: E/AndroidRuntime(2216):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
09-06 21:29:17.449: E/AndroidRuntime(2216):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
09-06 21:29:17.449: E/AndroidRuntime(2216):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
09-06 21:29:17.449: E/AndroidRuntime(2216):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
09-06 21:29:17.449: E/AndroidRuntime(2216):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
......

0 个答案:

没有答案