能否片段延长片段活动? [Android版]片段、Android

由网友(思戀知幻即離)分享简介:我创建了由3片这是滚动(通过使用ViewPager)一MainActivity。现在,这些3选项卡中的一个片段。另外,我使用ActionBarSherlock(ABS)。有关第一片段,我创建了以下类: 公共类Fragment_1扩展SherlockFragment {    @覆盖    公共查看onCreateVie...

我创建了由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);        }} 
正在使用智能手机不看后悔死,iOS 11和Android O中最好的新功能

第三片段是相同的第一个。

但是,当我启动应用程序,它崩溃。所以,我错了在类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 继承并以某种方式也的从片段。

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 your ViewPager) must inherit from Fragment, directly or indirectly.

Activity does not inherit from Fragment. FragmentActivity does not inherit from Fragment. Hence, you cannot inherit from Activity or FragmentActivity and somehow also inherit from Fragment.

阅读全文

相关推荐

最新文章