- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在开发一个项目,正在构建一个应用程序,该应用程序使用蓝牙向我的 Arduino Uno 发送命令,除了实际发送数据之外,一切都运行良好。
每当我尝试发送数据时,它都会抛出空指针异常,而我对这些异常的了解是,它们是在某些内容未初始化或无法找到时抛出的(如果我错了,请纠正我)。所以我认为问题是我的connectedThread没有正确初始化,但我不知道在哪里执行此操作以及要做什么。
还有一些其他用于语音转文本的代码,因此请忽略它。
我非常感谢任何帮助或建议。
非常感谢!
我的Main_Activity.java:
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;
}
});
}
}
我的activity_main.xml:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ECEFF1"
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/scrollView"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentTop="true"
android:background="#009688"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
</android.support.v7.widget.Toolbar>
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.amdroic.co,/apk/res-auto"
android:id="@+id/controller"
android:layout_width="fill_parent"
android:layout_height="320dp"
android:layout_below="@+id/toolbar"
android:layout_marginTop="10dp"
card_view:cardCornerRadius="4dp">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/bStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/ic_stop_black_24dp" />
<Button
android:id="@+id/bLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/bStop"
android:layout_toStartOf="@+id/bStop"
android:background="@drawable/ic_keyboard_arrow_left_black_24dp" />
<Button
android:id="@+id/bRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toEndOf="@+id/bStop"
android:layout_toRightOf="@+id/bStop"
android:background="@drawable/ic_keyboard_arrow_right_black_24dp" />
<Button
android:id="@+id/bForward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/bStop"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="@drawable/ic_keyboard_arrow_up_black_24dp" />
<Button
android:id="@+id/bReverse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/bStop"
android:layout_centerHorizontal="true"
android:background="@drawable/ic_keyboard_arrow_down_black_24dp" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Controller"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/card2"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_below="@+id/controller"
android:layout_marginTop="10dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:text="Bluetooth"
android:textAppearance="?android:attr/textAppearanceSmall" />
<ListView
android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/textView2"
android:clickable="true"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/cardView3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/card2"
android:layout_marginTop="10dp"
android:checked="false">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:text="Monitor"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
</ScrollView>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:src="@drawable/ic_mic_black_48dp"
app:backgroundTint="#009688"
app:elevation="6dp"
app:fabSize="normal" />
我的日志猫:
10-01 18:32:00.466 21197-21197/marggraffd.developer.com.robot W/System.err: 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)
10-01 18:32:00.481 21197-21197/marggraffd.developer.com.robot W/System.err: at android.view.View.dispatchTouchEvent(View.java:8130)
10-01 18:32:00.481 21197-21197/marggraffd.developer.com.robot W/System.err: at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2422)
10-01 18:32:00.481 21197-21197/marggraffd.developer.com.robot W/System.err: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2112)
10-01 18:32:00.481 21197-21197/marggraffd.developer.com.robot W/System.err: at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2422)
10-01 18:32:00.481 21197-21197/marggraffd.developer.com.robot W/System.err: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2112)
10-01 18:32:00.481 21197-21197/marggraffd.developer.com.robot W/System.err: at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2422)
10-01 18:32:00.481 21197-21197/marggraffd.developer.com.robot W/System.err: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2112)
10-01 18:32:00.481 21197-21197/marggraffd.developer.com.robot W/System.err: at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2422)
10-01 18:32:00.481 21197-21197/marggraffd.developer.com.robot W/System.err: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2112)
10-01 18:32:00.481 21197-21197/marggraffd.developer.com.robot W/System.err: at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2422)
10-01 18:32:00.481 21197-21197/marggraffd.developer.com.robot W/System.err: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2112)
10-01 18:32:00.481 21197-21197/marggraffd.developer.com.robot W/System.err: at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2422)
10-01 18:32:00.481 21197-21197/marggraffd.developer.com.robot W/System.err: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2112)
10-01 18:32:00.481 21197-21197/marggraffd.developer.com.robot W/System.err: at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2422)
10-01 18:32:00.481 21197-21197/marggraffd.developer.com.robot W/System.err: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2112)
10-01 18:32:00.481 21197-21197/marggraffd.developer.com.robot W/System.err: at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2422)
10-01 18:32:00.486 21197-21197/marggraffd.developer.com.robot W/System.err: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2112)
10-01 18:32:00.486 21197-21197/marggraffd.developer.com.robot W/System.err: at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2422)
10-01 18:32:00.486 21197-21197/marggraffd.developer.com.robot W/System.err: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2112)
10-01 18:32:00.486 21197-21197/marggraffd.developer.com.robot W/System.err: at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2422)
10-01 18:32:00.486 21197-21197/marggraffd.developer.com.robot W/System.err: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2112)
10-01 18:32:00.486 21197-21197/marggraffd.developer.com.robot W/System.err: at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2295)
10-01 18:32:00.486 21197-21197/marggraffd.developer.com.robot W/System.err: at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1622)
10-01 18:32:00.486 21197-21197/marggraffd.developer.com.robot W/System.err: at android.app.Activity.dispatchTouchEvent(Activity.java:2565)
10-01 18:32:00.486 21197-21197/marggraffd.developer.com.robot W/System.err: at android.support.v7.internal.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
10-01 18:32:00.486 21197-21197/marggraffd.developer.com.robot W/System.err: at android.support.v7.internal.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
最佳答案
非常感谢您的所有帮助。我已经解决了这个问题。我刚刚在 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) {
}
}
}
关于java - Android 应用程序尝试通过蓝牙写入数据时不断抛出空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32892589/
说真的,你怎么能在不发疯的情况下处理所有这些异常呢?我是不是读了太多关于异常处理的文章或什么?我尝试重构了几次,但每次似乎都以更糟糕的结果告终。也许我应该承认确实会发生异常(exception)情况,
背景 两者 try/rescue和 try/catch是 Elixir 中的错误处理技术。根据 corresponding chapter在介绍指南中。 Errors can be rescued u
每当我尝试在 Raspberry PI 上运行此 python 脚本时,我都会遇到问题: import socket import sys # Create a TCP/IP socket sock
我想知道一些关于 PHP 的 try , catch声明。 让我们考虑以下示例。 abstract class ExceptionA extends Exception {} class Except
我的 laravel v5.4 项目中有两个模型,user 和 admin。 在 config/auth.php 中,我向守卫和提供者添加了管理员,如下所示: 'guards' => [ 'w
try: r = requests.get(url, params={'s': thing}) except requests.ConnectionError, e: print e
我有以下代码。 但是,它并不能捕获所有错误,而我仍然会收到“throw er;//未处理的'错误'事件”。 为什么是这样? app.post('/api/properties/zip/:zip/bed
问题与细节 我正在使用自定义错误处理,遇到的错误之一是“路径中的非法字符”。我有一个自定义函数,旨在通过路径字符串查找此类非法字符,并在找到它们时引发自定义错误。但是我发现,取决于非法字符,Test-
This question already has answers here: How do I catch a numpy warning like it's an exception (not j
我正在使用其他人的代码,但我不熟悉try/catch,因此我举了一个类似的小例子。在第11行上,如果我写了error(''),似乎没有发现错误并增加了索引j。但是,编写error(' ')或error
我在我的一个程序中遇到了这个问题,在这种情况下,尝试/异常(exception)的错误使程序变得更好,以防用户意外输入了他们不应该输入的内容。它仍然给我错误,我为为什么感到困惑。如果对我的问题确实很重
我在尝试TRY ... CATCH块时遇到问题。有人可以解释为什么以下代码无法执行我的sp吗? DECLARE @Result int SET @Result = 0 BEGIN TRY SE
我有一个相当大的 powershell 脚本,其中包含许多(20 多个)执行各种操作的函数。 现在所有代码实际上都没有任何错误处理或重试功能。如果某个特定的任务/功能失败,它就会失败并继续。 我想改进
为什么我尝试时需要导入 inputmismatchException catch(InputMismatchException e){ System.out.println("
我对此感到困惑 - 我为辅助方法编写了一个 try/catch 。它的目的是捕获任何无效输入(任何不是“男性”或“女性”的内容(没有特定情况)。如果输入无效,它将通知用户,然后让他们重试。如果有效,则
我有时会发现自己处于如下场景。尽可能简单地陈述问题 “有时我会创建一段代码,Java 让我将其包含在 try/catch 语句中。我没有使用 catch,所以我将其留空。为什么这是错误的?” boo
我有点困惑为什么当我不使用 Try block 时会出现 Try block 错误。 我在代码块底部附近收到错误通知。如果我不使用 try/catch,有人可以向我解释为什么会发生这种情况吗? 它是否
我已经盯着我的电脑两个小时了,我不知道我做错了什么。谁能帮助我看到光明? package blackjack; import java.util.Random; import java.util.Sc
我想将方法保存在 Enum 中,但 Class.getDeclaredMethod 抛出 NoSuchMethodException,那么我该如何处理呢?我的代码: public enum Car
这个问题已经有答案了: Executing multi-line statements in the one-line command-line (18 个回答) 已关闭 3 年前。 如何使用try.
我是一名优秀的程序员,十分优秀!