1

In one of my navigation drawer fragments (NewFragment), there are 3 tabs (Tab1NewFragment, Tab2NewFragment, Tab3NewFragment), when a button is clicked in Tab1NewFragment (btn_calculate), a method (set_outputs) is called in Tab2NewFragment. However it says fragment not attached to a context. What is the problem?

*BTW, the reason that I send activity when I call the set_outputs method (tab2NewFragment_obj.set_outputs(getActivity());) is that when i tried to get the activity from Tab2Newfragment itself, it produced NullPointerException but this solved it.

MainActivity:

TabLayout tabLayout = findViewById(R.id.tab_layout);
    tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
    tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
    tabLayout.addTab(tabLayout.newTab().setText("Tab 3"));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

    final ViewPager viewPager = findViewById(R.id.pager);
    MyPagerAdapter MyPagerAdapter = new MyPagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
    viewPager.setAdapter(MyPagerAdapter);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });

Tab1NewFragment:

btn_calculate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        tab2NewFragment_obj.set_outputs(getActivity()); **//line478**
        }
    });

Tab2NewFragment:

        public class Tab2NewFragment extends Fragment {
        TextView frame_size_result;
        //...


        public Tab2NewFragment() {
        // Required empty public constructor
         }
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup                  container, Bundle savedInstanceState) {

    view = inflater.inflate(R.layout.tab2_new_fragment,
            container, false);
      //find views
      result_bmi_textView =  view.findViewById(R.id.result_bmi_textView);
      //...

  return view;
   }
    public void set_outputs(Context context) {
         tab1_new_fragment_prefs = context.getSharedPreferences("input_prefs", Context.MODE_PRIVATE);
  if (frame_size > 10.4) {
                    frame_size_result.setText(getString(R.string.frame_size_small)); **//line 279**
                    if (formula_type == 1) {
                        bmr = mainActivity_obj.one_digit((66.5 + (13.75 * weight_value) + (5.003 * height_value) - (6.755 * age_value)) * 90 / 100); 
                    } else if (formula_type == 2) {
                        bmr = mainActivity_obj.one_digit(((10 *        weight_value) + (6.25 * height_value) - (5 * age_value) + 5) * 90 / 100);
                    }
                }
        }

Error:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: mydietitian.milad, PID: 1579
              java.lang.IllegalStateException: Fragment Tab2NewFragment{3b28efa} not attached to a context.
                  at android.support.v4.app.Fragment.requireContext(Fragment.java:696)
                  at android.support.v4.app.Fragment.getResources(Fragment.java:760)
                  at android.support.v4.app.Fragment.getString(Fragment.java:782)
                  at mydietitian.milad.Tab2NewFragment.set_outputs(Tab2NewFragment.java:279)
                  at mydietitian.milad.Tab1NewFragment$11.onClick(Tab1NewFragment.java:478)
                  at android.view.View.performClick(View.java:5697)
                  at android.widget.TextView.performClick(TextView.java:10826)
                  at android.view.View$PerformClick.run(View.java:22526)
                  at android.os.Handler.handleCallback(Handler.java:739)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:158)
                  at android.app.ActivityThread.main(ActivityThread.java:7224)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
      I/Process: Sending signal. PID: 1579 SIG: 9
      Application terminated.
bopujuziha
  • 11
  • 2

1 Answers1

0

The simple approach I came across is to

Change

getActivity().getApplicationContext();

To

getContext();

To understand how to deal with the context effectively please refer to Difference between getContext() , getApplicationContext() , getBaseContext() and "this"

paulobunga
  • 277
  • 6
  • 13