Search code examples
javaandroidxmlandroid-fragmentsandroid-fragmentactivity

GridView Initialization Using findViewById Crashing My App, but ALL Other Code Runs?


So I have almost completed this app I just wanted to add a GridView into it that has 9 columns, 3 rows, and does some stuff with it. However, when I went to assign Gridview gridView to my ID in XML, it crashed. There was NO other code related to the grid, and when I remove the line, the app starts. Within the XML, I am SURE the ID exists. As an example, I put in a test, EditText dataEdt1. The dataEdt1 works, meaning that the XML file is read, and the ID should be visible. Here is the Java, followed by the XML.

public class Auto extends Fragment implements View.OnClickListener{//, GridView.OnItemClickListener{
public Auto() {
        // Required empty public constructor
    }

    public static Auto newInstance() {
        Auto fragment = new Auto();
        Bundle args = new Bundle();

        fragment.setArguments(args);
        return fragment;
    }
    Context context;
    EditText dataEdt1;
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TransitionInflater inflater = TransitionInflater.from(requireContext());
        setExitTransition(inflater.inflateTransition(R.transition.fade_f));
        setEnterTransition(inflater.inflateTransition(R.transition.slide_right_f));
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // THIS IS THE PROBLEM METHOD
        View root = inflater.inflate(R.layout.fragment_auto, container,false);
        context = container.getContext();
        dataEdt1 = root.findViewById(R.id.textComment);
        GridView gridView = root.findViewById(R.id.gridView1); //THIS LINE CRASHES THE APP, but it does not have a Gradle build error. 
        /*
        the code I want to add once this GridView is being detected.
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                if (getBackgroundColor(v) == Color.parseColor("#FFFFFF")){
                    v.setBackgroundColor(Color.parseColor("#FF0000"));
                    gridQR.set(position, 0);

                }else{
                    v.setBackgroundColor(Color.parseColor("#FFFFFF"));
                    gridQR.set(position, 1);

                }
            }

        });
         */
        
        dataEdt1.setOnClickListener(this); //As an example, I did this code, which works. 
        dataEdt1.setText(MainActivity.getAutoC());
        return root;
    }
@Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.textComment:
                dataEdt1.setText("");
                break;
        }
    }

Here's the XML

 <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/frameLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/cardview_dark_background"
    tools:context=".Auto">
  <EditText
            android:id="@+id/textComment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="15dp"
            android:autofillHints=""
            android:hint="Comments about Auto"
            android:inputType="text"
            android:textSize="26dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent" />
<GridLayout
            android:id="@+id/gridView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:columnCount="9"
            android:orientation="horizontal"
            android:rowCount="3"
            app:layout_constraintWidth="match_constraint"
            app:layout_constraintBottom_toTopOf="@+id/textComment"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/gridHeader3"
            app:layout_constraintTop_toBottomOf="@+id/textView"
            tools:context=".GridXMLActivity">
</FrameLayout>

I have tried to run the code without the line about finding the view by ID. This works. However, the second I set GridView to the root.findViewById(R.id.gridView1);, it crashes. There's nothing else I've tried, I'm stumped.


Solution

  • I replicated your code and this is what the issue is android.widget.GridLayout cannot be cast to android.widget.GridView

    I think you need to change the Gridlayout to GridView in the xml.