2013-07-14 3 views
-2

J'essaie d'utiliser un bouton à bascule pour basculer le service Bluetooth dans l'appareil. Mais l'activité se bloque toujours lorsque je clique sur le bouton bascule pour la deuxième fois. Toute suggestion serait appréciée! Merci!ToggleButton NullPointerException dans Android

activity_main.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context=".MainActivity" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:gravity="center_horizontal"> 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="left" 
      android:text="Settings" 
      tools:ignore="HardcodedText" /> 

     <ToggleButton 
      android:id="@+id/toggleButton1" 
      android:layout_width="0dip" 
      android:layout_height="wrap_content" 
      android:layout_gravity="fill" 
      android:layout_weight="1.0" 
      android:textOn="Turn Off" 
      android:textOff="Turn On" 
      android:onClick="onToggleClicked" 
      tools:ignore="HardcodedText" /> 

     <Button 
      android:id="@+id/button2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="right" 
      android:text="Devices" 
      tools:ignore="HardcodedText" /> 

    </LinearLayout> 

    <GridView 
     android:id="@+id/gridView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:numColumns="3" > 
    </GridView>   

</LinearLayout> 

MainActivity.java

public class MainActivity extends Activity {   
private static final int REQUEST_ENABLE_BT = 0; 
final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 

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

    if (mBluetoothAdapter == null) { 
     Toast.makeText(getApplicationContext(), "Device not supported!", Toast.LENGTH_LONG).show(); 
    }    
} 

public void onToggleClicked(View v) 
{      
    final ToggleButton toggle; 
    Boolean toggleStatus; 
    toggle = (ToggleButton)findViewById(R.id.toggleButton1); 

    toggleStatus = ((ToggleButton)v).isChecked(); 

    if(toggleStatus) 
    { 
     toggle.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       if (!mBluetoothAdapter.isEnabled()) { 
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); 
        Toast.makeText(getApplicationContext(), "Enabling Bluetooth..", Toast.LENGTH_LONG).show(); 
       }        
      } 
     }); 
    } 
    else 
    { 
     toggle.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       if (mBluetoothAdapter.isEnabled()) { 
        mBluetoothAdapter.disable(); 
        Toast.makeText(getApplicationContext(), "Disabling Bluetooth..", Toast.LENGTH_LONG).show(); 
       }        
      } 
     }); 
    } 

} 
+0

pourquoi vous définir le onClickListener deux fois? dans la mise en page et un autre dans la fonction onToggleClicked? –

Répondre

0

Je suppose que c'est parce que vous essayez de changer OnClickListener du ToggleButton à l'intérieur du OnClickListener lui-même.

Vous avez déjà configuré le OnClickListener dans le fichier XML avec android:onClick="onToggleClicked", donc il n'y a pas besoin de le faire à nouveau en code dans onToggleClicked

Votre gestionnaire de clic devrait alors devenir quelque chose comme:

public void onToggleClicked(View v) 
{      
    final ToggleButton toggle; 
    Boolean toggleStatus; 
    toggle = (ToggleButton)findViewById(R.id.toggleButton1); 

    toggleStatus = ((ToggleButton)v).isChecked(); 

    if(toggleStatus) 
    { 
       if (!mBluetoothAdapter.isEnabled()) { 
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); 
        Toast.makeText(getApplicationContext(), "Enabling Bluetooth..", Toast.LENGTH_LONG).show(); 
       }        
    } 
    else 
    { 
       if (mBluetoothAdapter.isEnabled()) { 
        mBluetoothAdapter.disable(); 
        Toast.makeText(getApplicationContext(), "Disabling Bluetooth..", Toast.LENGTH_LONG).show(); 
       }        
    } 
}