Tamil Selvan Tamil Selvan - 1 year ago 98
Android Question

Calling a fragment from Navigational Drawer

I am Having a Navigational Drawer which Consists of n number of menus on clicking the menu i need to open a particular fragment ,First i tried it with activities and intent it doesnt look good,in case of using fragments ,im Selecting them by id and calling the fragment from an activity ,In the fragment im using piecharts in which it is throwing null pointer exceptions.Ive declared the two pie charts separately and im using getView() since it is fragment.

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

pieChart1 = (PieChart) getView().findViewById(R.id.piechart1);
pieChart2 = (PieChart) getView().findViewById(R.id.piechart2);`

Answer Source

Here is a sample code that will help to understand. First replace the activity_main.xml contents with a fragment in OnCreate():

 FragmentManager fm = getFragmentManager();
        fm.beginTransaction().replace(R.id.content_frame, new HomeFragment()).commit();

Then replace this HomeFragment with others in onNavigationItemSelected():

@Override
    public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_home) {
        // Handle the camera action
        FragmentManager fm = getFragmentManager();
        fm.beginTransaction().replace(R.id.content_frame, new HomeFragment()).commit();
    } else if (id == R.id.nav_current_order) {
        FragmentManager fm = getFragmentManager();
        fm.beginTransaction().replace(R.id.content_frame, new MyCurrentOrderFragment(),"Tag").commit();
    } else if (id == R.id.nav_received_images) {
        FragmentManager fm = getFragmentManager();
        fm.beginTransaction().replace(R.id.content_frame, new HistoryFragment()).commit();
    } else if (id == R.id.nav_rate_app) {
        FragmentManager fm = getFragmentManager();
        fm.beginTransaction().replace(R.id.content_frame, new RateOurAppFragment()).commit();
    } else if (id == R.id.nav_help) {
        FragmentManager fm = getFragmentManager();
        fm.beginTransaction().replace(R.id.content_frame, new HelpFragment()).commit();
    } else if (id == R.id.nav_settings) {
        FragmentManager fm = getFragmentManager();
        fm.beginTransaction().replace(R.id.content_frame, new SettingsFragment()).commit();
    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

Add a FrameLayout in your content_main.xml and set it's id to "content_frame"

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="bd.com.chalo.dsstudio.MainActivity"
    tools:showIn="@layout/app_bar_main">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:visibility="gone" />
<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></FrameLayout>

</RelativeLayout>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download