【问题标题】:Android: setChecked() crashes AppAndroid:setChecked() 使应用程序崩溃
【发布时间】:2014-11-14 10:02:42
【问题描述】:

我想在其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(),它只会因NullPointerException 而崩溃。如果我使用menu.findItem(R.id.bluetooth_switch),它会显示ClassCastException。我不知道现在该怎么办,所以请亲爱的读者帮帮我:)

【问题讨论】:

    标签: java android exception user-interface null


    【解决方案1】:

    您有两个具有相同 ID 的控件。一个是菜单项,另一个是开关。如果您更改菜单项的 ID,它应该可以工作。在旁注中,我会输入代码:

    bluetoothSwitch = (Switch) findViewById(R.id.bluetooth_switch);
    
    if(bluetoothSwitch == null) {
        Log.d("NXTController", "Switch = null");
    }
    
    checkForBluetoothAdapter();
    

    在 onCreate() 中。这表明您没有尝试从菜单中获取项目。

    【讨论】:

      猜你喜欢
      • 2017-11-10
      • 2012-08-18
      • 2012-02-12
      • 2014-03-18
      • 2018-12-05
      • 2012-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多