Java Android应用程序在试图通过蓝牙写入数据时不断抛出空指针异常
我目前正在进行一个项目,我正在构建一个应用程序,使用蓝牙向我的Arduino Uno发送命令,除了实际发送数据外,其他一切都正常工作 每当我尝试发送数据时,它都会抛出一个空指针异常,而我所知道的这些异常是在未初始化或找不到某些内容时抛出的(如果我错了,请纠正我)。因此,我认为问题在于我的connectedThread没有正确初始化,但我不知道在哪里执行此操作以及执行什么操作 还有一些其他的语音到文本的代码,所以忽略它 我真的很感激任何帮助或建议 非常感谢你 My Main_Activity.java:Java Android应用程序在试图通过蓝牙写入数据时不断抛出空指针异常,java,android,bluetooth,Java,Android,Bluetooth,我目前正在进行一个项目,我正在构建一个应用程序,使用蓝牙向我的Arduino Uno发送命令,除了实际发送数据外,其他一切都正常工作 每当我尝试发送数据时,它都会抛出一个空指针异常,而我所知道的这些异常是在未初始化或找不到某些内容时抛出的(如果我错了,请纠正我)。因此,我认为问题在于我的connectedThread没有正确初始化,但我不知道在哪里执行此操作以及执行什么操作 还有一些其他的语音到文本的代码,所以忽略它 我真的很感激任何帮助或建议 非常感谢你 My Main_Activity.ja
package marggraffd.developer.com.robot;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.ActivityNotFoundException;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.speech.RecognizerIntent;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Locale;
import java.util.Set;
import java.util.UUID;
public class MainActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemClickListener {
public static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
private static final int SUCCESS_CONNECT = 0;
private static final int MESSAGE_READ = 1;
private static final String TAG = null;
Toolbar toolbar;
FloatingActionButton fab;
Button forward, reverse, left, right, stop;
private final int REQ_CODE_SPEECH_INPUT = 100;
TextView speech;
String text;
ArrayAdapter<String> listAdapter;
ListView listView;
ArrayList<String> pairedDevices;
ArrayList<BluetoothDevice> devices;
private BluetoothAdapter btAdapter;
Set<BluetoothDevice> devicesArray;
IntentFilter filter;
BroadcastReceiver receiver;
ConnectedThread connectedThread = null;
Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case SUCCESS_CONNECT:
ConnectedThread connectedThread = new ConnectedThread((BluetoothSocket) msg.obj);
Toast.makeText(getApplicationContext(), getResources().getString(R.string.bluetooth_success), Toast.LENGTH_LONG).show();
String s = "Successfully Connected";
connectedThread.write(s);
break;
case MESSAGE_READ:
byte[] readBuff = (byte[]) msg.obj;
String string = new String(readBuff);
Toast.makeText(getApplicationContext(), string, Toast.LENGTH_LONG).show();
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
init();
btAdapter = BluetoothAdapter.getDefaultAdapter();
if (btAdapter == null) {
Toast.makeText(getApplicationContext(), getResources().getString(R.string.error2), Toast.LENGTH_LONG).show();
} else {
if (!btAdapter.isEnabled()) {
turnOnBT();
}
getPairedDevices();
startDiscovery();
}
}
private void startDiscovery() {
btAdapter.cancelDiscovery();
btAdapter.startDiscovery();
}
private void turnOnBT() {
Intent intent = new Intent((BluetoothAdapter.ACTION_REQUEST_ENABLE));
startActivityForResult(intent, 1);
}
private void getPairedDevices() {
devicesArray = btAdapter.getBondedDevices();
if (devicesArray.size() > 0) {
for (BluetoothDevice device : devicesArray) {
pairedDevices.add(device.getName());
}
}
}
private void init() {
fab = (FloatingActionButton) findViewById(R.id.fab);
forward = (Button) findViewById(R.id.bForward);
reverse = (Button) findViewById(R.id.bReverse);
left = (Button) findViewById(R.id.bLeft);
right = (Button) findViewById(R.id.bRight);
stop = (Button) findViewById(R.id.bStop);
speech = (TextView) findViewById(R.id.textView3);
listView = (ListView) findViewById(R.id.listView);
listView.setOnItemClickListener(this);
listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, 0);
listView.setAdapter(listAdapter);
pairedDevices = new ArrayList<String>();
filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
devices = new ArrayList<BluetoothDevice>();
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
devices.add(device);
String s = "";
for (int a = 0; a < pairedDevices.size(); a++) {
if (device.getName().equals(pairedDevices.get(a))) {
s = "(Paired)";
break;
}
}
listAdapter.add(device.getName() + " " + s + " " + "\n" + device.getAddress());
} else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
} else if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
if (btAdapter.getState() == btAdapter.STATE_OFF) {
turnOnBT();
}
}
}
};
registerReceiver(receiver, filter);
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
registerReceiver(receiver, filter);
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(receiver, filter);
filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(receiver, filter);
fab.setOnClickListener(this);
stop.setOnClickListener(this);
buttons();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bStop:
connectedThread.write("e");
break;
case R.id.fab:
speech();
break;
}
}
private void speech() {
Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
i.putExtra(RecognizerIntent.EXTRA_PROMPT, getString(R.string.speech_prompt));
try {
startActivityForResult(i, REQ_CODE_SPEECH_INPUT);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(), getString(R.string.error), Toast.LENGTH_LONG).show();
startActivityForResult(i, REQ_CODE_SPEECH_INPUT);
}
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(receiver);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQ_CODE_SPEECH_INPUT && resultCode == RESULT_OK) {
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
speech.setText(result.get(0));
text = speech.getText().toString();
if (text == getString(R.string.forward)) {
Toast.makeText(getApplicationContext(), "Forward", Toast.LENGTH_LONG).show();
} else if (text == getString(R.string.reverse)) {
Toast.makeText(getApplicationContext(), "Reverse", Toast.LENGTH_LONG).show();
} else if (text == getString(R.string.left)) {
Toast.makeText(getApplicationContext(), "Left", Toast.LENGTH_LONG).show();
} else if (text == getString(R.string.right)) {
Toast.makeText(getApplicationContext(), "Right", Toast.LENGTH_LONG).show();
} else if (text == getString(R.string.stop)) {
Toast.makeText(getApplicationContext(), "Stop", Toast.LENGTH_LONG).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.menu_main, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (btAdapter.isDiscovering()) {
btAdapter.cancelDiscovery();
}
if (listAdapter.getItem(position).contains("(Paired)")) {
BluetoothDevice selectedDevices = devices.get(position);
ConnectThread connect = new ConnectThread(selectedDevices);
connect.start();
} else {
Toast.makeText(getApplicationContext(), getResources().getString(R.string.bluetooth_unpaired), Toast.LENGTH_LONG).show();
}
}
private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
public ConnectThread(BluetoothDevice device) {
BluetoothSocket tmp = null;
mmDevice = device;
try {
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
}
mmSocket = tmp;
}
public void run() {
btAdapter.cancelDiscovery();
try {
mmSocket.connect();
} catch (IOException connectException) {
try {
mmSocket.close();
} catch (IOException closeException) {
}
return;
}
mHandler.obtainMessage(SUCCESS_CONNECT, mmSocket).sendToTarget();
}
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
}
}
}
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket) {
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
byte[] buffer = new byte[9600];
int bytes;
while (true) {
try {
buffer = new byte[9600];
bytes = mmInStream.read(buffer);
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
break;
}
}
}
public void write(String message) {
byte[] msgBuffer = message.getBytes();
try {
mmOutStream.write(msgBuffer);
} catch (IOException e) {
e.printStackTrace();
}
}
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
}
}
}
private void buttons() {
forward.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
try {
connectedThread.write("e");
} catch (Exception e) {
e.printStackTrace();
}
Toast.makeText(getApplicationContext(), "Forward", Toast.LENGTH_SHORT).show();
break;
case MotionEvent.ACTION_UP:
connectedThread.write("e");
Toast.makeText(getApplicationContext(), "Stop", Toast.LENGTH_SHORT).show();
break;
}
return false;
}
});
reverse.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
connectedThread.write("d");
Toast.makeText(getApplicationContext(), "Reverse", Toast.LENGTH_SHORT).show();
break;
case MotionEvent.ACTION_UP:
connectedThread.write("e");
Toast.makeText(getApplicationContext(), "Stop", Toast.LENGTH_SHORT).show();
break;
}
return false;
}
});
left.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
connectedThread.write("b");
Toast.makeText(getApplicationContext(), "Left", Toast.LENGTH_SHORT).show();
break;
case MotionEvent.ACTION_UP:
connectedThread.write("e");
Toast.makeText(getApplicationContext(), "Stop", Toast.LENGTH_SHORT).show();
break;
}
return false;
}
});
right.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
connectedThread.write("c");
Toast.makeText(getApplicationContext(), "Right", Toast.LENGTH_SHORT).show();
break;
case MotionEvent.ACTION_UP:
connectedThread.write("e");
Toast.makeText(getApplicationContext(), "Stop", Toast.LENGTH_SHORT).show();
break;
}
return false;
}
});
}
根据您的
LogCat
错误消息:
java.lang.NullPointerException
10-01 18:32:00.481 21197-21197/marggraffd.developer.com.robot W/System.err: at marggraffd.developer.com.robot.MainActivity$3.onTouch(MainActivity.java:429)
错误在这一行:
connectedThread.write("e");
变量connectedThread
的值仍为null
,请检查代码以正确初始化connectedThread
...
...
...
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
try {
connectedThread.write("e"); //*** connectedThread is NULL
} catch (Exception e) {
e.printStackTrace();
}
Toast.makeText(getApplicationContext(), "Forward", Toast.LENGTH_SHORT).show();
...
...
定义为类字段:
ConnectedThread connectedThread = null;
ConnectedThread connectedThread = new ConnectedThread((BluetoothSocket) msg.obj);
然后您有一个处理程序
,它处理一个连接成功
,并将一个新的连接线程
分配给一个局部变量,not类字段:
ConnectedThread connectedThread = null;
ConnectedThread connectedThread = new ConnectedThread((BluetoothSocket) msg.obj);
我在你的代码中没有看到的是你实际上在哪里创建了一个ConnectedThread
并启动它,更不用说你在哪里给你的类字段分配了一个,你经常用它来写东西
因此,在您的onTouch
方法中抛出了一个NullPointerException
,因为没有connectedThread
,非常感谢您的帮助。我已经解决了这个问题。我刚刚在ConnectThread类中run()的末尾初始化了线程,现在它工作得很好
如果其他任何人面临同样的问题,这就是必须解决的问题:
private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
public ConnectThread(BluetoothDevice device) {
BluetoothSocket tmp = null;
mmDevice = device;
try {
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
}
mmSocket = tmp;
}
public void run() {
btAdapter.cancelDiscovery();
try {
mmSocket.connect();
} catch (IOException connectException) {
try {
mmSocket.close();
} catch (IOException closeException) {
}
return;
}
mHandler.obtainMessage(SUCCESS_CONNECT, mmSocket).sendToTarget();
connectedThread = new ConnectedThread(mmSocket);
connectedThread.start();
}
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
}
}
}
您好@Dietmar,MainActivity.java第429行有什么内容?可能是重复的@Elanasys感谢您的帮助。很抱歉要问,如果可以的话,你能告诉我如何初始化它吗?谢谢你的帮助@Sascha Kolberg。我一直试图在不同的地方启动线程,但每次都会出现更多错误。你能告诉我在哪里做这件事吗?如果可以的话,怎么做?