-1

enter image description here

Hi

I'am making an app which has a 1 main activity and 3 fragments.

In activity, there is a Fragment to show another fragments

And it changes by clicking button in main activity.

There is a fragment that has a button.

I want when Button in fragment is pressed, buttons in activity will be disabled.

How to Enable/ Disable button from another fragment in android?

There is an similar question and answer, so i have tried this.

UpdateButtonListener.java

public interface UpdateButtonListener {
    void onUpdate(boolean status);
}

MainActivity.java

public class MainActivity extends AppCompatActivity implements UpdateButtonListener{
public static UpdateButtonListener updateButton;
@Override
    public void onCreate(Bundle savedInstanceState) {
updateButton = this;
}

 @Override
    public void onUpdate(boolean status) {
        mBtn1.setEnabled(false);
        mBtn3.setEnabled(false);

    }
}

Fragment1.java

public class Fragment1 extends Fragment{
 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
View myView = inflater.inflate(R.layout.fragment_oper, container, false);

fBtn1 = (Button) myView.findViewById(R.id.fBtn1);
//fbtn1 = mStopBtn      
mStopBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View myView) {
                MainActivity.updateButton.onUpdate(false);
            }
        });

}

And when i run the app, there is an error

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setEnabled(boolean)' on a null object reference at com.example.samplecode4_3demo.FragmentMain.onUpdate(MainActivity.java:379) at com.example.samplecode4_3demo.FragmentOper$2.onClick(Fragment1.java:124)

I thnk this is because i tried to change button in activity.

How can i solve this porblem?

문경욱
  • 53
  • 2
  • 9

2 Answers2

0

are you already try yourbutton.setvisible(false)?

  • I used button.setVisibility(Vew.INVISIBLE); and It has an error Not allowed to start service Intent {}: app is in background uid UidRecord{cee171a u0a404 TPSL idle change:idle|cached procs:1 proclist:18123, seq(0,0,0)} – 문경욱 Sep 02 '19 at 02:28
0

The Problem

You have not initialized your variables. mBtn1 and mBtn3 are null as they have not been set correctly.

The Quick Solution

Initialize your variables:

public class MainActivity extends AppCompatActivity implements UpdateButtonListener {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        updateButton = this;
        setContentView(R.layout.this_activity_layout); // Set layout with views
        mBtn1 = findViewById(R.id.btn1); // Find Button 1
        mBtn3 = findViewById(R.id.btn3); // Find Button 3
    }
}

The Complete Solution

After doing the above, please read: https://developer.android.com/training/basics/fragments/communicating on how to properly communicate with activity via fragment. Then:

  1. Remove public static UpdateButtonListener updateButton; (you should not have a static reference to a button).
  2. Update fragment button click as so:

    mStopBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View myView) {
            // MainActivity.updateButton.onUpdate(false); // Don't access STATIC reference like this
    
            // Instead get the activity INSTANCE and invoke method on it
            UpdateButtonListener listener = (UpdateButtonListener) getActivity();
            if (listener != null) {
                listener.onUpdate(false);
            }
        }
    });
    

Hope that helps!

dominicoder
  • 9,338
  • 1
  • 26
  • 32