我无缘无故地得到了这个NullPointerException。我正在尝试将加速度计'x'值发送到通过蓝牙连接到我的手机的MCU。在它开始发送字节之前,一切似乎都很好。
MainActivity.class:
private void getAccelerometer(SensorEvent event) {
float[] values = event.values;
// Movement
float x = values[0];
float y = values[1];
float z = values[2];
// Convert float value to integer for progress bars because
// they dont't support float value
int mProgressStatus_x = (int)x;
int mProgressStatus_y = (int)y;
int mProgressStatus_z = (int)z;
// Set converted x, y and z values to progress bars
// Add to each progress bar value 10 because progress bar dosen't support negative value
mProgressBar_x.setProgress(mProgressStatus_x + 10);
mProgressBar_y.setProgress(mProgressStatus_y + 10);
mProgressBar_z.setProgress(mProgressStatus_z + 10);
mX = Float.toString(x);
SendBytes(mX);
}
private void SendBytes (String mX) {
if (mConnectionStatus == 1 && mX != null) {
// mX is the accelerometer value which is converted to the String
byte[] out = mX.getBytes();
// mBluetoothService is BluetoothService.java
mBluetoothService.write(out); // Line 150
}
}
BluetoothService.class:
public synchronized void connected(BluetoothSocket socket, BluetoothDevice device) {
// Cancel the thread that completed the connection
if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null; }
// Cancel the ConnectedThread to make sure that it's not running currently connection
if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;}
// Start the ConnectedThread to manage connection and perform transmission
mConnectedThread = new ConnectedThread(socket);
mConnectedThread.start();
// Send the name of the connected device back to the UI Activity
Message msg = mHandler.obtainMessage(MainActivity.MESSAGE_DEVICE_NAME);
Bundle bundle = new Bundle();
bundle.putString(MainActivity.DEVICE_NAME, device.getName());
msg.setData(bundle);
mHandler.sendMessage(msg);
// Set state of connection to STATE_CONNECTED
setState(STATE_CONNECTED);
mmConnectionStatus = 1;
System.out.println("State connected");
}
public void write(byte[] out) {
ConnectedThread r;
// Synchronize a copy of the ConnectedThread
synchronized (this) {
r = mConnectedThread;
}
if(out != null){
r.write(out); } // Line 133
}
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
// Thread for managing connection
public ConnectedThread(BluetoothSocket socket) {
System.out.println("ConnectedThread started");
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
try { // Get the input and output streams, using temp objects because
// member streams are final
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
System.out.println("creating socket");
}
catch (IOException e) {
System.out.println("tmp socket not created");
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
System.out.println("ConnectedThread status" + mConnectedThread);
// Buffer store for the stream
byte[] buffer = new byte[1024];
// Bytes returned from the read()
int bytesIn;
// Keep listening until an occurs
while(true) {
try {
bytesIn = mmInStream.read(buffer);
} catch (IOException e) {
break;
}
}
}
// Call this from the MainActivity to send data to the remote device
public void write(byte[] bytes) {
System.out.println(bytes);
if(mState == STATE_CONNECTED && bytes != null){
try {
mmOutStream.write(bytes);
mmOutStream.flush();
} catch (IOException e) {
System.out.println("Exception during write" + e);
}
}
}
// Call this from the MainActivity to shutdown the connection
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {}
}
}
日志:
12-12 03:22:59.228: D/libEGL(12554): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
12-12 03:22:59.228: D/libEGL(12554): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
12-12 03:22:59.228: D/libEGL(12554): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
12-12 03:22:59.368: D/OpenGLRenderer(12554): Enabling debug mode 0
12-12 03:23:01.767: D/dalvikvm(12554): GC_CONCURRENT freed 111K, 2% free 9181K/9324K, paused 4ms+7ms, total 33ms
12-12 03:23:01.892: D/BluetoothAdapter(12554): enable(): BT is already enabled..!
12-12 03:23:08.509: I/System.out(12554): ConnectThread statusThread[Thread-919,5,main]
12-12 03:23:08.509: W/BluetoothAdapter(12554): getBluetoothService() called with no BluetoothManagerCallback
12-12 03:23:08.517: D/BluetoothSocket(12554): connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[46]}
12-12 03:23:09.017: E/ActivityThread(12554): Activity com.example.bluetoothc.settings has leaked IntentReceiver com.example.bluetoothc.settings$2@4151ecb8 that was originally registered here. Are you missing a call to unregisterReceiver()?
12-12 03:23:09.017: E/ActivityThread(12554): android.app.IntentReceiverLeaked: Activity com.example.bluetoothc.settings has leaked IntentReceiver com.example.bluetoothc.settings$2@4151ecb8 that was originally registered here. Are you missing a call to unregisterReceiver()?
12-12 03:23:09.017: E/ActivityThread(12554): at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:795)
12-12 03:23:09.017: E/ActivityThread(12554): at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:596)
12-12 03:23:09.017: E/ActivityThread(12554): at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1316)
12-12 03:23:09.017: E/ActivityThread(12554): at android.app.ContextImpl.registerReceiver(ContextImpl.java:1296)
12-12 03:23:09.017: E/ActivityThread(12554): at android.app.ContextImpl.registerReceiver(ContextImpl.java:1290)
12-12 03:23:09.017: E/ActivityThread(12554): at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:423)
12-12 03:23:09.017: E/ActivityThread(12554): at com.example.bluetoothc.settings.onCreate(settings.java:106)
12-12 03:23:09.017: E/ActivityThread(12554): at android.app.Activity.performCreate(Activity.java:5104)
12-12 03:23:09.017: E/ActivityThread(12554): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-12 03:23:09.017: E/ActivityThread(12554): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-12 03:23:09.017: E/ActivityThread(12554): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-12 03:23:09.017: E/ActivityThread(12554): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-12 03:23:09.017: E/ActivityThread(12554): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-12 03:23:09.017: E/ActivityThread(12554): at android.os.Handler.dispatchMessage(Handler.java:99)
12-12 03:23:09.017: E/ActivityThread(12554): at android.os.Looper.loop(Looper.java:137)
12-12 03:23:09.017: E/ActivityThread(12554): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-12 03:23:09.017: E/ActivityThread(12554): at java.lang.reflect.Method.invokeNative(Native Method)
12-12 03:23:09.017: E/ActivityThread(12554): at java.lang.reflect.Method.invoke(Method.java:511)
12-12 03:23:09.017: E/ActivityThread(12554): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-12 03:23:09.017: E/ActivityThread(12554): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-12 03:23:09.017: E/ActivityThread(12554): at dalvik.system.NativeStart.main(Native Method)
12-12 03:23:09.814: I/System.out(12554): making connection to remote device
12-12 03:23:09.814: I/System.out(12554): ConnectedThread started
12-12 03:23:09.814: I/System.out(12554): creating socket
12-12 03:23:09.822: I/System.out(12554): ConnectedThread statusThread[Thread-920,5,main]
12-12 03:23:09.822: I/System.out(12554): State connected
12-12 03:23:09.837: I/System.out(12554): 1
12-12 03:23:09.876: D/AndroidRuntime(12554): Shutting down VM
12-12 03:23:09.876: W/dalvikvm(12554): threadid=1: thread exiting with uncaught exception (group=0x40c25930)
12-12 03:23:09.876: E/AndroidRuntime(12554): FATAL EXCEPTION: main
12-12 03:23:09.876: E/AndroidRuntime(12554): java.lang.NullPointerException
12-12 03:23:09.876: E/AndroidRuntime(12554): at com.example.bluetoothc.BluetoothService.write(BluetoothService.java:133)
12-12 03:23:09.876: E/AndroidRuntime(12554): at com.example.bluetoothc.MainActivity.SendBytes(MainActivity.java:150)
12-12 03:23:09.876: E/AndroidRuntime(12554): at com.example.bluetoothc.MainActivity.getAccelerometer(MainActivity.java:140)
12-12 03:23:09.876: E/AndroidRuntime(12554): at com.example.bluetoothc.MainActivity.onSensorChanged(MainActivity.java:111)
12-12 03:23:09.876: E/AndroidRuntime(12554): at android.hardware.SystemSensorManager$ListenerDelegate$1.handleMessage(SystemSensorManager.java:204)
12-12 03:23:09.876: E/AndroidRuntime(12554): at android.os.Handler.dispatchMessage(Handler.java:99)
12-12 03:23:09.876: E/AndroidRuntime(12554): at android.os.Looper.loop(Looper.java:137)
12-12 03:23:09.876: E/AndroidRuntime(12554): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-12 03:23:09.876: E/AndroidRuntime(12554): at java.lang.reflect.Method.invokeNative(Native Method)
12-12 03:23:09.876: E/AndroidRuntime(12554): at java.lang.reflect.Method.invoke(Method.java:511)
12-12 03:23:09.876: E/AndroidRuntime(12554): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-12 03:23:09.876: E/AndroidRuntime(12554): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-12 03:23:09.876: E/AndroidRuntime(12554): at dalvik.system.NativeStart.main(Native Method)
12-12 03:23:13.884: I/Process(12554): Sending signal. PID: 12554 SIG: 9
答案 0 :(得分:2)
有一个非常清楚的原因。
Activity com.example.bluetoothc.settings has leaked IntentReceiver com.example.bluetoothc.settings$2@4151ecb8 that was originally registered here. Are you missing a call to unregisterReceiver()?
您正在泄漏IntentReceiver。当一个事件被路由到Receiver(因为你没有注销它)时,很可能在该活动被暂停/销毁之后,它会在某处抛出NullPointerException,因为它丢失了所有上下文引用,或者它们不再有效。
答案 1 :(得分:0)
对象“r”的实例为空?检查mConnectedThread是否为null。如果你调用null引用的函数 - &gt;你有NullPointerException。
答案 2 :(得分:0)
这里可能r
为空或未初始化