Android中抛出android.app.Fragment$InstantiationException异常
来源:互联网 发布:手动榨汁机好用么 知乎 编辑:程序博客网 时间:2024/05/22 07:01
在照着郭霖的《第一行代码》第二版写酷欧天气时,开头是在MainActivity中填充ChooseAreaFragment。书里的写法是在MainActivity的布局文件activity_main.xml中将Fragment作为一个控件放置进去:
<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <fragment android:id="@+id/choose_area_fragment" android:name="com.lindroid.coolweather.ChooseAreaFragment" android:layout_width="match_parent" android:layout_height="match_parent" /></FrameLayout>
代码看起来没有问题,但是运行之后却崩溃了,抛出了下面这个异常:
Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class com.lindroid.coolweather.ChooseAreaFragment that is not a Fragment
这是因为在布局文件中使用的fragment节点设置Fragment时,默认的是android.app.Fragment,而我在创建ChooseAreaFragment时继承的是android.support.v4.app.Fragment。所以解决方法有两种:
- 将ChooseAreaFragment继承的Fragment改为android.app.Fragment即可;
- 要想在Activity中使用android.support.v4.app.Fragment,可以让MainActivity继承FragmentActivity。
按照上面两种方法来解决后,运行就不会报错了。
0 0
- Android中抛出android.app.Fragment$InstantiationException异常
- 关于Android中抛出android.app.Fragment$InstantiationException异常
- fragment异常android.app.Fragment$InstantiationException: Unable to instantiate fragment
- android.app.Fragment$InstantiationException 解决办法
- android.app.Fragment$InstantiationException 解决办法
- android.support.v4.app.Fragment$InstantiationException问题解决
- android.support.v4.app.Fragment$InstantiationException解决方案
- android.app.Fragment$InstantiationException:(小结)
- 找不到添加类。android.app.Fragment$InstantiationException:
- android.support.v4.app.Fragment$InstantiationException问题解决
- android.app.Fragment$InstantiationException的原因分析
- android.support.v4.app.Fragment$InstantiationException
- android.app.Fragment$InstantiationException的原因分析
- android.support.v4.app.Fragment$InstantiationException问题解决
- Android Fragment InstantiationException解决
- android.support.v4.app.Fragment$InstantiationException: Unable to instantiate&nbs
- 【Android策略】Fragment实例化及通过setArguments解决 android.support.v4.app.Fragment$InstantiationException
- android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment xxx.fragments
- Android中Loader及LoaderManager的使用(附源码下载)
- C++中引用(&)的用法和应用实例
- CAS
- web 屏蔽BackSpace键
- C/C++ 图像处理(15)------DXF文件の读取、绘图与显示
- Android中抛出android.app.Fragment$InstantiationException异常
- 自定义标签实现转义标签内文本内容
- Spring MVC 教程,快速入门,深入分析
- 新手上路
- 4.14制作视频播放
- how to import x509.pem pk8 file into jks(keystore)?
- Android APK软件汉化整合与探讨
- 116. Populating Next Right Pointers in Each Node**
- 中国大学MOOC翁恺C语言入门期末试题