初始化蓝牙适配器时,Android应用程序崩溃

时间:2015-01-10 08:48:40

标签: java android android-bluetooth

我正在开发一款Android应用,但由于我是新手,所以无法找出问题的解决方案。当我在我的手机(Android 4.3)上安装它时我的应用程序崩溃,而它在模拟器上运行正常。虽然,我知道它在我初始化蓝牙适配器mBluetoothAdapter时崩溃,但不知道解决方案。

public class MainActivity extends ActionBarActivity {
protected static final int REQUEST_ENABLE_BT = 1;
Button btnConnect;
int seekVal;
SeekBar seek ;
TextView editText1;
BluetoothAdapter mBluetoothAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btnConnect = (Button)findViewById(R.id.btnConnect);
    editText1 = (TextView)findViewById(R.id.editText1);
    seekVal = 0;
    seek = (SeekBar)findViewById(R.id.seekBar1);

    editText1.setText(String.valueOf(0));

    // Crashes when Bluetooth Adapter is initialized 
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    // App runs when the above line is commented out
    try{
    if (mBluetoothAdapter == null) {
        Toast.makeText(getApplicationContext(), "Your device does not support bluetooth device. Sorry but the application will exit now!", Toast.LENGTH_SHORT).show();
        btnConnect.setEnabled(false);
    }
    else
    {
        if (mBluetoothAdapter.isEnabled()) {
            btnConnect.setEnabled(false);
        }
    }
    }
    catch(Exception ex)
    {

    }

    btnConnect.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        }
    }); 
}
}

这是我的MANIFEST的一部分

 <uses-sdk
    android:minSdkVersion="13"
    android:targetSdkVersion="21" />
<permission android:name="android.permission.BLUETOOTH"></permission>

这是我的LogCat

01-10 14:55:56.142: E/ResourceType(1027): Style contains key with bad entry: 0x01010479
01-10 14:55:56.392: I/dalvikvm(1027): Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
01-10 14:55:56.392: W/dalvikvm(1027): VFY: unable to resolve virtual method 11341: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
01-10 14:55:56.404: D/dalvikvm(1027): VFY: replacing opcode 0x6f at 0x0000
01-10 14:55:56.412: I/dalvikvm(1027): Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
01-10 14:55:56.412: W/dalvikvm(1027): VFY: unable to resolve virtual method 11347: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
01-10 14:55:56.423: D/dalvikvm(1027): VFY: replacing opcode 0x6f at 0x0000
01-10 14:55:56.423: I/dalvikvm(1027): Could not find method android.view.ViewGroup.onWindowSystemUiVisibilityChanged, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onWindowSystemUiVisibilityChanged
01-10 14:55:56.423: W/dalvikvm(1027): VFY: unable to resolve virtual method 11349: Landroid/view/ViewGroup;.onWindowSystemUiVisibilityChanged (I)V
01-10 14:55:56.423: D/dalvikvm(1027): VFY: replacing opcode 0x6f at 0x0008
01-10 14:55:56.442: I/dalvikvm(1027): Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
01-10 14:55:56.442: W/dalvikvm(1027): VFY: unable to resolve virtual method 9035: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
01-10 14:55:56.453: D/dalvikvm(1027): VFY: replacing opcode 0x6e at 0x000e
01-10 14:55:56.662: I/dalvikvm(1027): Could not find method android.view.ViewGroup.onRtlPropertiesChanged, referenced from method android.support.v7.widget.Toolbar.onRtlPropertiesChanged
01-10 14:55:56.671: W/dalvikvm(1027): VFY: unable to resolve virtual method 11344: Landroid/view/ViewGroup;.onRtlPropertiesChanged (I)V
01-10 14:55:56.671: D/dalvikvm(1027): VFY: replacing opcode 0x6f at 0x0007
01-10 14:55:56.711: I/dalvikvm(1027): Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
01-10 14:55:56.711: W/dalvikvm(1027): VFY: unable to resolve virtual method 366: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
01-10 14:55:56.711: D/dalvikvm(1027): VFY: replacing opcode 0x6e at 0x0002
01-10 14:55:56.722: I/dalvikvm(1027): Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
01-10 14:55:56.722: W/dalvikvm(1027): VFY: unable to resolve virtual method 388: Landroid/content/res/TypedArray;.getType (I)I
01-10 14:55:56.722: D/dalvikvm(1027): VFY: replacing opcode 0x6e at 0x0002
01-10 14:55:58.262: D/dalvikvm(1027): GC_CONCURRENT freed 196K, 5% free 7098K/7431K, paused 8ms+5ms
01-10 14:55:58.492: D/gralloc_goldfish(1027): Emulator without GPU emulation detected.

1 个答案:

答案 0 :(得分:0)

基本上问题是Manifest

中的用户权限不足

https://stackoverflow.com/a/11469502/1584140帮助我使用try catch来获取实际错误。

最后,我通过设置这样的权限解决了我的问题。

 <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
 <uses-permission android:name="android.permission.BLUETOOTH" />