0
  1. My requirement is to load a fragment when I click the ClickableSpan.

  2. I get the error Variable "inflater" is accessed within the inner class, needs to be declared final.

  3. BTW I've created a Tabbed Activity with Action Bar tabs, where I've two tabs with two different fragments(Fragment A and Fragment B in the two tabs) and Fragment C. When I click the ClickableSpan from Fragment A, It should navigate me to fragment

public static class PlaceholderFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = null;

        switch (getArguments().getInt(ARG_SECTION_NUMBER)) {
            case 1:
                rootView = inflater.inflate(R.layout.fragment_customer, container, false);
                TextView register = rootView.findViewById(R.id.customer_register);
                String register_text = "You are not a member? Register";

                SpannableString spanableObject = new SpannableString(register_text);
                ClickableSpan clickableSpan = new ClickableSpan() {
                    @Override
                    public void onClick(View widget) {
                        Toast.makeText(getActivity(), "Clicked", Toast.LENGTH_SHORT).show();
                        rootView = inflater.inflate(R.layout.fragment_sign_up, container, false);
                    }

                    @Override
                    public void updateDrawState(TextPaint ds) {
                        super.updateDrawState(ds);
                        ds.setColor(Color.BLUE);
                    }
                };

                spanableObject.setSpan(clickableSpan, 22, 30, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                register.setText(spanableObject);

                register.setMovementMethod(LinkMovementMethod.getInstance());
                break;
            case 2:
                rootView = inflater.inflate(R.layout.fragment_contractor, container, false);
                break;
        }
}

Kindly help me resolve this issue. Million thanks in advance!!

  • The only problem you have mentioned is the need to declare `inflater` as `final`. We do not know where `inflater` is being declared, as that is not in your question. Presumably, it is a local variable in the current method, or perhaps a parameter to the method. If so, put `final` before it. See [Wikipedia](https://en.wikipedia.org/wiki/Final_%28Java%29) and [this SO question](https://stackoverflow.com/q/15655012/115145) to learn more about `final`. – CommonsWare Jun 14 '20 at 19:40
  • @CommonsWare My requirement to make a fragment call upon a clickable text(link). Since i'm using a tabbed activity(Action bar) two tabs for login, I'm supposed to inflate my layout inside the static PlaceHolderFragment class. So when i click the text that is clickable(link) in the fragment A. It should navigate me to the fragment B. My error now is: When I have the above code inside the onCreateView which is under static class PlaceHolderFragment class, I get the above mentioned error. Kindly let me know how to navigate to other fragment from the tabbed activity upon link click. Thanks! – Sapthagiri Manivannan Jun 14 '20 at 21:56
  • The code looks like the above – Sapthagiri Manivannan Jun 14 '20 at 21:58
  • @SapthaClans: To clear up the error message, put the `final` keyword before your first parameter: `public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)`. – CommonsWare Jun 14 '20 at 22:08
  • @CommonsWare Now when I add final in that place, I get the same error for rootView and Container in the same line. – Sapthagiri Manivannan Jun 15 '20 at 05:28
  • @Zain I'm new to android and not sure how to do that. It would be great if you would help me. Million thanks! – Sapthagiri Manivannan Jun 15 '20 at 05:32
  • @CommonsWare I've made the container also final and made the rootView an array according to the IDE suggestion. The app crashes now when the link is linked. – Sapthagiri Manivannan Jun 15 '20 at 05:59
  • "The app crashes now when the link is linked." -- use Logcat to examine the stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this If you do not understand what the stack trace is telling you, ask a separate Stack Overflow question, with a current [mcve] and the contents of the stack trace. – CommonsWare Jun 15 '20 at 10:46

0 Answers0