Reputation: 45
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?
O.T.: How do I put the code in "spoiler" mode or similar? I don't want to flood the forum with codes :)
The Main Java code is:
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;
}
}
Upvotes: 2
Views: 16489
Reputation: 1269
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.
Upvotes: 0
Reputation: 3074
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!
Upvotes: 4
Reputation: 3711
It is just a warning stating that you are never using it.
Also your naming is off. Fields should start lowercase by convention.
Upvotes: 0