0

I am beginning with Android and I have a NavigationView along with Tabular layout. I want to change the values in the tabs fragments based on the selection made in the navigation view. Here is my code-

MainActivity.java

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    Context mainActivityContext;
    Intent serviceIntent;

    SharedPreferences sharedPref;
    SharedPreferences.Editor editor;

    private SectionsPagerAdapter mSectionsPagerAdapter;
    private String valSelected = "";
    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        editor  = sharedPref.edit();

        mainActivityContext = this;

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);


        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());


        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);
        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


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

        if (id == R.id.sel1) {

            valSelected = "First";
            NotificationFragment fragment_obj = (NotificationFragment)getSupportFragmentManager().
                    findFragmentById(R.id.title_notification);

            fragment_obj.updateView();
        }


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





    public static class PlaceholderFragment extends Fragment {

        private static final String ARG_SECTION_NUMBER = "section_number";

        public PlaceholderFragment() {
        }


        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main2, container, false);
            TextView textView = (TextView) rootView.findViewById(R.id.section_label);

             textView.setText("HELLO");
            return rootView;
        }
    }




    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {

            if(position == 0) {
                return new NotificationFragment();

            }
            return PlaceholderFragment.newInstance(position + 1);
        }

        @Override
        public int getCount() {

            return 4;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "Tab1";
                case 1:
                    return "Tab2";
                case 2:
                    return "Tab3";
                case 3:
                    return "Tab4";


            }
            return null;
        }

        @Override
        public int getItemPosition(Object object) {
            if (object instanceof NotificationFragment) {
                ((NotificationFragment)object).updateView();
            }
            return super.getItemPosition(object);
        }
    }
}

This is my NotificationFragment NotificationFragment.java

public class NotificationFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);

        View rootView = inflater.inflate(R.layout.notification_layout, container, false);
        TextView textView = (TextView) rootView.findViewById(R.id.title_notification);

        textView.setText(MainActivity.coinSelected.toUpperCase());
        return rootView;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    public void updateView(){
        TextView text = (TextView) this.getView().findViewById(R.id.title_notification);
        text.setText(MainActivity.valSelected);
    }

}

But when I try to update the value in my MainActivity using

NotificationFragment fragment_obj = (NotificationFragment)getSupportFragmentManager().
                    findFragmentById(R.id.title_notification);

it gives me null.

Edit Here is the notification layout file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >


    <TextView
        android:id="@+id/title_notification"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="BTC/USD"
        android:textSize="26dp"/>

</LinearLayout>
Noober
  • 1,516
  • 4
  • 22
  • 48
  • check this updateData(); name use updateView() – MinnuKaAnae Dec 06 '17 at 08:32
  • ``findFragmentById(R.id.title_notification);`` in this code ``R.id.title_notification`` seems to be a textView. it should be the container id of the fragment – Emil Dec 06 '17 at 09:22
  • @Emil Yes it is the id of the textView. Please look at the edit. – Noober Dec 06 '17 at 09:34
  • @Noober ``findFragmentId()`` -> _Finds a fragment that was identified by the given id either when inflated from XML or as the container ID when added in a transaction_. ``R.id.title_notification`` doesn't represent a fragment or its container – Emil Dec 06 '17 at 09:38
  • @Emil I tried putting an id to my Linearlayout and using that but still I get null pointer exception – Noober Dec 06 '17 at 09:42
  • @Noober read this https://stackoverflow.com/questions/18609261/getting-the-current-fragment-instance-in-the-viewpager – Emil Dec 06 '17 at 10:04

2 Answers2

0

I faced this problem once when in my MainActivity.java i was importing android.support.v4.app.Fragment; and in my other class i was importing android.app.Fragment. I changed it to the same thing and findFragmentById(R.id) now returned the right fragment.

0

in your code NotificationFragment fragment_obj = (NotificationFragment)getSupportFragmentManager(). findFragmentById(R.id.title_notification); R.id.title_notification must be the id of fragment,but in your xml,it's a textview

kelo
  • 83
  • 1
  • 6