为什么我的蓝牙应用程序崩溃

时间:2014-04-18 08:00:10

标签: java android bluetooth

我正在开发蓝牙应用程序,但我的应用程序崩溃了。这是我的MainActivity.java

package com.race_gurram.bluetoothadapter;

import java.util.ArrayList;
import java.util.Set;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {
private Button on, off, get, bring;
private BluetoothAdapter BA;
private Set<BluetoothDevice> pairedDevices;
private ListView lv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    on = (Button) findViewById(R.id.on);
    off = (Button) findViewById(R.id.off);
    get = (Button) findViewById(R.id.get);
    bring = (Button) findViewById(R.id.list);

    lv = (ListView) findViewById(R.id.listView1);
    BA = BluetoothAdapter.getDefaultAdapter();
}

public void on(View view)
{
    if(!BA.isEnabled())
    {
        Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(turnOn,0);
        Toast.makeText(getApplicationContext(), "Turned On", Toast.LENGTH_SHORT).show();
    }
    else
    {
        Toast.makeText(getApplicationContext(), "Already on", Toast.LENGTH_SHORT).show();
    }
}

public void list(View view)
{
    pairedDevices = BA.getBondedDevices();
    ArrayList list = new ArrayList();
    for(BluetoothDevice bt : pairedDevices)
    list.add(BA.getName());

    Toast.makeText(getApplicationContext(), "Searching for devices", Toast.LENGTH_SHORT).show();
    final ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,list);
    lv.setAdapter(adapter);

}

public void get()
{
    Intent getVisible = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
    startActivityForResult(getVisible,0);

}
 public void off()
 {
     BA.disable();
     Toast.makeText(getApplicationContext(), "Turned off", Toast.LENGTH_SHORT).show();
 }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

这是我的activity.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:orientation="vertical"
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=".MainActivity" >

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <Button
            android:id="@+id/on"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="on"
            android:text="@string/on" />

        <Button
            android:id="@+id/get"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="get"
            android:text="@string/get" />

        <Button
            android:id="@+id/list"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="list"
            android:text="@string/bring" />

        <Button
            android:id="@+id/off"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="off"
            android:text="@string/off" />

        <ListView
            android:id="@+id/listView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="visible" >
        </ListView>
    </LinearLayout>
</ScrollView>

</RelativeLayout> 

每当我点击一个按钮时,应用程序崩溃......请帮我解决这个问题..提前谢谢。

2 个答案:

答案 0 :(得分:2)

在get和off方法中,应该有一个View参数,对吗?

public void get(View v) {
    Intent getVisible = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
    startActivityForResult(getVisible,0);

}  public void off(View v)  {
     BA.disable();
     Toast.makeText(getApplicationContext(), "Turned off", Toast.LENGTH_SHORT).show();  }

答案 1 :(得分:0)

也许就是当你点击列表按钮时。假设手机有0个绑定设备, paired.getBondedDevices()可能会返回null,程序因NullPointerException而崩溃。

pairedDevices = BA.getBondedDevices();
ArrayList list = new ArrayList();
if(pairedDevices != null) {
    for(BluetoothDevice bt : pairedDevices)
        list.add(BA.getName());
}