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 ...
随机推荐
- win8 64位+Oracle 11g 64位下使用PL/SQL Developer 的解决办法
1)安装Oracle 11g 64位2)安装32位的Oracle客户端( instantclient-basic-win32-11.2.0.1.0)下载 instantclient-basic-win ...
- Centos6.5 mysql折腾记
1.yum安装mysql [root@localhost ~]# yum -y install mysql-server 安装结果 Installed: mysql-server.x86_64 0:5 ...
- android Fragment和FragmentActivity
MainActivity.java import android.app.AlertDialog; import android.app.Notification; import android.co ...
- 深入探讨 ECMAScript 规范第五版
深入探讨 ECMAScript 规范第五版 随着 Web 应用开发的流行,JavaScript 越来越受到开发人员的重视.作为 ECMAScript 的变体,JavaScript 语言的很多语法特性都 ...
- iOS多线程——GCD与NSOperation总结
很长时间以来,我个人(可能还有很多同学),对多线程编程都存在一些误解.一个很明显的表现是,很多人有这样的看法: 新开一个线程,能提高速度,避免阻塞主线程 毕竟多线程嘛,几个线程一起跑任务,速度快,还不 ...
- ES1:Windows下安装ElasticSearch
ElasticSearch(简称ES)是一个基于Lucene的分布式全文搜索服务器,本随笔演示在Windows安装ElasticSearch和用于管理ES的Head插件. ElasticSearch官 ...
- 最近一年多我总结的常用mate标签-常用mate标签
昨天开始上班 ,今天晚上不是太忙 ,来写篇博客了 meta元素共有三个可选属性(http-equiv.name和scheme)和一个必选属性(content),content定义与 http-equ ...
- linux c语言定时器
原文来自于:http://hi.baidu.com/opetrhsxszbckzd/item/126966cae5f9524aa9ba94f5 我只是把其重新排版标注一下. linux c语言定时器 ...
- 判断iPhone的WiFi是否打开的两种方法 之是否连接上 WiFi
iOS中用来查询当前连接的网络信息的API即CNCopyCurrentNetworkInfo 这个API位于SystemConfiguration.framework里面,使用时需要增加.h和包含库文 ...
- 单页应用SPA的路由
关于单页应用 单页Web应用(single page web application,SPA),就是只有一张Web页面的应用,是加载单个HTML 页面并在用户与应用程序交互时动态更新该页面的Web应用 ...