简体   繁体   English

从内部类访问变量“ ”,需要声明为 final

[英]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.

相关问题 “从内部类访问变量需要声明为final”错误 - "variable is accessed from within inner class needs to be declared final" error 从内部 class 访问的变量需要声明为 final - Variable accessed from within inner class needs to be declared final 变量(dialogView)是从内部类中访问的,需要声明为final - Variable (dialogView) is accessed from within inner class, needs to be declared final 从内部类访问变量“i”,需要声明为 final - Variable 'i' is accessed from within inner class, needs to be declared final 变量'btnsave'是从内部类中访问的,需要声明为final - Variable 'btnsave' is accessed from within inner class, needs to be declared final 在内部类中访问变量。 需要宣布为最终 - Variable is accessed within inner class. Needs to be declared final 在内部类中访问变量“名称”。 需要宣布为最终 - Variable “name” is accessed within inner class. Needs to be declared final 需要在OnTouchListener中用MediaPlayer声明内部类中访问的变量的最终声明 - Variable Is Accessed Within Inner Class Needs to be Declared Final with MediaPlayer In a OnTouchListener 在内部类中访问如何解析变量,并且需要将其声明为final - How to resolve variable is accessed within inner class and needs to be declared final 从内部类访问,需要声明为final - is accessed from within inner class, needs to be declared final
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM