大家好,欢迎来到IT知识分享网。
最近将一个项目作为library,通过import Moudle导入一个新项目引用时,爆出这个错误:需要常量表达式,定位到switch。于是百度一下,解决方法为把switch case,改成if else,即:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()){
case R.id.scan:
Intent serverIntent=new Intent(this,DeviceList.class);
startActivityForResult(serverIntent,REQUEST_CONNECT_DEVICE);
return true;
case R.id.discoverable:
ensureDiscoverable();
return true;
case R.id.BtOpen:
if (!mBluetoothAdapter.isEnabled()) {
Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnOn, REQUEST_ENABLE_BT);
}
return true;
case R.id.BtOff:
mBluetoothAdapter.disable();
return true;
}
return false;
}
改为:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
if(item.getItemId()==R.id.scan)
{
Intent serverIntent=new Intent(this,DeviceList.class);
startActivityForResult(serverIntent,REQUEST_CONNECT_DEVICE);
}
if(item.getItemId()==R.id.discoverable)
{
ensureDiscoverable();
return true;
}
if(item.getItemId()==R.id.BtOpen)
{
if (!mBluetoothAdapter.isEnabled()) {
Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnOn, REQUEST_ENABLE_BT);
}
return true;
}
if(item.getItemId()==R.id.BtOff)
{
mBluetoothAdapter.disable();
return true;
}
return false;
}
具体原因:
http://tools.android.com/tips/non-constant-fields.
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/21750.html