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

时间:2019-01-05 16:44:34

标签: android gridview nullpointerexception baseadapter

我正在尝试在自定义弹出窗口模式中放置GridView,以在网格中显示一些信息,但是我继续获取NullPointerException,但所有步骤对我来说都是正确的。当我运行调试模式时,它说我的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;
}}

我的GridView适配器

public class GridViewAdapter extends BaseAdapter {
LayoutInflater layoutInflater;
Context context;

public GridViewAdapter(Context context) {
    this.context = context;
}

int[] images = {
        R.drawable.text,
        R.drawable.pdf,
        R.drawable.popupscanner,
        R.drawable.text
};

String[] titles = {
        "Write Note",
        "Upload Note",
        "Scan Document",
        "Text Recogniton"
};

@Override
public int getCount() {
    return titles.length;
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
    View view = layoutInflater.inflate(R.layout.row_info,null);
    TextView title = (TextView) convertView.findViewById(R.id.titleTextView);
    ImageView icon = (ImageView) convertView.findViewById(R.id.iconImageView);

    title.setText(titles[position]);
    icon.setImageResource(images[position]);
    return view;
}}

我的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>

我犯了什么错误?

1 个答案:

答案 0 :(得分:0)

替换:“ 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。

希望这对您有所帮助!