Calling a method from another class is causing app to crash

前端 未结 3 1642
星月不相逢
星月不相逢 2020-12-12 06:58

I decided to try and make my code more object oriented and avoid repetitive code in another class.

Source code for Activities :

            


        
3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-12 07:47

    Try This:

    Make one Class Utils:

    In Utils:

    public class Utils{
    private Activity context;
    Button button;
    public Utils(Activity context) {
        this.context=context;
    
    }
    
    public void inititializeButton(Activity context){
        button[0]= (Button) context.findViewById(R.id.button_flasher);
    }
    
    }
    

    And in your Class use:

     public class EasyMode extends MainActivity {
    
            Utils utils;
    
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.game_layout_pvp);
                utils=new Utils(this);
                utils.initializeButtons();
            }
        }
    

提交回复
热议问题