[英]Variable ' ' is accessed from within inner class, needs to be declared final
In 'Main' im trying to use function declared in class Bluetooth, and having error as in title.在“Main”中,我尝试使用在类蓝牙中声明的函数,并且在标题中出现错误。
Here is the code这是代码
ImageButton3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
bluetooth.wlaczBT();
}
});
I have class bluetooth and within it i have a method wlaczBT (in english its Turn on BT).我有蓝牙课程,在其中我有一个方法 wlaczBT(用英语说它打开 BT)。
But have bluetooth underlined in red unfortunetly.但不幸的是,蓝牙有红色下划线。 How can I used that method in Main class?如何在 Main 类中使用该方法?
This is how i declared variable bluetooth (in Main ofc)这就是我声明变量蓝牙的方式(在 Main ofc 中)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bluetooth bluetooth = new Bluetooth(this);
i tried to add "final" before "Bluetooth", but the app crashed while instaling on phone.我试图在“蓝牙”之前添加“最终”,但应用程序在手机上安装时崩溃了。
You need to make bluetooth a global variable.您需要将蓝牙设为全局变量。 That means declare it before the onCreate() but initialize it in onCreate ().这意味着在 onCreate() 之前声明它但在 onCreate() 中初始化它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.