Fragment method not accessible from Activity

DarkMalice

So, I am back to trying to update the EditText content in a Fragment from the Activity. I should be getting the right fragment object from within the viewpager using its position, however the method called, drawSurname(surname), is still giving me 'Cannot resolve' error.

Relevant code from FormActivity;

//initialise pager for swipe screens
        ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
        adapter = new MyPagerAdapter(getSupportFragmentManager());
        pager.setAdapter(adapter);

public void DrawText() {
        // Fetching user details from sqlite
        HashMap<String, String> user = db.getUserDetails();

        if (user.size() != 0) {
            Log.e(TAG, "string surname: " + surname);

            // Displaying the user details on the screen
            Fragment n = adapter.getRegisteredFragment(0);
            n.drawSurname(surname);

        }else{
            Log.e(TAG, "table empty");
        }
    }

public class MyPagerAdapter extends FragmentStatePagerAdapter {
        SparseArray<Fragment> registeredFragments = new SparseArray<Fragment>();

        public MyPagerAdapter(FragmentManager fm) {

            super(fm);
        }

        @Override
        public Fragment getItem(int pos) {
            switch(pos) {

                case 0: return FirstFragment.newInstance("@string/form_instruct");
                case 1: return SecondFragment.newInstance("SecondFragment, Instance 1");
                case 2: return ThirdFragment.newInstance("ThirdFragment, Instance 1");
                case 3: return FourthFragment.newInstance("FourthFragment, Instance 1");
                //case 4: return FifthFragment.newInstance("ThirdFragment, Instance 3");
                default: return FirstFragment.newInstance("FirstFragment, Default");
            }
        }

        @Override
        public int getCount() {
            return 4;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            Fragment fragment = (Fragment) super.instantiateItem(container, position);
            registeredFragments.put(position, fragment);
            return fragment;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            registeredFragments.remove(position);
            super.destroyItem(container, position, object);
        }

        public Fragment getRegisteredFragment(int position) {
            return registeredFragments.get(position);
        }
    }

FirstFragment.java

public class FirstFragment extends android.support.v4.app.Fragment {

    //create variables & Logcat tag
    private static final String TAG = FirstFragment.class.getSimpleName();
    private EditText inputTitle;
    private EditText inputName;
    private EditText inputSurname;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_first, container, false);

        //set inputs for fields
        inputTitle = (EditText) v.findViewById(R.id.titleText);
        inputName = (EditText) v.findViewById(R.id.foreText);
        inputSurname = (EditText) v.findViewById(R.id.surnameText);

        TextView tv = (TextView) v.findViewById(R.id.tvFragFirst);
        tv.setText(getArguments().getString("msg"));

        inputSurname = (EditText) getActivity().findViewById(R.id.surnameText);

        return v;
    }
    public static FirstFragment newInstance(String text) {

        FirstFragment f = new FirstFragment(text);
        Bundle b = new Bundle();
        b.putString("msg", text);

        f.setArguments(b);

        return f;
    }

    public void drawSurname(String x) {

        inputSurname.setText(x);
    }
}

I was sure I'd got the fragment correctly with getRegisteredFragment(), but cannot see why the method wouldn't call otherwise.

user4624062

The problem comes from the fact that Fragment class is not supposed to have a function called drawSurname but FirstFragment is. To solve this create an abstract class extending Fragment called SuperFrag from which all your custom fragments will inherit. In SuperFrag add a function called drawSurname. You can override it in your inheriting classes. Finally SparseArray should be a list of SuperFrag

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From

Call an activity method from a fragment

From Dev

Call Fragment method from Activity

From Dev

Call method in fragment from another fragment/activity

From Dev

How to call an activity method from a fragment?

From Dev

How to call ViewHolder method from Activity/Fragment

From Dev

NullPointerException when calling a Fragment method from Activity?

From

Calling a Fragment method from a parent Activity

From Dev

calling parent activity method from fragment

From Dev

NullPointer when calling Fragment method from activity

From Dev

Call a method in a drawer fragment from the base activity

From Dev

Call fragment method from activity but view is null

From Dev

Call Tabbed Fragment method from Activity

From Dev

How call method from Fragment in Activity?

From Dev

Access ViewPager Fragment method from Activity

From Dev

Calling fragment method from activity Android

From Dev

Accessing a tabbed fragment method from the parent activity?

From Dev

Call method from Main Activity into Fragment Adapter

From Dev

How to Call Method in Fragment From Parent Activity?

From Dev

Call method in child fragment in parent fragment from activity

From Dev

Call fragment method inside an activity from another activity

From Dev

Trying to call a fragment method from parent activity but the method is not being resolved

From Dev

NavHostFragment not accessible from fragment in android

From Dev

How to disable an onDestroy method from activity in a specific fragment?

From Dev

Access a ViewPager Fragment's public method from the main Activity

From Dev

Call method of interface implements with child fragment From container activity Android

From Dev

Calling Fragment method from Activity,findFragmentById returns null

From Dev

Calling Fragment's method from Activity doesn't work?

From Dev

Does Activity method called from Fragment run in the UI thread?

From Dev

implement abstract method while moving from activity to fragment

Related Related

  1. 1

    Call an activity method from a fragment

  2. 2

    Call Fragment method from Activity

  3. 3

    Call method in fragment from another fragment/activity

  4. 4

    How to call an activity method from a fragment?

  5. 5

    How to call ViewHolder method from Activity/Fragment

  6. 6

    NullPointerException when calling a Fragment method from Activity?

  7. 7

    Calling a Fragment method from a parent Activity

  8. 8

    calling parent activity method from fragment

  9. 9

    NullPointer when calling Fragment method from activity

  10. 10

    Call a method in a drawer fragment from the base activity

  11. 11

    Call fragment method from activity but view is null

  12. 12

    Call Tabbed Fragment method from Activity

  13. 13

    How call method from Fragment in Activity?

  14. 14

    Access ViewPager Fragment method from Activity

  15. 15

    Calling fragment method from activity Android

  16. 16

    Accessing a tabbed fragment method from the parent activity?

  17. 17

    Call method from Main Activity into Fragment Adapter

  18. 18

    How to Call Method in Fragment From Parent Activity?

  19. 19

    Call method in child fragment in parent fragment from activity

  20. 20

    Call fragment method inside an activity from another activity

  21. 21

    Trying to call a fragment method from parent activity but the method is not being resolved

  22. 22

    NavHostFragment not accessible from fragment in android

  23. 23

    How to disable an onDestroy method from activity in a specific fragment?

  24. 24

    Access a ViewPager Fragment's public method from the main Activity

  25. 25

    Call method of interface implements with child fragment From container activity Android

  26. 26

    Calling Fragment method from Activity,findFragmentById returns null

  27. 27

    Calling Fragment's method from Activity doesn't work?

  28. 28

    Does Activity method called from Fragment run in the UI thread?

  29. 29

    implement abstract method while moving from activity to fragment

HotTag

Archive