Issues in implementing GridView within a fragment

后端 未结 1 1699
灰色年华
灰色年华 2021-01-20 16:17

I am working on a small application which contains Activity with NavigationDrawer, and i have placed a fragment on that activity using FragmentManager

相关标签:
1条回答
  • 2021-01-20 16:28

    You're not respecting the Activity lifecycle. When you do this

    FragmentManager FM = getFragmentManager();
    FragmentTransaction Trans = FM.beginTransaction();
    

    before the method onCreate being called, you don't have the Activity properly created.

    Move that code to the onCreate method:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.navigation_drawer);
        AddApps Aapps = new AddApps();
        drawerlayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        LVdrawer = (ListView) findViewById(R.id.left_drawer);
        LVdrawer.setAdapter(new CustomAdapter(this, 1));
    
        FragmentManager FM = getFragmentManager();
        FragmentTransaction Trans = FM.beginTransaction();
    
        Trans.add(R.id.content_frame, Aapps, "AddAppsF");
        Trans.addToBackStack("AddApps");
        Trans.commit();
    }
    
    0 讨论(0)
提交回复
热议问题