简体   繁体   English

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

[英]Why is my Bluetooth application Crashing

I am working on a bluetooth application but my application crash .Here is my MainActivity.java 我正在研究蓝牙应用程序,但我的应用程序崩溃。这是我的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;
}

}

and this is my activity.xml file 这是我的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> 

Whenever I onclick on a button the application crashes... Please help me out with this .. thank in advance. 每当我点击一个按钮时,应用程序崩溃......请帮我解决这个问题..提前谢谢。

On get and off method, there should be a View parameter, right? 在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();  }

Perhaps it is when you click the list button. 也许就是当你点击列表按钮时。 Assuming that the phone has 0 bonded devices, paired.getBondedDevices() will probably return null and the program crashes due to a NullPointerException. 假设手机有0个绑定设备,paired.getBondedDevices()可能会返回null并且程序因NullPointerException而崩溃。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM