2014-09-19 4 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()처럼 말하면 그냥 NullPointerException와 충돌합니다. menu.findItem(R.id.bluetooth_switch)을 사용하는 경우 ClassCastException이라고 표시됩니다. 나는 지금 무엇을 해야할지에 대한 생각이 없기 때문에, pls는 나를 사랑하는 독자를 도와 준다. :)

답변

0

같은 ID를 가진 두 개의 컨트롤이있다. 하나는 다른 스위치의 메뉴 항목입니다. 메뉴 항목의 ID를 변경하면 제대로 작동합니다. 보조 노트에서 나는 코드를 넣을 것이다. :

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

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

checkForBluetoothAdapter(); 

in onCreate(). 이렇게하면 메뉴에서 항목을 가져 오지 않으려한다는 것을 분명히 알 수 있습니다.