2016-03-04 37 views
0

我是新來的Java和我試圖建立一個藍牙連接。我做了一個藍牙類來啓用和禁用藍牙,但是當我從另一個活動呼叫藍牙類時,它會崩潰。我認爲這是造成事故的意圖。應用程序崩潰從另一個類的調用意圖,以啓用藍牙

這是從我的藍牙Class(Bluetooth.java)

public class Bluetooth extends AppCompatActivity { 
private final static int REQUEST_ENABLE_BT = 1; 
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 

public void bt_Check(View v){ 
    if (mBluetoothAdapter != null) { 
     if (mBluetoothAdapter.isEnabled()) { 
      Snackbar.make(v, "BT is ON, now what?", Snackbar.LENGTH_LONG).show(); 
     } else { 
      Snackbar.make(v, "Bluetooth is currently disabled", Snackbar.LENGTH_LONG) 
        .setAction("ENABLE BLUETOOTH", new View.OnClickListener() { 
         @Override 
         public void onClick(View v) { 
          Snackbar.make(v, "Enabling bluetooth", Snackbar.LENGTH_LONG).show(); 
          Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
          startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); 
         } 
        }).show(); 
     } 
    } else { 
     Snackbar.make(v, "Device does not support Bluetooth", Snackbar.LENGTH_LONG).show(); 
    } 
} 

的logcat:

03-04 11:45:17.104 23762-23762/com.hszuyd.noodle_.testing E/AndroidRuntime: FATAL EXCEPTION: main 
                     Process: com.hszuyd.noodle_.testing, PID: 23762 
                     Theme: themes:{} 
                     java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference 
                      at android.app.Activity.startActivityForResult(Activity.java:3931) 
                      at android.app.Activity.startActivityForResult(Activity.java:3890) 
                      at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:784) 
                      at com.hszuyd.noodle_.testing.Bluetooth$1.onClick(Bluetooth.java:27) 
                      at android.support.design.widget.Snackbar$2.onClick(Snackbar.java:295) 
                      at android.view.View.performClick(View.java:5204) 
                      at android.view.View$PerformClick.run(View.java:21156) 
                      at android.os.Handler.handleCallback(Handler.java:739) 
                      at android.os.Handler.dispatchMessage(Handler.java:95) 
                      at android.os.Looper.loop(Looper.java:148) 
                      at android.app.ActivityThread.main(ActivityThread.java:5466) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                      at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:117) 

AndroidManifest:

<?xml version="1.0" encoding="utf-8"?> 

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

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_app" 
    android:label="@string/app_name" 
    android:supportsRtl="false" 
    android:theme="@style/AppTheme"> 
    <activity 
     android:name=".MainActivity" 
     android:label="@string/title_activity_main" 
     android:theme="@style/AppTheme.NoActionBar"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN"/> 
      <category android:name="android.intent.category.LAUNCHER"/> 
     </intent-filter> 
    </activity> 
    <activity 
     android:name=".KickPanelActivity" 
     android:label="@string/title_activity_kickpanel" 
     android:theme="@style/AppTheme.NoActionBar"> 
    </activity> 
    <activity 
     android:name=".TribotActivity" 
     android:label="@string/title_activity_tribot" 
     android:theme="@style/AppTheme.NoActionBar"> 
    </activity> 
</application> 

這裏就是我從調用KickPanelActivity(我說的是活動):

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_kickpanel); 

    Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(mToolbar); 

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      // TODO Replace this with something useful 
      Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
        .setAction("Action", null).show(); //Action that should be run when the snackbar!? is pressed 
     } 
    }); 
} 

public void button_bt_check_OnClick(View v) { 
     bluetooth.bt_Check(v); 
    } 
+1

你的錯誤日誌請 –

+0

添加日誌(貓):-) – Remco1250

+0

爲你的活動顯示清單,似乎有什麼錯誤的主題 – IgorB

回答

0

好像你必須getDefaultAdapter中的OnCreate(),因此,嘗試這樣的方式希望這個將爲你工作。

private BluetoothAdapter mBluetoothAdapter; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.frg_home, container, false); 
    findViews(rootView); 
    try { 

     mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
     if (!mBluetoothAdapter.isEnabled()) { 
      Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
      startActivityForResult(enableBtIntent, 1); 
     } 
    } catch (Exception e) { 
    } 
    return rootView; 
} 
0

嘿,我可以在這裏看到

下面的一行代碼中的一個主要問題

Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 

錯了,它應該是

Intent yourIntentname = new Intent(yourActivityOrServiceSource.this, yourActivityOrServiceDestination.class); 

因爲你的意圖對象爲空,並且你正在試圖做一些事情,因此你得到了空指針引用。

從您的意見

另一個類,這裏​​的匿名內部類OnClickListener內部創建的意圖發現了另一個問題。因此,這並沒有將您的Activity(或Context)的實例作爲目標引用,而是將您的匿名內部類OnClickListener的實例引用。

所以,你可以緩存這個對象像

類名是=這一點;

,並使用變量,對於意圖

UPDATE

如果你的目的是剛剛啓用藍牙

可以使用

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); mBluetoothAdapter。使();

+0

嗨,我試過這樣的:'Intent enableBtIntent = new Intent(Bluetooth.this,KickPanelActivity.class);' Bluetooth.this是藍牙類,而KickPanelActivity.class是我的活動從中調用它。 – Remco1250

+0

@ Remco1250,你可以在這裏發佈藍牙類 –

+0

藍牙已在問題 –

相關問題