-3

enter image description here

I'm making app that has 3 fragment and 1 activity.

And I want

if Button at fragment named 'FragmentOper" is clicked,

two buttons in Activity named 'FragmentMain' is disabled.

I have been searched on the internet and found i simple way.

FragmentMain.java

public class FragmentMain extends AppCompatActivity {
    public static Button BtnInit, btnOper, btnGraph;

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        btnInit = (Button) findViewById(R.id.button1);
        btnOper = (Button) findViewById(R.id.button2);
        btnGraph = (Button) findViewById(R.id.button3);

        setContentView(R.layout.activity_fragment); // added
        FragmentMain.context = FragmentMain.this; // added
}
// added to stack Overflow codes
public void changeFragment(View view){
        int id = view.getId();
        Fragment fragment = null;

        switch (id){
            case R.id.button1:
            fragment = new FragmentInit();
            break;

            case R.id.button2:
                fragment = new FragmentOper();
                break;

            case R.id.button3:
                fragment = new FragmentGraph();
                break;
        }

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.fragment, fragment);
        fragmentTransaction.commit();
    }

public void changeState(boolean state){
        FragmentMain.btnGraph.setEnabled(state);
        FragmentMain.btnInit.setEnabled(state);
   }
}

FragmentOper.java

public class FragmentOper extends Fragment{
    public Button mStartBtn;

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {

        View myView = inflater.inflate(R.layout.fragment_oper, container, false);

        mStartBtn = (Button) myView.findViewById(R.id.startBtn);

mStartBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View myView) {

                if (isTimerRunning) // STOP
                {
                    mStartBtn.setText("START");
                    isTimerRunning = false;
                    timerHandler.removeCallbacks(timerRunnable);
                    ((FragmentMain)getActivity()).changeState(true);


                } else {
                    mStartBtn.setText("PAUSE"); // Working
                    startTimer();
                    ((FragmentMain)getActivity()).changeState(false);
                }
            }
        });
return myView;
}

But There is java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setEnabled(boolean)' on a null object reference error in changeState in 'FragmentMain'

When this app is started, I pressed btnINIT and btnGRAPH and it is worked.

And before I add ((FragmentMain)getActivity()).changeState(true); in onclicklistener, it works fine.

So i don't understand why there is an NullPointerException.

I have never declared Buttons in any other place.

How can it fixed?

문경욱
  • 53
  • 2
  • 9

1 Answers1

0

you are missing the

setContentView(R.layout.activity_main);

in your Activity and setContentView needs to be declared before the buttons are

Aiko West
  • 791
  • 1
  • 10
  • 30