调用方法后按钮变量变为空
Button variable turns to null after calling method
在我的主要活动中,我有以下片段
MainActivity.class
private Button btnx10;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button btnx10=(Button)findViewById(R.id.MainCOPbtn);
DrawLines();
}
private void drawLines(){
float centerYOnImage1=btnx10.getHeight()/2;
}
我正在尝试从 drawLines()
方法访问在 onCreate()
方法中创建的按钮
即在相同的 class MainActivity.class
中但在此方法之外。
当我尝试访问 drawlines()
方法中的按钮时,它的值为空。
如何访问该按钮?
将代码更改为
btnx10= findViewById(R.id.MainCOPbtn);
您在声明中强制转换 Button
,这使得全局变量无法访问。
再次删除 Button
的局部声明。
只需在onCreate()
中使用btnx10=(Button)findViewById(R.id.MainCOPbtn);
由于您已在 方法范围 中声明了按钮 onCreate()
Button btnx10=(Button)findViewById(R.id.MainCOPbtn);
并且您试图在方法 onCreate()
之外访问它,这使得它在此方法之外 无法访问 。
只需在 class 级别(全局)上进行引用,并在 onCreate()
方法中使用相同的引用。
你可以这样做:-
private Button btnx10;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
btnx10 = (Button)findViewById(R.id.MainCOPbtn);
DrawLines();
}
private void drawLines(){
float centerYOnImage1 = btnx10.getHeight()/2;
}
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;
}
在我的主要活动中,我有以下片段
MainActivity.class
private Button btnx10;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button btnx10=(Button)findViewById(R.id.MainCOPbtn);
DrawLines();
}
private void drawLines(){
float centerYOnImage1=btnx10.getHeight()/2;
}
我正在尝试从 drawLines()
方法访问在 onCreate()
方法中创建的按钮
即在相同的 class MainActivity.class
中但在此方法之外。
当我尝试访问 drawlines()
方法中的按钮时,它的值为空。
如何访问该按钮?
将代码更改为
btnx10= findViewById(R.id.MainCOPbtn);
您在声明中强制转换 Button
,这使得全局变量无法访问。
再次删除 Button
的局部声明。
只需在onCreate()
btnx10=(Button)findViewById(R.id.MainCOPbtn);
由于您已在 方法范围 中声明了按钮 onCreate()
Button btnx10=(Button)findViewById(R.id.MainCOPbtn);
并且您试图在方法 onCreate()
之外访问它,这使得它在此方法之外 无法访问 。
只需在 class 级别(全局)上进行引用,并在 onCreate()
方法中使用相同的引用。
你可以这样做:-
private Button btnx10;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
btnx10 = (Button)findViewById(R.id.MainCOPbtn);
DrawLines();
}
private void drawLines(){
float centerYOnImage1 = btnx10.getHeight()/2;
}
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;
}