尝试实现其他类中的方法

时间:2019-08-04 21:37:05

标签: android android-tablayout

我正在使用TabLayout在应用程序的不同选项卡之间切换。该应用程序的该部分有效,但是,我必须创建一个新类来处理计算器选项卡。我有计算器可用的代码,但是,我不知道如何调用从calculatorfunctionFragmentMainActivity的方法。

我尝试使用“主要活动”中的代码: 调用CalculatorFunctionFragment

        calculatorfunctionFragment cls2 = new calculatorfunctionFragment();

        cls2.startActivity(getIntent());

,我什至尝试添加cls2.getIntent() 两者都不起作用。

public class MainActivity extends AppCompatActivity {

    //declaring the Tab layout and other app functions for the app
    public TabLayout tabLayout;
    public AppBarLayout appBarLayout;
    public ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Now making the tab and appBar layout and viewPager active
        tabLayout = (TabLayout) findViewById(R.id.tablayout_id);
        appBarLayout = (AppBarLayout) findViewById(R.id.appabar);
        viewPager = (ViewPager) findViewById(R.id.viewpager_id);

        ViewPageAdapter adapter = new ViewPageAdapter(getSupportFragmentManager());
        //adding Fragment and Titles for the ViewPagerAdapter to find and populate
        adapter.AddFragment(new calculatorFragment(),"Calculator Page");
        adapter.AddFragment(new formulapageFragment(),"Formula Page");
        adapter.AddFragment(new webFragment(),"Web Page");


        // adapter Setup
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        //calling the CalculatorFunctionFragment

        calculatorfunctionFragment cls2 = new calculatorfunctionFragment();

        cls2.startActivity(getIntent());


    }

我希望Main Activity可以与计算器片段一起工作

1 个答案:

答案 0 :(得分:0)

朋友-您名为calculatorfunctionFragment的班级是扩展AppCompatActivity的班级。所以它不是一个片段,它是一个活动。您不能通过在活动上调用new来启动活动。

您似乎需要查看文档并通过一些“入门”指南进行工作,以了解活动如何工作以及如何启动它们。

review the official docs on Activities,然后向Google询问“如何开始新的活动”。

希望有帮助!