0

i need some help. I'm making an app on android studio and I have a Button initialized in a Fragment. I need to call this button in another Activity and set a click listener on it.

The problem is that when I do this I get the .NullPointerExceptionError and it says that the value of the object is null.

Now i tried to initialize the button in both my fragment and Activity but I keep getting that error and the app crashes.

I need some help to call this button from the Fragment. Maybe my question is similar to some other but it's hours that I'm searching a solution to this problem and I found nothing.

Thanks in advance for all your help

Zain
  • 37,492
  • 7
  • 60
  • 84
Berna
  • 3
  • 4
  • Why would you want to add a click listener on a button which is in a Fragment from another Activity? That Fragment is not visible, right? – Nikhil Bansal Aug 17 '21 at 16:52
  • @NikhilBansal I need to do that because that button will call another activity and I know I could do that from the fragment but I prefer to do that from another activity (that activity is the one that switches the fragments). If u know how to do this It will be very useful Thanks :) – Berna Aug 17 '21 at 17:19
  • So, you have Activity A which is showing a Fragment A. Fragment A shows a button in its UI, and on click of this button, another Activity B will open. Am I right? – Nikhil Bansal Aug 17 '21 at 17:23
  • @NikhiBansal Yes, you're completely right but I need to find a way to set the ClickListener of the button in the Activity A. But I dont know how to do that because even if i initialize the Button in the Activity, I get NullPointerException Error. The problem is with initialization: with debug console I see that in the Fragment the button is correctly initialized but in the Activity it has a null value. And that's why I can't set the OnCLickListener. :( I don't know how to solve this – Berna Aug 17 '21 at 17:28
  • show more detail code about your Fragment – shuabing Aug 18 '21 at 03:02

1 Answers1

1

Since the button is part of the fragment, it only fits that any interactions/modifications to the button be defined in the Fragment's scope itself.

There are 2 ways to go about it:

  1. Create the click listener in the Fragment itself and navigate to another activity from there.

  2. Create an interface which your activity implements and the fragment has an instance of that interface. On the button click, call the interface's method. That would give you control in the activity which is hosting the fragment.

    MyActivity extends AppCompatActivity implements MyClickListener {
    
        void onMyButtonClick() {
           // Navigate to another activity here
        }
    }
    
    
    MainFragment extends Fragment {
    
        private MyClickListener listener;
    
        void onAttach(Context context) {
            listener = (MyClickListener) context;
            // Now call listener.onMyButtonClick() from button's on click listener.
        }
    }
    
    interface MyClickListener {
        void onMyButtonClick();
    }
    
Nikhil Bansal
  • 187
  • 2
  • 11
  • Sorry for the questions, but what is MyClickListener? Because If i write it it gives me error. Then the void onAttach it's never used. And the second comment that u wrote with onMyButtonClick u mean OnClick()? If you have some time could you re-write that code because I can't get It as it's written. Sorry for the inconvenience but I'm so confused with this and now a little desperate. If you cant't re-write it is fine. Thanks anyways for your help :) – Berna Aug 17 '21 at 18:08
  • @FrancescoBernardi The `MyClickListener` is just an interface you will have to define. I have updated the code sample. – Nikhil Bansal Aug 18 '21 at 03:16