java - Android中使用FragmentTransaction物件的add方法後應用閃退
仅有的幸福
仅有的幸福 2017-06-12 09:19:13
0
1
865

想在activity中進行fragment的設定和替換。 setDefaultFragment()方法中,使用transaction.add(R.id.layFrame, MapsFragment.newInstance("map","map"));
語句則套用閃退。註解掉此語句則正常運作。

應用截圖

#xml

    

Activity

public class MainActivity extends AppCompatActivity implements BottomNavigationBar.OnTabSelectedListener{ private ArrayList fragments; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //底部导航栏对象 BottomNavigationBar bottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar); //设置样式、颜色 bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC); bottomNavigationBar .setActiveColor(R.color.colorPrimary) .setInActiveColor("#FFFFFF") .setBarBackgroundColor("#ECECEC"); //添加导航项 bottomNavigationBar .addItem(new BottomNavigationItem(R.drawable.ic_location_on_white_24dp, "Location")) .addItem(new BottomNavigationItem(R.drawable.ic_tracks_on_white_24dp, "Tracks")) .setFirstSelectedPosition(0) .initialise(); //获取fragment列表,设定默认fragment fragments = getFragments(); setDefaultFragment(); //监听 bottomNavigationBar.setTabSelectedListener(this); } private ArrayList getFragments() { ArrayList fragments = new ArrayList<>(); fragments.add(MapsFragment.newInstance("Map","Map")); fragments.add(TracksFragment.newInstance(3)); return fragments; } private void setDefaultFragment(){ FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.add(R.id.layFrame, MapsFragment.newInstance("map","map")); transaction.addToBackStack(null); transaction.commit(); } @Override void init() { } @Override public void onTabSelected(int position) { } @Override public void onTabUnselected(int position) { } @Override public void onTabReselected(int position) { } }
仅有的幸福
仅有的幸福

全部回覆 (1)
给我你的怀抱

解決了,原來是fragment中有OnFragmentInteraction接口,而activity中未implement

    最新下載
    更多>
    網站特效
    網站源碼
    網站素材
    前端模板
    關於我們 免責聲明 Sitemap
    PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!