Android蓝牙应用程序在启动时崩溃

时间:2015-02-08 04:40:45

标签: java android bluetooth

当应用程序首次启动时,它会在启动时立即崩溃。我已经将问题缩小到一行但不确定该做什么= /这是目前应用程序中的两个类:

package com.example.bluetooth_app;

import java.util.Set;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class Bluetooth {
    private BluetoothAdapter mBluetoothAdapter;
    private Activity activity; //Activity to store main window's activity
    private ArrayAdapter<String> pDevices; //Array adapter for storing already paired devices
    private ArrayAdapter<String> nDevices; //Array adapter for storing newly discovered devices
    private IntentFilter filter; //Filter for catching bluetooth device actions
    private Button sButton; //Scan button
    private ListView lvBox; //listview box

    /**
     * default constructor, basic initializations
     */
    public Bluetooth(Activity activity) {
        this.activity = activity; //Set class activity to the activity passed to it by the main activity window
        pDevices = new ArrayAdapter<String>(activity, R.layout.activity_bluetooth__app);
        nDevices = new ArrayAdapter<String>(activity, R.layout.activity_bluetooth__app);
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

        sButton = (Button) activity.findViewById(R.id.scanButton); //sButton = scan button
        sButton.setOnClickListener(new View.OnClickListener() { //Listener to check if button is pressed
            public void onClick(View v) { //If button is pressed start discovering and hide button
                startDiscovering();
                sButton.setVisibility(4); //Make button invisible
            }
        });

        lvBox = (ListView) activity.findViewById(R.id.deviceList); // lvBox = deviceList listview
        lvBox.setAdapter(pDevices);


        // Register for broadcasts when a device is discovered
        filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        activity.registerReceiver(mReceiver, filter);

        // Register for broadcasts when discovery has finished
        filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        activity.registerReceiver(mReceiver, filter);
    }

    /**
     * Check if bluetooth is enabled, if not enable it
     */
    public void getAdapter() {
        if (!mBluetoothAdapter.isEnabled()) {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            activity.startActivityForResult(enableBtIntent, 1);
        }

    }

    /**
     * Check if device is bluetooth compatible
     */
    public boolean isCompat() {
        if(mBluetoothAdapter == null) {
            return false; //TODO: better error handling
        } else {
            return true;
        }
    }

    /**
     * Close some shit so we do not eat up resources
     */
    public void destroy() { 
        if(mBluetoothAdapter != null) {
            mBluetoothAdapter.cancelDiscovery(); //cancel discovering devices
        }

        activity.unregisterReceiver(mReceiver);
    }

    /**
     * Start discovering devices with bluetooth adapter
     */
    public void startDiscovering() {
        if(mBluetoothAdapter.isDiscovering()) {
            mBluetoothAdapter.cancelDiscovery();
        }

        mBluetoothAdapter.startDiscovery();
    }

    public void pairedDevices() {
        Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
        // If there are paired devices
        if (pairedDevices.size() > 0) {
            sButton.setText("found some");
            // Loop through paired devices
            for (BluetoothDevice device : pairedDevices) {
                // Add the name and address to an array adapter to show in a ListView
                pDevices.add(device.getName() + "\n" + device.getAddress());
            }
        }
    }

    /**
     * The BroadcastReceiver that listens for discovered devices and changes the title when
     * discovery is finished
     */
    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            // When discovery finds a device
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                // Get the BluetoothDevice object from the Intent
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                // If it's already paired, skip it, because it's been listed already
                if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
                    nDevices.add(device.getName() + "\n" + device.getAddress());
                }
                // When discovery is finished, change the Activity title
            } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
                sButton.setVisibility(0); //Make button visible again
                if (nDevices.getCount() == 0) {
                    sButton.setText("none");
                    //TODO: none found do something
                }
            }
        }
    };

}

和:

package com.example.bluetooth_app;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import com.example.bluetooth_app.Bluetooth;

public class Bluetooth_App extends ActionBarActivity {
    private Bluetooth bT;

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

        bT = new Bluetooth(this);

        bT.isCompat();
        bT.getAdapter();
        bT.pairedDevices();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        bT.destroy();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.bluetooth__app, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

问题在于第一类,这一行:lvBox.setAdapter(pDevices);,如果我发表评论它运行得很好,如果不是它在启动时崩溃。任何帮助将不胜感激 - 谢谢。

EDIT1 - 没有名为deviceList的列表视图,XML文件:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.bluetooth_app.Bluetooth_App" >

    <Button
        android:id="@+id/scanButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="28dp"
        android:layout_marginTop="21dp"
        android:text="Scan" />

    <ListView
        android:id="@+id/deviceList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginRight="26dp"
        android:layout_marginTop="48dp" >
    </ListView>

</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

请注意,在R.layout.activity_bluetooth__app初始化ArrayAdapter时,您似乎正在重复使用活动的布局文件Bluetooth_App。这可能不是你想要做的。传递给ArrayAdapter构造函数的资源应代表单个AdapterView行的布局,并且必须包含TextView,其ID为text1(以使用更自定义的行布局,你需要继承ArrayAdapter并覆盖getView)。