Android 在片段内部使用GridView时应用程序崩溃
当textView更改为GridView时,我的应用程序不断崩溃Android 在片段内部使用GridView时应用程序崩溃,android,fragment,Android,Fragment,当textView更改为GridView时,我的应用程序不断崩溃 TextView textView = (TextView) view; textView.setText("Fragment Science #" + mPage); main java.lang.ClassCastException: android.widget.GridView cannot be cast to android.widget.TextView .... 04-14 09:18:50.704
TextView textView = (TextView) view;
textView.setText("Fragment Science #" + mPage);
main java.lang.ClassCastException: android.widget.GridView cannot be cast to android.widget.TextView ....
04-14 09:18:50.704 6903-6903/com.example.bernine.practicalsessions E/AndroidRuntime:致命异常:main java.lang.ClassCastException:android.widget.GridView无法在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"
/>
当textView没有注释,gridview有注释时,应用程序运行良好 从
片段中删除这部分代码
TextView textView = (TextView) view;
textView.setText("Fragment Science #" + mPage);
而不是把你的GridView
logic从代码中删除你的TextView
,声明你的GridView
并为你的GridView制作适配器
TextView textView = (TextView) view;
textView.setText("Fragment Science #" + mPage);
main java.lang.ClassCastException: android.widget.GridView cannot be cast to android.widget.TextView ....
这意味着您仍在使用Textview您能提供错误的堆栈跟踪吗?04-14 09:18:50.704 6903-6903/com.example.bernine.practicalsessions E/AndroidRuntime:FATAL EXCEPTION:main java.lang.ClassCastException:android.widget.GridView无法在com.example.bernine.practicalsessions.TechnologyFragment.onCreateView(TechnologyFragment.java:38)上转换到android.widget.TextView,这意味着您正在尝试“将GridView强制转换为TextView”。执行以下操作:GridView gView=(GridView)视图;尝试将此行更改为TextView TextView=(TextView)视图;
更改为GridView GridView=(GridView)view;
then.是否必须在onCreate或onCreateView方法中声明gridView?我使用的是Fragments。您可以像使用GrdiView一样进行声明。您以前使用过gridView吗?请检查问题的答案或检查此项