应用程序在recyclerview中的holder.textViewVar.setVisibility之后崩溃

时间:2017-02-27 14:42:23

标签: android android-layout android-studio android-xml

我有一个recyclerView,我想检查一下是否出现了某个按钮:

@Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        Posts post =  Posts.get(position);
        holder.post = post;

        //Loading image from url
        imageLoader = CustomVolleyRequest.getInstance(context).getImageLoader();

//THE PROBLEM IS HERE (in this 7 lines):
        String lk = post.getLk();
        if(lk.equals("1")){ //show button
            holder.textViewUnlk.setVisibility(View.VISIBLE);
        }
        else { //hide
            holder.textViewLk.setVisibility(View.VISIBLE);
        }



        holder.textViewName.setText(post.getName());

        holder.textViewPublisher.setText(post.getPublisher());
        holder.setIsRecyclable(false);

        } 

textViewLk和textViewUnlk在xml中为GONE。

为什么我的应用崩溃了?

< - edit ------------------------------------------ -----------

    public ViewHolder(View itemView) {
        super(itemView);
        imageView = (NetworkImageView) itemView.findViewById(R.id.imageViewHero);
        textViewName = (TextView) itemView.findViewById(R.id.textViewName);
        textViewPublisher = (TextView) itemView.findViewById(R.id.textViewPublisher);
        textViewLk = (TextView) itemView.findViewById(R.id.textViewLk);
        textViewUnlk = (TextView) itemView.findViewById(R.id.textViewUnlk);
        textViewName.setClickable(true);
        textViewName.setOnClickListener(this);
        textViewLk.setClickable(true);
        textViewLk.setOnClickListener(this);
        textViewUnlk.setClickable(true);
        textViewUnlk.setOnClickListener(this);
    }

崩溃日志:

--------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.sk.mf, PID: 2955
                  java.lang.NullPointerException: println needs a message
                      at android.util.Log.println_native(Native Method)
                      at android.util.Log.w(Log.java:185)
                      at com.sk.mf.CardAdapter.onBindViewHolder(CardAdapter.java:76)
                      at com.sk.mf.CardAdapter.onBindViewHolder(CardAdapter.java:35)
                      at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6279)
                      at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6312)
                      at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5258)
                      at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5521)
                      at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5363)
                      at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5359)
                      at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2141)
                      at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1525)
                      at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1488)
                      at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:585)
                      at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3506)
                      at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3254)
                      at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3767)
                      at android.view.View.layout(View.java:17523)
                      at android.view.ViewGroup.layout(ViewGroup.java:5612)
                      at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
                      at android.view.View.layout(View.java:17523)
                      at android.view.ViewGroup.layout(ViewGroup.java:5612)
                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                      at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                      at android.view.View.layout(View.java:17523)
                      at android.view.ViewGroup.layout(ViewGroup.java:5612)
                      at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1193)
                      at android.view.View.layout(View.java:17523)
                      at android.view.ViewGroup.layout(ViewGroup.java:5612)
                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                      at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                      at android.view.View.layout(View.java:17523)
                      at android.view.ViewGroup.layout(ViewGroup.java:5612)
                      at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:437)
                      at android.view.View.layout(View.java:17523)
                      at android.view.ViewGroup.layout(ViewGroup.java:5612)
                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                      at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                      at android.view.View.layout(View.java:17523)
                      at android.view.ViewGroup.layout(ViewGroup.java:5612)
                      at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
                      at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
                      at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
                      at android.view.View.layout(View.java:17523)
                      at android.view.ViewGroup.layout(ViewGroup.java:5612)
                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                      at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                      at com.android.internal.policy.DecorView.onLayout(DecorView.java:724)
                      at android.view.View.layout(View.java:17523)
                      at android.view.ViewGroup.layout(ViewGroup.java:5612)
                      at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2342)
                      at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2069)
                      at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1246)
                      at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6301)
                      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
                      at android.view.Choreographer.doCallbacks(Choreographer.java:683)
                      at android.view.Choreographer.doFrame(Choreographer.java:619)
E/AndroidRuntime:     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
                      at android.os.Handler.handleCallback(Handler.java:751)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6077)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Application terminated.

posts.java:

public String getLk() {
        return lk;
    }

1 个答案:

答案 0 :(得分:2)

Log.wgetLk()试图不打印任何内容。

确保始终填充该日志中的值。你可以使用如下快速讨厌的黑客:

Log.w("whatever", "" + whateverItIsYourePrinting);

(与字符串连接以确保始终有要打印的内容)

您可能还需要更改

if (lk.equals("1")) {

if (lk != null && lk.equals("1")) {