ColorFrog
ColorFrog

Reputation: 1

Disabling buttons in a Fragment

I have three fragments. One of them can dynamically replaced with another fragment. How can I disable buttons in this "dynamical" fragment. Disabling buttons of the other fragments funtctions. When I compile my App, I get a java.lang.NullPointerException.

//MainActivity

public class MainActivity extends Activity{

public static Button Button_1;
public static Button Button_2;

Block fragment;

...

public void onCreate(Bundle savedInstanceState) 
{
...
Button_1=(Button)findViewById(R.id.button1);
    Button_2=(Button)findViewById(R.id.button2);
...
fragment = (Block) getFragmentManager().findFragmentById(R.id.block_frag);
...
}

... }

public void onRadioButtonClicked(View view) {

switch(view.getId()) {
    ...
    case R.id.radioButton4:
    {

    Button_1.setEnabled(false);
    Button_2.setEnabled(false);
            fragment.Button_3.setEnabled(false);
            fragment.Button_4.setEnabled(false);

    break;
        }
        case R.id.radioButton5:
        {
    ...

}

//block_frag:

public class Block extends Fragment{

public Button Button_3; public Button Button_4; public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Button_3=(Button)getView().findViewById(R.id.button3); Button_4=(Button)getView().findViewById(R.id.button4);

... }

Upvotes: 0

Views: 2411

Answers (1)

Artem Zelinskiy
Artem Zelinskiy

Reputation: 2210

    @Override 
    public void onActivityCreated (Bundle savedInstanceState){
    super.onActivityCreated(savedInstanceState);
    Button_3=(Button)getActivity().findViewById(R.id.button3);
    }

Yes it should be inside fragment

Upvotes: 1

Related Questions