2016-12-21 173 views
-1

我的藍牙應用程序運行良好,直到我使用onTouchListener方法時,當我點擊底部時,應用程序崩潰。我有幾個按鈕,它們都會崩潰,波紋管是我的主要代碼和崩潰日誌的一部分,你能幫我找到錯誤嗎?如果問題是模糊或缺乏信息,請告訴我。使用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

你可以添加崩潰日誌嗎? – FarshidABZ

+0

我已編輯的問題與崩潰日誌包括現在 –

回答

2

看起來你很可能得到NPE作爲outputStream尚未初始化。

+0

我新來這個,我怎麼能初始化它? –

+0

初始化它的方式取決於你使用的是什麼....參見以下示例https://examples.javacodegeeks.com/core-java/io/outputstream/java-io-outputstream-example/ –

+0

我讀了例子,我想輸出流得到字符串「命令」的值併發送它,我看到了「寫字符方法,但我不知道如何實現它。 –

相關問題