禁用片段中的按钮

时间:2012-12-19 11:49:59

标签: android android-fragments android-activity

我有三个片段。其中一个可以动态替换为另一个片段。如何禁用此“动态”片段中的按钮。禁用其他片段功能的按钮。当我编译我的应用程序时,我得到一个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);

... }

1 个答案:

答案 0 :(得分:1)

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

是的,它应该在片段