我正在制作蓝牙应用,我在Android Studio中遇到错误。 当我将鼠标悬停在“私人按钮开,关,可见,列表”时,它告诉我“私人字段''”已分配,但从未访问过“。 这是什么意思?奇怪的是,该应用程序在模拟器中不起作用,但它在我的设备上运行正常。知道我做错了吗?
O.T。:如何将代码置于“剧透”模式或类似模式?我不想用代码充斥论坛:))
主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;
}
}
答案 0 :(得分:2)
你有四个变量:
private Button On, Off, Visible, list;
您分配所有这些:
On = (Button)findViewById(R.id.button1);
Off = (Button)findViewById(R.id.button2);
Visible = (Button)findViewById(R.id.button3);
list = (Button)findViewById(R.id.button4);
但你只使用&#34; list&#34;:
for(BluetoothDevice bt : pairedDevices)
list.add(bt.getName());
错误(或警告)告诉您通过不使用变量来浪费变量。现在拆分你的私人变量而不是分配它们,它应该是这样的:
private Button list;
//private Button On, Off, Visible;
然后:
//On = (Button)findViewById(R.id.button1);
//Off = (Button)findViewById(R.id.button2);
//Visible = (Button)findViewById(R.id.button3);
list = (Button)findViewById(R.id.button4);
如果你想使用这些变量,请取消注释掉它们然后你的好处就可以了!
答案 1 :(得分:0)
这只是一个警告,表明你从未使用它。
此外,您的命名已关闭。字段应按惯例开始小写。
答案 2 :(得分:0)
这意味着您已声明On,Off,Visible并且您没有使用它。这只是一个警告,你可以忽略它。