java.lang.NullPointerException:尝试调用虚拟方法';void android.widget.GridView.setAdapter

java.lang.NullPointerException:尝试调用虚拟方法';void android.widget.GridView.setAdapter,android,gridview,nullpointerexception,baseadapter,Android,Gridview,Nullpointerexception,Baseadapter,我试图在我的自定义弹出模式中放置一个GridView,以在网格中显示一些信息,但我一直得到一个NullPointerException,但所有步骤对我来说都是正确的。当我运行调试模式时,它会显示myLayoutInflater。我不确定这是否是问题所在 我的错误: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.davidlutta.purenote/com.davidlutta.purenote.

我试图在我的自定义弹出模式中放置一个
GridView
,以在网格中显示一些信息,但我一直得到一个
NullPointerException
,但所有步骤对我来说都是正确的。当我运行调试模式时,它会显示my
LayoutInflater
。我不确定这是否是问题所在

我的错误:

 java.lang.RuntimeException: Unable to start activity  ComponentInfo{com.davidlutta.purenote/com.davidlutta.purenote.UI.Activities.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.GridView.setAdapter(android.widget.ListAdapter)' on a null object reference

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.GridView.setAdapter(android.widget.ListAdapter)' on a null object reference
    at com.davidlutta.purenote.UI.Fragments.Home_Fragment.onCreateView(Home_Fragment.java:82)
我的碎片类

public class Home_Fragment extends Fragment {

@BindView(R.id.floatingActionButton) FloatingActionButton floatingActionButton;

private String TAG = ">>>>>>>>";

Dialog customDialog;


public Home_Fragment() {
    // Required empty public constructor
}


public static Home_Fragment newInstance() {
    Home_Fragment fragment = new Home_Fragment();
    return fragment;
}

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

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.fragment_home_, container, false);
    ButterKnife.bind(this,root);

    customDialog = new Dialog(getContext());

    floatingActionButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            customDialog.setContentView(R.layout.upload_popup);

            ImageView closeDialogButton = (ImageView) customDialog.findViewById(R.id.closeButton);

            closeDialogButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    customDialog.dismiss();
                }
            });

            customDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            customDialog.show();
        }
    });

    GridView gridView = (GridView) getActivity().findViewById(R.id.gridView);
    GridViewAdapter gridViewAdapter = new GridViewAdapter(getContext());

    gridView.setAdapter(gridViewAdapter);

    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toasty.info(getContext(),"I Work",Toast.LENGTH_SHORT).show();
        }
    });
    return root;
}}
我的XML文件

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"
android:gravity="center">
<ImageView
    android:id="@+id/closeButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_close_black_24dp"
    android:layout_marginTop="7dp"
    android:layout_marginRight="7dp"
    android:elevation="@dimen/card_elevation"
    android:layout_alignParentRight="true"/>

<androidx.cardview.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardCornerRadius="@dimen/card_radius"
    app:cardBackgroundColor="@color/colorCream"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true">

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_marginTop="25dp"
            android:layout_marginBottom="25dp">

            <GridView
                android:id="@+id/gridView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:numColumns="2"
                android:verticalSpacing="1dp"
                android:horizontalSpacing="1dp"></GridView>

        </LinearLayout>
    </LinearLayout>

</androidx.cardview.widget.CardView>

我犯了哪个错误?

替换:“GridView GridView=(GridView)getActivity().findViewById(R.id.GridView)” 作者:“GridView GridView=(GridView)root.findViewById(R.id.GridView)”

我想是因为GridView放在fragment_home.xml中,而不是放在活动布局xml文件中。因此,当调用getActivity().findViewById(..)时,它找不到gridview并返回null


希望这对你有帮助

请看Home_Fragment.java第82行。似乎有一个指向null的对象的用法,不幸的是它仍然不起作用
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"
android:gravity="center">
<ImageView
    android:id="@+id/closeButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_close_black_24dp"
    android:layout_marginTop="7dp"
    android:layout_marginRight="7dp"
    android:elevation="@dimen/card_elevation"
    android:layout_alignParentRight="true"/>

<androidx.cardview.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardCornerRadius="@dimen/card_radius"
    app:cardBackgroundColor="@color/colorCream"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true">

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_marginTop="25dp"
            android:layout_marginBottom="25dp">

            <GridView
                android:id="@+id/gridView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:numColumns="2"
                android:verticalSpacing="1dp"
                android:horizontalSpacing="1dp"></GridView>

        </LinearLayout>
    </LinearLayout>

</androidx.cardview.widget.CardView>