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?