簡體   English   中英

Android:setChecked()使應用程序崩潰

[英]Android: setChecked() crashes App

我想創建一個在其ActionBar帶有Switch的應用程序以切換Bluetooth 這是我的代碼:

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 ”。 如果我改為像checkForBluetoothAdapte() setChecked()那樣說setChecked() ,它將崩潰,並拋出NullPointerException 如果我使用menu.findItem(R.id.bluetooth_switch) ,它說的是ClassCastException 我不知道現在該怎么做,所以請幫助我,親愛的讀者:)

您有兩個具有相同ID的控件。 一個是菜單項,另一個是開關。 如果您更改菜單項的ID,則它應該起作用。 順便說一句,我將代碼:

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

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

checkForBluetoothAdapter();

在onCreate()中。 這清楚表明您不嘗試從菜單中獲取項目。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM