java.lang.NullPointerException:尝试在空对象引用上调用虚方法 'void android.widget.CardView.setVisibility(int)'

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.CardView.setVisibility(int)' on a null object reference

我正在尝试在按下 FAB 时为 Cardview 设置动画。我想获得圆形动画。 Cardview 最初设置为不可见。但是,它在 CardView.setVisibility.

上引用空对象
public class FeedFragment extends Fragment {

private FloatingActionButton floatingBtn;
private CardView cardView;
boolean flag = true;
public FeedFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_feed, container, false);

    floatingBtn = view.findViewById(R.id.floatBtn);
    cardView = view.findViewById(R.id.cardView);

    floatingBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if(flag) {

                // Check if the runtime version is at least Lollipop
                if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP) {
                    // get the center for the clipping circle
                    int cx = cardView.getWidth() / 2;
                    int cy = cardView.getHeight() / 2;

                    // get the final radius for the clipping circle
                    float finalRadius = (float) Math.hypot(cx, cy);

                    // create the animator for this view (the start radius is zero)
                    Animator anim =
                            ViewAnimationUtils.createCircularReveal(cardView, cx, cy, 0, finalRadius);

                    // make the view visible and start the animation
                    cardView.setVisibility(View.VISIBLE);
                    anim.start();
                } else {
                    // set the view to visible without a circular reveal animation below Lollipop
                    cardView.setVisibility(View.VISIBLE);
                }

                flag = false;
            } else {

                // Check if the runtime version is at least Lollipop
                if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP) {
                    // get the center for the clipping circle
                    int cx = cardView.getWidth() / 2;
                    int cy = cardView.getHeight() / 2;

                    // get the initial radius for the clipping circle
                    float initialRadius = (float) Math.hypot(cx, cy);

                     // create the animation (the final radius is zero)
                    Animator anim =
                            ViewAnimationUtils.createCircularReveal(cardView, cx, cy, initialRadius, 0);

                   // make the view invisible when the animation is done
                    anim.addListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            super.onAnimationEnd(animation);
                            cardView.setVisibility(View.INVISIBLE);
                        }
                    });

                 // start the animation
                    anim.start();
                } else {
                    // set the view to visible without a circular reveal animation below Lollipop
                    cardView.setVisibility(View.VISIBLE);
                }
                flag = true;
            }

        }
    });

    return view;
   }
  }

这是XML代码

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.authent.authentication.FeedFragment">

<!-- TODO: Update blank fragment layout -->

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_gravity="bottom">

    <android.support.v7.widget.CardView
        android:id="@+id/cardView"
        android:layout_width="match_parent"
        android:layout_height="208dp"
        android:layout_gravity="bottom"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginStart="16dp"
        app:cardCornerRadius="20dp"
        android:visibility="gone">

        <LinearLayout
            android:id="@+id/linearReveal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:visibility="gone">

            <Button
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="@color/white"
                android:text="Ask a Query"
                android:textSize="24dp"
                android:textAllCaps="false"
                android:textColor="#00ccff"/>

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="@android:color/darker_gray"/>

            <Button
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="@color/white"
                android:text="Share Something"
                android:textSize="24dp"
                android:textAllCaps="false"
                android:textColor="#ff3300"/>

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="@android:color/darker_gray"/>

            <Button
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="@color/white"
                android:text="Post an MCQ"
                android:textSize="24dp"
                android:textAllCaps="false"
                android:textColor="#cc9900"/>

        </LinearLayout>

    </android.support.v7.widget.CardView>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/floatBtn"
        android:layout_width="56dp"
        android:layout_height="56dp"
        android:layout_gravity="end"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="16dp"
        android:layout_marginRight="16dp"
        android:src="@drawable/edit_nick_name" />

</LinearLayout>

求大神帮忙实现圆形动画。我是编程新手。 我还尝试使 LinearLayout INVISIBLE,它还在空对象引用的 LinearLayout.setVisibility 上抛出 NullPointerException。 提前致谢。

Intilaize cardview 在 "clickListener" 之外。

您的 CardView 为空。根据您的代码,您正在尝试从收到 onClick 的视图中获取 CardView,即 FloatingButton.

尝试在侦听器之前调用外部初始化。理想情况下与初始化 FloatingButton.

的级别相同

你只需要删除这一行cardView = view.findViewById(R.id.cardView);

并粘贴到

之前
    floatingBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {


       return view;
 }