我正在使用 TabLayout 在应用中的不同选项卡之间切换。该应用程序的那部分工作,但是,我必须创建一个新类来处理计算器选项卡。我有计算器工作的代码,但是,我不知道如何调用从 calculatorfunctionFragment
到 MainActivity
的方法。
我试过使用主 Activity 中的代码: 调用 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 与计算器 fragment 一起工作
最佳答案
friend - 你的名为 calculatorfunctionFragment
的类是一个扩展 AppCompatActivity
的类。所以它不是一个 fragment ,它是一个 Activity 。您不会通过调用 new
来启动 Activity。
您似乎需要查看文档并通过一些“入门”指南来了解 Activity 的工作原理以及启动方式。
请review the official docs on Activities然后谷歌搜索“如何开始新的 Activity ”。
希望对您有所帮助!
关于android - 尝试从不同的类实现方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57350372/