我的蓝牙应用程序运行良好,直到我使用onTouchListener方法,当我点击底部时,应用程序崩溃。我有几个按钮,所有这些按钮最终崩溃,下面是我的主要活动代码和崩溃日志的一部分,你能帮我找到错误吗?如果问题是模糊或缺乏信息,请告诉我。
public class MainActivity extends AppCompatActivity {
Button forward_btn,forward_left_btn , forward_right_btn , reverse_btn , reverse_right_btn , reverse_left_btn , btnConexao ;
public static final int SOLICITA_ATIVACAO = 1; // é o codigo numero 1 , é diferenciado o codigo pq pode haver varias solicitações na msma tela
public static final int SOLICITA_CONEXAO = 2;
BluetoothAdapter meuBluetoothAdapter=null; //declarar o meu adptador bluetooth
BluetoothDevice meuDevice=null;
BluetoothSocket meuSocket=null;// faz transiçao dos dados
boolean conexao = false; //variavel para a conexao que indica se a conexao está em andamento ou nao
private OutputStream outputStream;
private static String MAC = null;
String command; //string variable that will store value to be transmitted to the bluetooth module
UUID MEU_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); // canal de comunicaçao bluetooth , UUID- ID do canal
//protocolo Rfcomm
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
forward_btn = (Button) findViewById(R.id.forward_btn); //definir botoes e associar ao ficheiro xml
forward_left_btn = (Button) findViewById(R.id.forward_left_btn);
forward_right_btn = (Button) findViewById(R.id.forward_right_btn);
reverse_btn = (Button) findViewById(R.id.reverse_btn);
reverse_left_btn = (Button) findViewById(R.id.reverse_left_btn);
reverse_right_btn = (Button) findViewById(R.id.reverse_right_btn);
btnConexao = (Button) findViewById(R.id.btnConexao);
forward_btn.setOnTouchListener(new View.OnTouchListener() {
@Override
// comando para o botao frente , forward button
public boolean onTouch(View v, MotionEvent event) { // comandos bluetooth correspondentes do arduino
if (event.getAction() == MotionEvent.ACTION_DOWN) //MotionEvent.ACTION_DOWN é quando botao é segurado
{
command = "1";
try
{
outputStream.write(command.getBytes()); //transmite o valor do comando para o modulo bluetooth
}
catch (IOException e)
{
e.printStackTrace();
}
return true;
}
else if(event.getAction() == MotionEvent.ACTION_UP)
{
command = "10";
try
{
outputStream.write(command.getBytes());
}
catch(IOException e)
{
e.printStackTrace();
}
}
return false;
}
});
崩溃日志:
12-21 15:57:01.117 14520-14520/? I/art: Late-enabling -Xcheck:jni
12-21 15:57:01.117 14520-14520/? D/houdini: [14520] Initialize library(version: 5.0.7a_y.48167 RELEASE)... successfully.
12-21 15:57:01.184 14520-14520/com.example.noazevedo.appbluetoothfinal I/InstantRun: Instant Run Runtime started. Android package is com.example.noazevedo.appbluetoothfinal, real application class is null.
12-21 15:57:01.426 14520-14520/com.example.noazevedo.appbluetoothfinal V/Monotype: SetAppTypeFace- try to flip, app = com.example.noazevedo.appbluetoothfinal
12-21 15:57:01.427 14520-14520/com.example.noazevedo.appbluetoothfinal V/Monotype: Typeface getFontPathFlipFont - systemFont = default#default
12-21 15:57:01.430 14520-14520/com.example.noazevedo.appbluetoothfinal V/Monotype: SetAppTypeFace- try to flip, app = com.example.noazevedo.appbluetoothfinal
12-21 15:57:01.430 14520-14520/com.example.noazevedo.appbluetoothfinal V/Monotype: Typeface getFontPathFlipFont - systemFont = default#default
12-21 15:57:01.596 14520-14520/com.example.noazevedo.appbluetoothfinal W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
12-21 15:57:01.633 14520-14520/com.example.noazevedo.appbluetoothfinal V/Monotype: SetAppTypeFace- try to flip, app = com.example.noazevedo.appbluetoothfinal
12-21 15:57:01.634 14520-14520/com.example.noazevedo.appbluetoothfinal V/Monotype: Typeface getFontPathFlipFont - systemFont = default#default
12-21 15:57:01.873 14520-14584/com.example.noazevedo.appbluetoothfinal D/OpenGLRenderer: Render dirty regions requested: true
12-21 15:57:01.879 14520-14520/com.example.noazevedo.appbluetoothfinal D/Atlas: Validating map...
12-21 15:57:01.941 14520-14584/com.example.noazevedo.appbluetoothfinal I/OpenGLRenderer: Initialized EGL, version 1.4
12-21 15:57:01.942 14520-14584/com.example.noazevedo.appbluetoothfinal W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
12-21 15:57:01.952 14520-14584/com.example.noazevedo.appbluetoothfinal D/OpenGLRenderer: Enabling debug mode 0
12-21 15:57:13.907 14520-14520/com.example.noazevedo.appbluetoothfinal E/InputEventReceiver: Exception dispatching input event.
12-21 15:57:13.907 14520-14520/com.example.noazevedo.appbluetoothfinal E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
12-21 15:57:13.909 14520-14520/com.example.noazevedo.appbluetoothfinal E/MessageQueue-JNI: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.OutputStream.write(byte[])' on a null object reference
at com.example.noazevedo.appbluetoothfinal.MainActivity$1.onTouch(MainActivity.java:70)
at android.view.View.dispatchTouchEvent(View.java:8382)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2314)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1692)
at android.app.Activity.dispatchTouchEvent(Activity.java:2781)
at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:63)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2275)
at android.view.View.dispatchPointerEvent(View.java:8578)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4021)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3887)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3468)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3578)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3476)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3635)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3468)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3476)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5701)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5675)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5646)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5791)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:143)
at android.os.Looper.loop(Looper.java:122)
at android.app.ActivityThread.main(ActivityThread.java:5264)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:900)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:695)
12-21 15:57:13.909 14520-14520/com.example.noazevedo.appbluetoothfinal D/AndroidRuntime: Shutting down VM
--------- beginning of crash
12-21 15:57:13.909 14520-14520/com.example.noazevedo.appbluetoothfinal E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.noazevedo.appbluetoothfinal, PID: 14520
java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.OutputStream.write(byte[])' on a null object reference
at com.example.noazevedo.appbluetoothfinal.MainActivity$1.onTouch(MainActivity.java:70)
at android.view.View.dispatchTouchEvent(View.java:8382)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2314)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1692)
at android.app.Activity.dispatchTouchEvent(Activity.java:2781)
at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:63)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2275)
at android.view.View.dispatchPointerEvent(View.java:8578)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4021)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3887)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3468)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3578)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3476)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3635)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3468)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3476)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5701)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5675)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5646)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5791)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:143)
at android.os.Looper.loop(Looper.java:122)
at android.app.ActivityThread.main(ActivityThread.java:5264)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:900)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:695)
答案 0 :(得分:2)
看起来你可能因为outputStream
尚未初始化而获得NPE。