Button variable turns to null after calling method

前端 未结 4 955
不知归路
不知归路 2021-01-29 11:34

In my mainactivity I have the following snip

MainActivity.class

private Button btnx10;

@Override
protected void onCreate(Bundle savedInstanceState) {
         


        
4条回答
  •  一整个雨季
    2021-01-29 11:46

    You are declaring Button btnx10 twice. Remove the local declaration. You should declare outside the method, and define inside the method.

    Class MainActivity...
    private Button btnx10;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
        btnx10=(Button)findViewById(R.id.MainCOPbtn); //MINOR CORRECTION IN THIS LINE
        DrawLines()    
    }
    
    private void drawLines() {
         float centerYOnImage1=btnx10.getHeight()/2;
    }
    

提交回复
热议问题