2014-09-19 64 views
0

我想創建一個應用程序Switch在其ActionBar切換Bluetooth。這裏是我的代碼:Android:setChecked()崩潰應用程序

MainActivity.java

package com.github.paul1999.NXTController; 

import android.app.Activity; 
import android.bluetooth.BluetoothAdapter; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.CompoundButton; 
import android.widget.CompoundButton.OnCheckedChangeListener; 
import android.widget.ListView; 
import android.widget.Switch; 
import android.widget.Toast; 
import android.widget.ToggleButton; 

public class MainActivity extends Activity { 

    private ListView device_list; 
    private ArrayAdapter<String> list_adapter; 

    private boolean searching = false; 
    private boolean btActive = false; 

    private boolean supportsBluetooth = true; 

    private Switch bluetoothSwitch; 

    private BluetoothAdapter bluetoothAdapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     device_list = (ListView) findViewById(R.id.device_list); 

     list_adapter = new ArrayAdapter<String>(this, 
       android.R.layout.simple_list_item_1); 

     device_list.setAdapter(list_adapter); 

     bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 

     Log.d("NXTController", "MainActivity onCreate() down"); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 

     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.main_activity_actions, menu); 

     bluetoothSwitch = (Switch) findViewById(R.id.bluetooth_switch); 

     if(bluetoothSwitch == null) { 
      Log.d("NXTController", "Switch = null"); 
     } 

     checkForBluetoothAdapter(); 

     Log.d("NXTConroller", "MainActivity MenuBar created"); 

     return super.onCreateOptionsMenu(menu); 

    } 

    private void noBluetoothNotification() { 
     Toast.makeText(this, "No Bluetooth available", Toast.LENGTH_SHORT) 
       .show(); 
    } 

    private void checkForBluetoothAdapter() { 

     if (bluetoothAdapter == null) { 
      noBluetoothNotification(); 
      supportsBluetooth = false; 
     } else if (bluetoothAdapter.isEnabled()) { 
      btActive = true; 
      bluetoothSwitch.setChecked(true); 

      if (bluetoothAdapter.isDiscovering()) 
       searching = true; 

     } 

    } 

} 

action_bar.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" > 

    <Switch 
     android:id="@+id/bluetooth_switch" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:textOn="On" 
     android:textOff="Off" /> 

</RelativeLayout> 

main_activity_actions.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:yourapp="http://schemas.android.com/apk/res-auto" > 


    <item android:id="@+id/action_search" 
      android:icon="@android:drawable/ic_menu_search" 
      android:title="@string/action_search" 
      android:showAsAction="ifRoom" /> 

    <item 
     android:id="@+id/bluetooth_switch" 
     android:title="" 
     android:showAsAction="always" 
     android:actionLayout="@layout/action_bar" 
    /> 

</menu> 

現在,如果我開始這個,LogCat說「bluetoothSwitch = null」。如果我反而說setChecked()checkForBluetoothAdapte()那樣只會崩潰空指針異常。如果我使用menu.findItem(R.id.bluetooth_switch),它說ClassCastException。我不知道該怎麼做,所以請幫助我,親愛的讀者:)

回答

0

你有兩個控件具有相同的ID。一個是菜單項,另一個是Switch。如果你改變菜單項的ID,它應該工作。在旁註中,我會把代碼:

bluetoothSwitch = (Switch) findViewById(R.id.bluetooth_switch); 

if(bluetoothSwitch == null) { 
    Log.d("NXTController", "Switch = null"); 
} 

checkForBluetoothAdapter(); 

in onCreate()。這表明你不想從菜單中獲取項目。