试图从一个活动发送消息到另一个空指针异常 [英] Null Pointer Exception when trying to send message from one activity to another
问题描述
为什么会出现NullPointerException异常错误,当我试图实现code,它发送一个字符串消息从一个活动延伸服务的BluetoothService活动。
下面是涉及发送字符串code的主要部分:
私人BluetoothService mChatService = NULL;onCaptureButtonClicked(查看视图)公共无效抛出IOException
尝试{
的sendMessage(1);
}赶上(NullPointerException异常五){
e.printStackTrace();
} takePhoto(1);
}
}私人无效的sendMessage(字符串消息){ 如果(mChatService.getState()!= BluetoothService.STATE_CONNECTED){
返回;
} 如果(message.length()大于0){
字节[] =发送message.getBytes();
mChatService.write(发送);
}
}
以下是错误的logcat:
02-15 18:21:19.960:W / System.err的(1252):显示java.lang.NullPointerException
02-15 18:21:19.960:W / System.err的(1252):在com.example.application.parss.CameraActivity.sendMessage(CameraActivity.java:751)
02-15 18:21:19.960:W / System.err的(1252):在com.example.application.parss.CameraActivity.onCaptureButtonClicked(CameraActivity.java:245)
02-15 18:21:19.960:W / System.err的(1252):在java.lang.reflect.Method.invokeNative(本机方法)
02-15 18:21:19.960:W / System.err的(1252):在java.lang.reflect.Method.invoke(Method.java:507)
02-15 18:21:19.960:W / System.err的(1252):在android.view.View $ 1.onClick(View.java:2180)
02-15 18:21:19.960:W / System.err的(1252):在android.view.View.performClick(View.java:2585)
02-15 18:21:19.960:W / System.err的(1252):在android.view.View $ PerformClick.run(View.java:9299)
02-15 18:21:19.960:W / System.err的(1252):在android.os.Handler.handleCallback(Handler.java:587)
02-15 18:21:19.960:W / System.err的(1252):在android.os.Handler.dispatchMessage(Handler.java:92)
02-15 18:21:19.960:W / System.err的(1252):在android.os.Looper.loop(Looper.java:130)
02-15 18:21:19.960:W / System.err的(1252):在android.app.ActivityThread.main(ActivityThread.java:3691)
02-15 18:21:19.960:W / System.err的(1252):在java.lang.reflect.Method.invokeNative(本机方法)
02-15 18:21:19.970:W / System.err的(1252):在java.lang.reflect.Method.invoke(Method.java:507)
02-15 18:21:19.970:W / System.err的(1252):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:912)
02-15 18:21:19.970:W / System.err的(1252):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
02-15 18:21:19.970:W / System.err的(1252):在dalvik.system.NativeStart.main(本机方法)
编辑:
我intialised的mChatSession是这样的:
mChatService =新BluetoothService(这一点,mHandler);
和称这个:
mChatService.write(发送);
但类似NullPointerException异常错误仍然存在。
BluetoothService mChatService =新BluetoothService(参数);
您需要在某一时刻的变量分配一个实例。
Why is there NullPointerException error when I try to implement the code which sends a string message from one activity to the BluetoothService Activity which extends Service.
Here is the main parts of the code that is involved in sending the string:
private BluetoothService mChatService = null;
public void onCaptureButtonClicked(View view) throws IOException{
try{
sendMessage("1");
}catch(NullPointerException e){
e.printStackTrace();
}
takePhoto(1);
}
}
private void sendMessage(String message) {
if (mChatService.getState() != BluetoothService.STATE_CONNECTED) {
return;
}
if (message.length() > 0) {
byte[] send = message.getBytes();
mChatService.write(send);
}
}
Here is the error logcat:
02-15 18:21:19.960: W/System.err(1252): java.lang.NullPointerException
02-15 18:21:19.960: W/System.err(1252): at com.example.application.parss.CameraActivity.sendMessage(CameraActivity.java:751)
02-15 18:21:19.960: W/System.err(1252): at com.example.application.parss.CameraActivity.onCaptureButtonClicked(CameraActivity.java:245)
02-15 18:21:19.960: W/System.err(1252): at java.lang.reflect.Method.invokeNative(Native Method)
02-15 18:21:19.960: W/System.err(1252): at java.lang.reflect.Method.invoke(Method.java:507)
02-15 18:21:19.960: W/System.err(1252): at android.view.View$1.onClick(View.java:2180)
02-15 18:21:19.960: W/System.err(1252): at android.view.View.performClick(View.java:2585)
02-15 18:21:19.960: W/System.err(1252): at android.view.View$PerformClick.run(View.java:9299)
02-15 18:21:19.960: W/System.err(1252): at android.os.Handler.handleCallback(Handler.java:587)
02-15 18:21:19.960: W/System.err(1252): at android.os.Handler.dispatchMessage(Handler.java:92)
02-15 18:21:19.960: W/System.err(1252): at android.os.Looper.loop(Looper.java:130)
02-15 18:21:19.960: W/System.err(1252): at android.app.ActivityThread.main(ActivityThread.java:3691)
02-15 18:21:19.960: W/System.err(1252): at java.lang.reflect.Method.invokeNative(Native Method)
02-15 18:21:19.970: W/System.err(1252): at java.lang.reflect.Method.invoke(Method.java:507)
02-15 18:21:19.970: W/System.err(1252): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
02-15 18:21:19.970: W/System.err(1252): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
02-15 18:21:19.970: W/System.err(1252): at dalvik.system.NativeStart.main(Native Method)
EDIT:
I intialised the mChatSession in this way:
mChatService = new BluetoothService(this, mHandler);
And called this: mChatService.write(send);
But the similar NullPointerException error still persists.
BluetoothService mChatService = new BluetoothService(args);
You need to assign an instance at some point to the variable.
这篇关于试图从一个活动发送消息到另一个空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!