-5

My app keeps crashing when the textView was changed to a GridView.

04-14 09:18:50.704 6903-6903/com.example.bernine.practicalsessions E/AndroidRuntime: FATAL EXCEPTION: main java.lang.ClassCastException: android.widget.GridView cannot be cast to android.widget.TextView at com.example.bernine.practicalsessions.TechnologyFragment.onCreateView(Technology‌​Fragment.java:38)

Am I doing something wrong in the XML file?

<?xml version="1.0" encoding="utf-8"?>
<!--<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center" />-->

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnWidth="90dp"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
    />

And this is the fragment class

public class ScienceFragment extends Fragment {

    public static final String ARG_PAGE = "ARG_PAGE";

    private int mPage;

    public static ScienceFragment newInstance(int page) {
        Bundle args = new Bundle();
        args.putInt(ARG_PAGE, page);
        ScienceFragment fragment = new ScienceFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPage = getArguments().getInt(ARG_PAGE);
        Toast.makeText(getContext(), "Science", Toast.LENGTH_SHORT).show();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_sports_fragment, container, false);
        TextView textView = (TextView) view;
        textView.setText("Fragment Science #" + mPage);
        return view;
    }
}

When the textView was uncommented and the gridview was commented, the app worked well.

Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
Techworld
  • 141
  • 1
  • 12
  • Can you provide the stacktrace of the error? – GVillani82 Apr 14 '16 at 07:18
  • 04-14 09:18:50.704 6903-6903/com.example.bernine.practicalsessions E/AndroidRuntime: FATAL EXCEPTION: main java.lang.ClassCastException: android.widget.GridView cannot be cast to android.widget.TextView at com.example.bernine.practicalsessions.TechnologyFragment.onCreateView(TechnologyFragment.java:38) = – Techworld Apr 14 '16 at 07:19
  • This means that you are trying to "cast a GridView to TextView". Do like this: GridView gView = (GridView) view; – GVillani82 Apr 14 '16 at 07:21
  • Try changing this line `TextView textView = (TextView) view;` to `GridView gridView = (GridView ) view;` then. – Shree Krishna Apr 14 '16 at 07:21

2 Answers2

0

Remove this part of code from your Fragment

TextView textView = (TextView) view;
textView.setText("Fragment Science #" + mPage);

and instead of that put your GridView logic

Jimit Patel
  • 4,265
  • 2
  • 34
  • 58
0

Remove your TextView from code and declare your GridView and make adapter for your GridView.

 TextView textView = (TextView) view;
    textView.setText("Fragment Science #" + mPage);

main java.lang.ClassCastException: android.widget.GridView cannot be cast to android.widget.TextView ....

This means that you are still using Textview

Ameer
  • 2,709
  • 1
  • 28
  • 44
  • does the gridView have to be declared in the onCreate or onCreateView methods? I am using fragments – Techworld Apr 14 '16 at 07:25
  • You can do same like you are doing as GrdiView. Have you used Gridview before? http://stackoverflow.com/questions/19214620/android-imageadapter-with-gridview-in-fragment. Check the answers of the question or check this http://www.perfectapk.com/android-gridview-viewholder.html – Ameer Apr 14 '16 at 08:48