在activity中找到fragment组件的问题
public class MainActivity extends AppCompatActivity { private ViewPager fragmentContainer; private TabLayout tabs ; private Fragment_main fragment_main; private Fragment_shop fragment_shop; private Fragment_me fragment_me; private Fragment_CitySelect fragment_citySelect; Button btn_CitySelect; @Override protected void onStart() { super.onStart(); //初始化fragment_main中的组件 initFragmentView(); } private void initFragmentView() { //城市选择按钮 View view = this.fragment_main.getView(); btn_CitySelect = view.findViewById(R.id.btn_selecCity); btn_CitySelect.setOnClickListener(new Fragment_main_listener()); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity); initView(); setOnListener(); } private void setOnListener() { fragmentContainer.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabs)); tabs.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(fragmentContainer)); //main_fragment中的组件 } private void initView() { fragmentContainer = findViewById(R.id.container); tabs = findViewById(R.id.tabs); fragment_me = Fragment_me.newInstance(); fragment_main = Fragment_main.newInstance(); fragment_shop = Fragment_shop.newInstance(); final Fragment[] fragmentsList = new Fragment[]{fragment_main,fragment_shop,fragment_me}; fragmentContainer.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) { @Override public Fragment getItem(int i) { return fragmentsList[i]; } @Override public int getCount() { return fragmentsList.length; } }); } private class Fragment_main_listener implements View.OnClickListener{ @Override public void onClick(View v) { switch (v.getId()){ case R.id.btn_selecCity: fragment_main.getFragmentManager().beginTransaction().hide(fragment_main); fragment_main.getChildFragmentManager().beginTransaction() .add(R.id.main_fragment_main_menu, fragment_citySelect).commit(); } } } }
initFragmentView()函数中,fragment_main.findViewById()一直返回空值. 谢谢老师!
0
收起
正在回答 回答被采纳积分+1
2回答
1.Android 零基础入门
- 参与学习 人
- 提交作业 1789 份
- 解答问题 2907 个
Android大楼Java起,本阶段是Android攻城狮培养计划的第一部分语法与界面基础篇,将带大家从0开始入门Android开发。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星