能否片段延长片段活动? [Android版] [英] Can a fragment extend a fragment activity? [Android]
问题描述
我创建了由3片这是滚动(通过使用ViewPager)一MainActivity。现在,这些3选项卡中的一个片段。另外,我使用ActionBarSherlock(ABS)。
有关第一片段,我创建了以下类:
公共类Fragment_1扩展SherlockFragment {
@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){
// TODO自动生成方法存根
视图V = inflater.inflate(R.layout.fragment1,集装箱,FALSE);
返回伏;
/ * R.layout.fragment1只包含一个TextView。 * /
}
}
对于第二个片段,我希望延长FragmentActivity如下图所示。
公共类Fragment_2扩展FragmentActivity {
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.fragment2);
}
}
第三片段是相同的第一个。
但是,当我启动应用程序,它崩溃。所以,我错了在类Fragment_2延伸FragmentActivity?它是非法的扩展FragmentActivity甚至是活动的情况下碎片班?如果不是,这里有什么问题吗?
感谢。
编辑:后@ CommonsWare的答案,我更新了我的类,如下所示:
公共类Fragment_2扩展SherlockFragment {
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
按钮发送=(按钮)findViewById(R.id.bSend); //错误在该行
//一些无差错code
FragmentTransaction T = getSupportFragmentManager()调用BeginTransaction()。 //错误在该行
}@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){
// TODO自动生成方法存根
视图V = inflater.inflate(R.layout.fragment2,集装箱,FALSE);
返回伏;
}
我的最后两个问题是:
- 我应该如何访问我R.id.bSend按钮在Fragment_2类。
- Eclipse是给的建议,改变getSupportFragmentManager()来getFragmentManager()。是变化吧?
谢谢!
它是非法的扩展FragmentActivity甚至是活动的情况下碎片班?
块引用>
片段
是一个Java类。您的片段实现(用作网页上的ViewPager
)必须继承片段
,直接或间接的影响。
活动
不会从片段
继承。FragmentActivity
不会从片段
继承。因此,你不能从活动
或FragmentActivity
继承并以某种方式也的从<$ C继承$ C>片段。I have created a MainActivity which consists of 3 tabs which are scrollable (by using ViewPager). Now each of these 3 tabs is a Fragment. Also, I am using ActionBarSherlock (ABS).
For the 1st Fragment, I have created the following class:
public class Fragment_1 extends SherlockFragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub View v = inflater.inflate(R.layout.fragment1, container, false); return v; /* R.layout.fragment1 only contains a TextView. */ } }
For the 2nd Fragment, I want to extend a FragmentActivity as shown below.
public class Fragment_2 extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment2); } }
The 3rd Fragment is same as the first one.
But when I launch the app, it crashes. So, am I wrong in extending FragmentActivity for the class Fragment_2? Is it illegal to extend a FragmentActivity or even an Activity to fragment classes? If not, what is the problem here?
Thanks.
EDIT: After @CommonsWare's answer I updated my class as follows:
public class Fragment_2 extends SherlockFragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Button send = (Button) findViewById(R.id.bSend); //Error at this line //some error free code FragmentTransaction t = getSupportFragmentManager().beginTransaction(); //Error at this line } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub View v = inflater.inflate(R.layout.fragment2, container, false); return v; }
My final two questions are:
- How should I access my R.id.bSend Button in the Fragment_2 class.
- Eclipse is giving the suggestion to change getSupportFragmentManager() to getFragmentManager(). Is that change all right?
Thanks!
解决方案Is it illegal to extend a FragmentActivity or even an Activity to fragment classes?
Fragment
is a Java class. Your fragment implementations (for use as pages in yourViewPager
) must inherit fromFragment
, directly or indirectly.
Activity
does not inherit fromFragment
.FragmentActivity
does not inherit fromFragment
. Hence, you cannot inherit fromActivity
orFragmentActivity
and somehow also inherit fromFragment
.这篇关于能否片段延长片段活动? [Android版]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!