为了账号安全,请及时绑定邮箱和手机立即绑定

按照老师的代码写的,还是闪退。下面是错误代码。

10-05 21:45:43.645 7552-7552/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                 Process: com.mengmengda.gridview, PID: 7552
                                                 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mengmengda.gridview/com.mengmengda.gridview.MainActivity}: java.lang.ClassCastException: android.widget.GridLayout cannot be cast to android.widget.GridView
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2330)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392)
                                                     at android.app.ActivityThread.access$800(ActivityThread.java:154)
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                     at android.os.Looper.loop(Looper.java:135)
                                                     at android.app.ActivityThread.main(ActivityThread.java:5273)
                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                     at java.lang.reflect.Method.invoke(Method.java:372)
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
                                                  Caused by: java.lang.ClassCastException: android.widget.GridLayout cannot be cast to android.widget.GridView
                                                     at com.mengmengda.gridview.MainActivity.onCreate(MainActivity.java:28)
                                                     at android.app.Activity.performCreate(Activity.java:6041)
                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1109)
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2283)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392)
                                                     at android.app.ActivityThread.access$800(ActivityThread.java:154)
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                     at android.os.Looper.loop(Looper.java:135)
                                                     at android.app.ActivityThread.main(ActivityThread.java:5273)
                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                     at java.lang.reflect.Method.invoke(Method.java:372)
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)

正在回答

2 回答

错误代码中的第四行android.widget.GridLayout cannot be cast to android.widget.GridView的意思应该是你layout布局文件中的布局方式写错了,老师教程里用的是LineLayout,而你用的是GridLayout。所以说可能是GridLayout不居中不能用GridView控件。。然后我也是初学者,,只能说是可能是这个问题

0 回复 有任何疑惑可以回复我~
#1

慕数据2535060 提问者

我确实写错了,在布局文件中把该写成GridLayout的地方写成了GridView,不报错才怪,多谢提醒
2016-10-05 回复 有任何疑惑可以回复我~
#2

慕数据2535060 提问者

说反了 是GridView写成了GridLayout
2016-10-05 回复 有任何疑惑可以回复我~

恩恩  那我也明白了,错误提示的是GridLayout不能被强制转化为GridView,所以说在GridLayout中也能用GridView控件。。。

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
Android攻城狮的第二门课(第1季)
  • 参与学习       111162    人
  • 解答问题       1457    个

本课程由浅入深地带您学会Android的常用控件的开发和使用

进入课程

按照老师的代码写的,还是闪退。下面是错误代码。

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信