ImageView button in Fragment does not work

1

I'm trying to make an ImageView within a fragment that when I clicked send you another Activity. The problem is that when clicking nothing happens, but I do not get any error to know what is happening.

This is the code of the Fragment.java

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

    return view;
}

private ProfileFragment mContext = ProfileFragment.this;

private void setupToolbar(){
    Toolbar toolbar = (Toolbar) getView().findViewById(R.id.profileToolBar);
    ((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);

   ImageView profileMenu = (ImageView) getView().findViewById(R.id.profileMenu);
    profileMenu.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.d(TAG, "onClick: navigating to account settings.");
            Intent intent = new Intent(getActivity(), AccountSettingsActivity.class);
            startActivity(intent);
        }
    });
}
    
asked by Theo Rap 10.07.2017 в 01:39
source

2 answers

0

Okay, look, I do not understand what makes code that seems to be your Activity in your Fragment, but I'll explain how you should be able to do it ...

Fragment.java:
    @Override     public View onCreateView (LayoutInflater Inflator, ViewGroup container, Bundle savedInstanceState) {       View view = inflater.inflate (R.layout.fragment_profile, container, false);       ImageView profileMenu = (ImageView) view.findViewById (R.id.profileMenu);       profileMenu.setOnClickListener (new View.OnClickListener () {           @Override           public void onClick (View view) {                 Log.d (TAG, "onClick: navigating to account settings.");                 Intent intent = new Intent (getActivity (), AccountSettingsActivity.class);                 startActivity (intent);           }       });       return view;     }

Now, as you are explaining it, I suppose that your ImageView is inside the XML of your Fragment, otherwise it is because you want to use it in the wrong place, in Activity everything that is in the XML of the Activity and in the Fragment all of the XML of the Fragment.
I hope I have helped you although I repeat, I do not understand your code very much, it seems as if you mix code of your ACtivity with the Fragment and that should not be done, I do not know if I misunderstood or if your mistake is there.

    
answered by 10.07.2017 в 04:15
-1

You are not calling anywhere in your code the listener initialization of ImageView to open the Activity . To do what you want you can call the setupToolbar() method from onCreateView() in this way your ImageView will configure its listener to open the% Activity% co act.

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

    setupToolbar(); //* Inicializa listener de ImageView.

    return view;
}
    
answered by 15.10.2017 в 21:12