java Android Studio 错误“私有字段''”已分配但从未访问过

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30299358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 16:51:23  来源:igfitidea点击:

Android Studio error "Private field '' "is assigned but never accessed

javaandroid

提问by cicciobamba32

I am making a bluetooth app and I have encountered an error in Android Studio. When I mouse over "private Button On,Off,Visible,list"it tells me "Private field '' "is assigned but never accessed". What it means? And the strange thing is that the app doesn't work in the emulator but it works just fine on my device. Any idea what I am doing wrong?

我正在制作蓝牙应用程序,但在 Android Studio 中遇到错误。当我将鼠标悬停在 "private Button On,Off,Visible,list" 上时,它告诉我"Private field '' "已分配但从未访问过"。这是什么意思?奇怪的是该应用程序在模拟器中不起作用但它在我的设备上运行良好。知道我做错了什么吗?

O.T.: How do I put the code in "spoiler" mode or similar? I don't want to flood the forum with codes :)

OT:如何将代码置于“剧透”模式或类似模式?我不想用代码淹没论坛:)

The Main Java code is:

主要的Java代码是:

   import android.os.Bundle;
   import android.app.Activity;
   import android.bluetooth.BluetoothAdapter;
   import android.bluetooth.BluetoothDevice;
   import android.content.Intent;
   import android.view.Menu;
   import android.view.View;
   import android.widget.ArrayAdapter;
   import android.widget.Button;
   import android.widget.ListAdapter;
   import android.widget.ListView;
   import android.widget.Toast;

    public class MainActivity extends Activity {

private Button On,Off,Visible,list;
private BluetoothAdapter BA;
private Set<BluetoothDevice>pairedDevices;
private ListView lv;
@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    On = (Button)findViewById(R.id.button1);
    Off = (Button)findViewById(R.id.button2);
    Visible = (Button)findViewById(R.id.button3);
    list = (Button)findViewById(R.id.button4);

    lv = (ListView)findViewById(R.id.listView1);

    BA = BluetoothAdapter.getDefaultAdapter();
}

public void on(View view){
    if (!BA.isEnabled()) {
        Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(turnOn, 0);
        Toast.makeText(getApplicationContext(),"Turned on"
                ,Toast.LENGTH_LONG).show();
    }
    else{
        Toast.makeText(getApplicationContext(),"Already on",
                Toast.LENGTH_LONG).show();
    }
}
public void list(View view){
    pairedDevices = BA.getBondedDevices();

    ArrayList list = new ArrayList();
    for(BluetoothDevice bt : pairedDevices)
        list.add(bt.getName());

    Toast.makeText(getApplicationContext(),"Showing Paired Devices",
            Toast.LENGTH_SHORT).show();
    final ArrayAdapter adapter = new ArrayAdapter
            (this,android.R.layout.simple_list_item_1, list);
    lv.setAdapter(adapter);

}
public void off(View view){
    BA.disable();
    Toast.makeText(getApplicationContext(),"Turned off" ,
            Toast.LENGTH_LONG).show();
}
public void visible(View view){
    Intent getVisible = new Intent(BluetoothAdapter.
            ACTION_REQUEST_DISCOVERABLE);
    startActivityForResult(getVisible, 0);

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

}

回答by apmartin1991

You have four Variables:

你有四个变量:

 private Button On, Off, Visible, list;

You assign all of them:

您分配所有这些:

 On = (Button)findViewById(R.id.button1);
    Off = (Button)findViewById(R.id.button2);
    Visible = (Button)findViewById(R.id.button3);
    list = (Button)findViewById(R.id.button4);

But you only use "list":

但你只使用“列表”:

for(BluetoothDevice bt : pairedDevices)
        list.add(bt.getName());

The error (or warning) is telling you that you are wasting variables by not using them. For now split up your private Variables and don't assign them, it should look something like this:

错误(或警告)告诉您不使用变量是在浪费变量。现在拆分你的私有变量并且不分配它们,它应该是这样的:

private Button list;
//private Button On, Off, Visible;

and then:

接着:

//On = (Button)findViewById(R.id.button1);
//Off = (Button)findViewById(R.id.button2);
//Visible = (Button)findViewById(R.id.button3);
list = (Button)findViewById(R.id.button4);

When you want to use these variables un-comment out them and then your good to go!

当您想使用这些变量时,请取消注释它们,然后就可以了!

回答by Samvid Kulkarni

It means that you have declared On, Off, Visible and you are not using it. it is just a warning and you can ignore it.

这意味着您已声明 On、Off、Visible 并且您没有使用它。这只是一个警告,您可以忽略它。

回答by barq

It is just a warning stating that you are never using it.

这只是一个警告,说明您从未使用过它。

Also your naming is off. Fields should start lowercase by convention.

您的命名也已关闭。按照惯例,字段应以小写开头。