-1

enter image description here

As a image, I'm making app using fragment.

I want when btn1 is clicked, btn2 is disabled.

Here are codes Aclass.JAVA

mBtn1 = (Button) myView.findViewById(R.id.button1);
mStartBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View myView) {
                    BClass.button2.setEnabled(false);

                }
            }
        });

Bclass.JAVA

public static Button mBtn2;
mBtn2 = (Button) findViewById(R.id.button2);

There is similar question

Access a button from another class in android

But it makes function and i don't get it.

How can i solve this problem?

********** edited *************

When i click btn1

There is an error

Attempt to invoke virtual method 'void android.widget.Button.setEnabled(boolean)' on a null object reference.

I want to solve this problem

문경욱
  • 53
  • 2
  • 9

1 Answers1

0

IF you are trying hard to understand. Try this one;

I am sending a text to another activity that says disable

Aclass.JAVA

mBtn1 = (Button) myView.findViewById(R.id.button1);
mStartBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View myView) {
                Intent ii = new Intent(Aclass.this,Bclass.class);
                ii.putExtra("status","disable");
                startActivity(ii);
            }
        }
    });

In your Bclass.JAVA

I am getting the text that i send from previous activity and check if it equals

  Intent ii = getIntent();
  string status = ii.getStringExtra("status");
    if(status.equals("disable")){
       mBtn2.setEnabled(false);
    }
Cyrille Con Morales
  • 918
  • 1
  • 6
  • 21
  • A class is a fragmetnt so i changed into Intent ii = new Intent(getActivity(), B.class); and i added below code into onCreate which declare mbtn2. it also has an error : java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference at – 문경욱 Aug 27 '19 at 03:42
  • Please check your codes including inflating layout, check IDs of components and to further assist you add some corresponding codes to your post. Thank you – Cyrille Con Morales Aug 27 '19 at 03:56