Мое приложение продолжает сбой, когда textView был изменен на GridView.Приложение сбой при использовании внешнего фрагмента GridView
04-14 09: 18: 50,704 6903-6903/com.example.bernine.practicalsessions Е/AndroidRuntime: неустранимый: Основной java.lang.ClassCastException: android.widget.GridView не может быть приведен к Android. widget.TextView в com.example.bernine.practicalsessions.TechnologyFragment.onCreateView (технология Fragment.java:38)
Я делаю что-то неправильно в файле XML?
<?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"
/>
И это класс фрагмент
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;
}
}
Когда TextView был раскомментирована и GridView комментировали, приложение работало хорошо.
Можете ли вы предоставить StackTrace ошибки? – GVillani82
04-14 09: 18: 50.704 6903-6903/com.example.bernine.practicalsessions E/AndroidRuntime: FATAL EXCEPTION: main java.lang.ClassCastException: android.widget.GridView нельзя передать в android.widget.TextView at com.example.bernine.practicalsessions.TechnologyFragment.onCreateView (TechnologyFragment.java:38) = – Techworld
Это означает, что вы пытаетесь «передать GridView в TextView». Сделайте так: GridView gView = (GridView); – GVillani82