我目前正在进行一个项目,我正在构建一个应用程序,使用蓝牙向我的Arduino Uno发送命令,除了实际发送数据外,其他一切都正常工作。
每当我尝试发送数据时,它都会抛出一个空指针异常,我所知道的这些异常是在没有初始化或找不到东西时抛出的(如果我错了,请纠正我)。所以我认为问题是我的连接线程没有正确初始化,但我不知道在哪里做这件事,该做什么。
还有一些其他的语音到文本的代码,所以忽略它。
我真的非常感谢任何帮助或建议。
非常感谢!
我的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)
定义为类字段:
ConnectedThread connectedThread = null;
然后有一个处理程序
,它处理连接_SUCCESS
并将一个新的ConnectedThread
分配给一个局部变量,而不是类字段:
ConnectedThread connectedThread = new ConnectedThread((BluetoothSocket) msg.obj);
在您的代码中,我没有看到您实际创建< code>ConnectedThread并启动它的地方,更没有看到您将它赋给class字段的地方,您经常使用它来< code >编写内容。
因此,在您的onTouch
方法中抛出了NullPointerException
,因为没有连接的Thread
。
基于您的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
,请检查代码以正确初始化ConnectedThead
!
...
...
...
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();
...
...
非常感谢你的帮助。我已经解决了问题。我刚刚在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) {
}
}
}
问题内容: 我正在android中做一个应用程序,因此我需要访问com.android.internal.telephony API。现在,我可以访问这些API了,但问题是,无论我在自己的类中调用Class Call.java方法的什么地方,都会抛出。您可以在http://hi- android.info/src/com/android/internal/telephony/Call.java.h
我是android和java的新手,我试着从教程中创建一个android的导航抽屉屏幕: http://www.androidhive.info/2013/11/Android-Sliding-Menu-Using-Navigation-Drawer/ 我复制了代码,我的代码中有一些活动可以很好地工作,但现在我尝试添加导航抽屉活动到我的应用程序,但它在启动时崩溃。我的menifest文件是这样的:
我得到的只有java.lang.NullPointerException。 我还尝试了各种命名风格,如java:jdbc/mydb或java:comp/env/jdbc/mydb 这使用了最新的Glassfish(4.1),针对Postgres 9.4的最新Postgres驱动程序(9.3-1102 JDBC 41),以及最新的Java(1.8.0_31-B13)。
首先,下面的代码片段是Google云项目应用程序的一部分,在我的本地客户机Raspberry Pi 1上运行。为了能够从连接到Pi的传感器向云发送数据,需要授权。所有需要的客户端机密都存储在src/main/resources中的“client_secrets.json”中。 项目层次结构 当试图使用客户端机密来授权时,下面的代码抛出一个NullPointerException。它是类“CmdLi
我完全不知道如何使用BLE。我需要为Android设备创建一个应用程序,将一个小字符串从一个Android设备发送到另一个Android设备。我没有使用任何传感器类型的设备。我只需要使用BLE连接将一个字符串从一个电话发送到另一个电话。根据我的理解,我需要创建一个自定义服务,在一个中央设备上实现一个GATT服务器,并让一个外围设备作为一个客户端访问它,但我甚至不确定这是否是我的情况所需要的。我在互
Struts检测到未处理的异常: 消息:java.lang.nullPointerException文件:org/apache/jsp/web_002dinf/jsps/api/multilineout_jsp.java行号:160 以下是服务器控制台上的错误: 我在前面读到过,这个问题可能是由于Tomcat库&Web-INF库中的JAR文件冲突造成的,但事实并非如此。 我对Java非常陌生,只维