2015-01-10 116 views
0

我正在開發一個Android應用程序,但由於我是新手,所以無法找出問題的解決方案。我的應用程序在我的手機上安裝時崩潰(Android 4.3),而它在模擬器上運行良好。雖然,我知道它初始化我的藍牙適配器mBluetoothAdapter時崩潰,但不知道解決方案。初始化藍牙適配器時Android應用程序崩潰

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) { 
     } 
    }); 
} 
} 

這裏是我的清單

<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. 
+0

在logcat中沒有堆棧跟蹤,你可以在這裏發佈? – helleye

+0

@helleye,Plz查看相同的更新問題。但請注意,應用程序在模擬器上運行,但在手機上崩潰。 – Cyberpks

+0

這是完整的logcat輸出,當你的應用程序在手機上運行(並崩潰)? – helleye

回答

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" />