Android: Fragment (Trying to instantiate a class that is not a Fragment)
Fragments API: https://developer.android.com/guide/components/fragments.html
第一次使用Fragment遇到一个问题就是:
Trying to instantiate a class xxx that is not a Fragment)
问题来由:
Fragment是Android3.0(API Level 11)才开始引入的,之前的API不能直接使用android.app.Fragment。
就出现了兼容低版本的放在android.support.v4.jar兼容包中的Fragment。
但是通过Activity的布局文件内声明Fragment时,<fragment>节点默认是使用android.app.Fragment中的Fragment。
如果想使用android.support.v4.app.Fragment,需要Activity继承android.support.v4.app.FragmentActivity。
解决方案:
1. 自定义的Fragment需要继承自android.app.Fragment而不是android.support.v4.app.Fragment
这个需要API最低版本也支持Fragment才行;
package com.example.helloservice.fragments; import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; import com.example.helloservice.R; /**
* create an instance of this fragment.
*/
public class ExampleFragment extends Fragment { @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_example, container, false);
} @Override
public void onAttach(Activity activity) {
super.onAttach(activity);
} @Override
public void onDetach() {
super.onDetach();
} }
2. Activity继承自FragmentActivity而不是Activity
public class ExampleFragment extends android.support.v4.app.Fragment {
....
}
public class MainActivity extends android.support.v4.app.FragmentActivity {
....
}
xml:
<fragment
android:name="com.example.helloservice.fragments.ExampleFragment"
android:id="@+id/exampleFragment"
android:tag="@string/hello_blank_fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="300dp"/>
Android: Fragment (Trying to instantiate a class that is not a Fragment)的更多相关文章
- fragment Trying to instantiate a class com.example.testhuanxindemo.MyFragment that is not a Fragmen
在使用fragment的时候,先创建了一个fragment,然后为他创建布局,并在oncreateview中返回载入该视图的后返回的view,在activity的布局文件里,使用xml布局,用frag ...
- slidingmenu+fragment实现经常使用的側滑效果(包含Fragment状态的保存)
一.需求 关于fragment的问题,一直想写一篇博客了.应该当初自己也是对这玩意一点都不熟悉到如今也大概知道个日常的使用的地步. 一个側滑的导航栏,内有4个条目.每个选项点击进入相应的界面,每个界面 ...
- fragment显示 Binary XML file line #12: Error inflating class fragment 错误
问题 最近换了新机子,今天在静态用fragment时突然发现闪退,一看显示 Binary XML file line #12: Error inflating class fragment 错误 后面 ...
- 【Android开发实践】android.view.InflateException: Binary XML file line #12: Error inflating class fragment问题解决
一般出现的原因是fragment引入的包错了,应该是import android.app.ListFragment;而不是import android.support.v4.app.ListFragm ...
- android异常Unable to instantiate activity ComponentInfo解决方法
我是下面提到的第四条: 在Order and Export 中 把新加的 android-support-v4.jar的前面的对号打上勾 保存:就可以了: 做android开发的可能都碰到" ...
- Android笔记:Fragment与ViewPager组合时,如何在FragmentActicity获取Fragment对象
项目中有一处需要监听宿主的控件,再去更新Fragment页面,由此必须去获取Fragment里的对象; 在FragmentActivity + Fragment组合里是可以使用以下方案: MedicF ...
- Fragment生命周期及实现点击导航图片切换fragment,Demo
PS:Fragment简介 Fragment是Android3.0后引入的一个新的API,他出现的初衷是为了适应大屏幕的平板电脑, 当然现在他仍然是平板APP UI设计的宠儿,而且我们普通手机开发也会 ...
- Fragment之三:根据屏幕尺寸加载不同的Fragment
Fragment一个重要的作用在于根据屏幕的尺寸或者方向加载不同的布局. 未完待续
- Android 开发 之 Fragment 详解
本文转载于 : http://blog.csdn.net/shulianghan/article/details/38064191 本博客代码地址 : -- 单一 Fragment 示例 : http ...
随机推荐
- Spring context:component-scan代替context:annotation-config
Spring context:component-scan代替context:annotation-config XML: <?xml version="1.0" encod ...
- Spring Boot启动过程(一)
之前在排查一个线上问题时,不得不仔细跑了很多遍Spring Boot的代码,于是整理一下,我用的是1.4.3.RELEASE. 首先,普通的入口,这没什么好说的,我就随便贴贴代码了: SpringAp ...
- loadrunner controller:设置多个load generator
下面讲一下如何使用多台电脑进行负载测试. 1) 打开load generator,如图所示默认已添加了我们本地的Generator: 2) 点击"Add. ...
- CMD修改IP地址
在操作系统下,我们可以使用"本地连接"的属性来修改IP地址,但是如果我们要在多个IP地址之间切换,使用这种方法未免过于麻烦.我们可以使用NETSH命令来添加,相当简便.使用DOS修 ...
- B树的查找、插入、删除(附源代码)
B-Tree Index B-Tree搜索 B-Tree插入 分裂节点 插入节点 B-Tree删除 合并节点 删除节点 Basic B-Tree有两个比较重要的性质: 所有的leaf均在同一个leve ...
- Codeforces Round #257 (Div. 1)A~C(DIV.2-C~E)题解
今天老师(orz sansirowaltz)让我们做了很久之前的一场Codeforces Round #257 (Div. 1),这里给出A~C的题解,对应DIV2的C~E. A.Jzzhu and ...
- JavaScript 语法
一.JavaScript简介 1.JavaScript是个什么东西? 它是个脚本语言,需要有宿主文件,它的宿主文件是HTML文件. 2.它与Java什么关系? 没有什么直接的联系,Java是Sun公司 ...
- vs调试时底部输出调试信息“无法查找或打开 PDB 文件”解决办法
用VS调试程序时,有时会在VS底部的"输出"框中提示"无法查找或打开 PDB 文件".这该怎么解决呢? 下面,我们以VS2013为例,来教大家解决办法. 工具/ ...
- autotools归纳
最近接触到许多linux项目,其编译都是使用的autotools. autotools是一个自动化的编译工具.个人理解它的最主要功能就是生成Makefile. 因为直接写Makefiel,其依赖关系还 ...
- git 打卡的第一天
因为某种原因,所以不得不重新巩固下前端的基础知识,从最基本的学习还得额外的学习新知识,倍感压力之大. 昨天初略学习下git,算是自己学习的一个新知识.简单记录下,希望四海八荒的大神看过来,有错的请指导 ...