Attempt to invoke virtual method 'void com.facebook.drawee.view.SimpleDraweeView.setImageURI(android

 FATAL EXCEPTION: main
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime: Process: tech.androidstudio.recyclerviewdemo, PID: 20080
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.facebook.drawee.view.SimpleDraweeView.setImageURI(android.net.Uri)' on a null object reference
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime:     at tech.androidstudio.recyclerviewdemo.adapter.PicRecyclerViewAdapter.onBindViewHolder(PicRecyclerViewAdapter.java:42)
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime:     at tech.androidstudio.recyclerviewdemo.adapter.PicRecyclerViewAdapter.onBindViewHolder(PicRecyclerViewAdapter.java:24)
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime:     at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5217)
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime:     at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5250)
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime:     at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4487)
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime:     at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4363)
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime:     at android.support.v7.widget.LayoutState.next(LayoutState.java:86)
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime:     at android.support.v7.widget.StaggeredGridLayoutManager.fill(StaggeredGridLayoutManager.java:1423)
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime:     at android.support.v7.widget.StaggeredGridLayoutManager.onLayoutChildren(StaggeredGridLayoutManager.java:610)
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime:     at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2900)
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime:     at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3071)
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime:     at android.view.View.layout(View.java:15614)
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:4968)
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime:     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076)
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime:     at android.view.View.layout(View.java:15614)
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:4968)
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime:     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime:     at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime:     at android.view.View.layout(View.java:15614)
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:4968)
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime:     at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:435)
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime:     at android.view.View.layout(View.java:15614)
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:4968)
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime:     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime:     at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime:     at android.view.View.layout(View.java:15614)
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:4968)
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime:     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime:     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime:     at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime:     at android.view.View.layout(View.java:15614)
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:4968)
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime:     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime:     at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime:     at android.view.View.layout(View.java:15614)
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:4968)
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime:     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2102)
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime:     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1859)
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime:     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1077)
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime:     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5884)
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime:     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime:     at android.view.Choreographer.doCallbacks(Choreographer.java:580)
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime:     at android.view.Choreographer.doFrame(Choreographer.java:550)
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime:     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739)
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135)
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5310)
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)

03-15 10:07:47.642 20080-20080/tech.androidstudio.recyclerviewdemo E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)


原因分析:

因为在RecyclerView的onCreateViewHolder里面返回错误。那么在Bind 的时候就会报错了。

    @Override
    public PicViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

       
//        PicViewHolder picViewHolder = new PicViewHolder(parent);
//        return picViewHolder;
//        return null;
    }

    @Override
    public void onBindViewHolder(PicViewHolder holder, int position) {
        Log.d("Kodulf","position"+position+" Uri:");
        Product product = list.get(position);
        holder.pic.setImageURI(Uri.parse(product.getLocalPosition()));
        holder.textView.setText(product.getProductName());
    }


解决方法:


    public PicViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View ret = LayoutInflater.from(context).inflate(R.layout.item_recyclerview,parent,false);
        return new PicViewHolder(ret);

        //TODO 下面的 返回方法是错误的,如果这样做的话,会报错
        //TODO java.lang.NullPointerException: Attempt to invoke virtual method 'void com.facebook.drawee.view.SimpleDraweeView.setImageURI(android.net.Uri)' on a null object reference

//        PicViewHolder picViewHolder = new PicViewHolder(parent);
//        return picViewHolder;
//        return null;
    }

    @Override
    public void onBindViewHolder(PicViewHolder holder, int position) {
        Log.d("Kodulf","position"+position+" Uri:");
        Product product = list.get(position);
        holder.pic.setImageURI(Uri.parse(product.getLocalPosition()));
        holder.textView.setText(product.getProductName());
    }

你可能感兴趣的:(TroubleShooting,开发中遇到的问题解决)