0

I'm trying to use Intent inside PlaceholderFragment class of TabActivity's fragment to open next Activity.

Here is my code:

PlaceHolderFragment class

    public ImageButton Next;
     public View onCreateView(
            @NonNull LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)
    {
        View rootView=inflater.inflate(R.layout.fragment_main_tab,container,true);
        Next=(ImageButton)rootView.findViewById(R.id.btn_expand);
        Next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                updatedetails();
            }
        });

        View root=null;
        switch (getArguments().getInt(ARG_SECTION_NUMBER))
        {
            case 1:
                 root = inflater.inflate(R.layout.fragment_main_tab, container, false);
                break;
            case 2:
                    root = inflater.inflate(R.layout.fragment_team_screen_, container, false);
                    break;
        }
        return root;
    }
public void updatedetails(){
    Intent intent = new Intent(getActivity(), Enquiry_followup.class);
    startActivity(intent);
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

3 Answers3

0

Can you try the below. Last param of the inflater, make it false.

View rootView = inflater.inflate(R.layout.fragment_main_tab,container,false);

You should never pass attachToRoot as true when you are not responsible for adding the child view to parent.

You can read more about this from this answer @NOW OR NOT NOW

Anjana
  • 903
  • 1
  • 5
  • 13
0

getActivity() is not the best way for getting context in a fragment. And make the attachToRoot = false while creating the view Try the following code

public class yourClass{
public ImageButton Next;

 Context context;
@Override public void onAttach(Context context) { 
super.onAttach(context);
 this.context=context; 
}
     public View onCreateView(
            @NonNull LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)
    {
        View rootView=inflater.inflate(R.layout.fragment_main_tab,container,false);
        Next=(ImageButton)rootView.findViewById(R.id.btn_expand);
        Next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                updatedetails();
            }
        });

        View root=null;
        switch (getArguments().getInt(ARG_SECTION_NUMBER))
        {
            case 1:
                 root = inflater.inflate(R.layout.fragment_main_tab, container, false);
                break;
            case 2:
                    root = inflater.inflate(R.layout.fragment_team_screen_, container, false);
                    break;
        }
        return root;
    }
public void updatedetails(){
    Intent intent = new Intent(context, Enquiry_followup.class);
    startActivity(intent);
}
}

Try this and post the update.

Edit 1:Try returning rootView instead of root in onCreateView().

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Gowtham K K
  • 3,123
  • 1
  • 11
  • 29
0
public class TestFragment extends Fragment {

public ImageButton Next;

public View onCreateView(
        @NonNull LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main_tab, container, false);


    View root = null;
    switch (getArguments().getInt(ARG_SECTION_NUMBER)) {
        case 1:
            root = inflater.inflate(R.layout.fragment_main_tab, container, false);
            break;
        case 2:
            root = inflater.inflate(R.layout.fragment_team_screen_, container, false);
            break;
    }

    Next = (ImageButton) rootView.findViewById(R.id.btn_expand);
    Next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            updatedetails();
        }
    });

    return rootView;
}

public void updatedetails() {
    Intent intent = new Intent(getActivity(), Enquiry_followup.class);
    startActivity(intent);
}
}

Rearrange the code like this. And make sure both the xml file contains the ImageButton with the id - btn_expand

The issue with your code was that the onClickListener was before setting the view.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Milan Joseph
  • 118
  • 10
  • Thank you so much @MilanJoseph...It will help for me just by setting view. –  Jun 27 '19 at 09:57
  • After using your code my SWITCH CASE Is not working.I'm not able to see different fields in tab2.Only i can see same fields as present in tab 1 . @Milan Joseph –  Jun 27 '19 at 10:15
  • In switch I have given 1 hardcoded, Please provide your login. Then it will work. – Milan Joseph Jun 27 '19 at 12:41