2013-03-21 68 views
0

我已經讀了大量的SO線程,我仍然困惑。我需要發送一個艦船對象,用於戰列艦遊戲。我已經將ship類實現爲可序列化的,但我似乎無法發送該對象。我基本上只使用了我在網上找到的代碼,並將它合併到我的connectedThread代碼中(與android dev站點相同)。代碼如下。任何人都可以告訴我哪裏出錯了,非常感謝任何幫助。我得到的錯誤,當我運行的代碼,這似乎是從ObjectInputStream的側通過android上的藍牙發送對象,我如何serialise對象和發送?

public class ConnectedThread extends Thread { 
    private final BluetoothSocket mmSocket; 
    private final InputStream mmInStream; 
    private final OutputStream mmOutStream; 
    ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
    private ObjectOutput out = null; 
    byte[] yourBytes = new byte[1024]; 

    public ConnectedThread(BluetoothSocket socket) { 
     Log.d(TAG, "connectedthread started"); 
     // mHandler.obtainMessage(TEST).sendToTarget(); 
     mmSocket = socket; 
     InputStream tmpIn = null; 
     OutputStream tmpOut = null; 
     yourBytes = null; 

     // Get the input and output streams, using temp objects because 
     // member streams are final 
     try { 

      tmpIn = socket.getInputStream(); 
      tmpOut = socket.getOutputStream(); 
      out = new ObjectOutputStream(bos); 

     } catch (IOException e) { 
      Log.e(TAG, "temp sockets not created"); 
     } 

     mmInStream = tmpIn; 
     mmOutStream = tmpOut; 

    } 

    public void run() { 

     Log.i(TAG, "Begin mConnectedThread"); 
     byte[] buffer = new byte[1024]; // buffer store for the stream 
     int bytes; // bytes returned from read() 

     // Keep listening to the InputStream until an exception occurs 
     while (true) { 
      try { 
        // Read from the InputStream 
        bytes = mmInStream.read(buffer); 
        // Send the obtained bytes to the UI activity 
        Log.i(TAG, "reaaaad msg"); 
        mHandler.obtainMessage(SetUpGame.MESSAGE_READ2, bytes, 
          -1, buffer).sendToTarget(); 

       Log.i(TAG, "READ OBJECT"); 
        // ........................................THIS IS CODE THAT HAS ERRORS----------- 
        ByteArrayInputStream bis = new ByteArrayInputStream(yourBytes); 
        ObjectInput in = null; 
        try { 
         in = new ObjectInputStream(mmInStream); 
         Ship yourObject = (Ship) in.readObject(); 
         //Log.i("Object received maybe", yourObject.toString()); 
         if(yourObject!=null) 
         mHandler.obtainMessage(SetUpGame.MESSAGE_READ_OBJ, -1, -1, yourObject).sendToTarget(); 
        } catch (ClassNotFoundException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } finally { 
         bis.close(); 
         in.close(); 
        } 

// -----------------------------------IF I DELETE THE ABOVE CODE IT WORKS FOR STRINGS PERFECTLY------ 
       } 

      catch (IOException e) { 
       Log.e(TAG, "disconnectd"); 
       break; 
      } 
     } 
    } 

    /* 
    * Call this from the main activity to send data to the remote device 
    */ 
    public void write(byte[] buffer) { 

     try { 

      mmOutStream.write(buffer); 
      Log.i(TAG, "writeeee msg"); 
      mHandler.obtainMessage(SetUpGame.MESSAGE_WRITE, -1, -1, buffer) 
        .sendToTarget(); 
     } catch (IOException e) { 
      Log.e(TAG, "Exception during write"); 
     } 
    } 

    public void writeObj(Ship ship) { 
     bos = new ByteArrayOutputStream(); 
     ObjectOutput out = null; 
     try { 
      out = new ObjectOutputStream(mmOutStream); 
      out.writeObject(ship); 
      yourBytes = bos.toByteArray(); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    } 

    /* Call this from the main activity to shutdown the connection */ 
    public void cancel() { 
     try { 
      mmSocket.close(); 
      out.close(); 
      bos.close(); 
     } catch (IOException e) { 
      Log.e(TAG, "close of connect socket failed"); 
     } 
    } 

} 

和logcat的未來:

03-21 02:41:41.594: E/AndroidRuntime(12529): java.lang.NullPointerException 
03-21 02:41:41.594: E/AndroidRuntime(12529): at java.io.ByteArrayInputStream.<init>(ByteArrayInputStream.java:58) 
03-21 02:41:41.594: E/AndroidRuntime(12529): at com.example.battleships.v2.ChatService$ConnectedThread.run(ChatService.java:280) 
03-21 02:41:41.875: E/ActivityThread(12529): Activity com.example.battleships.v2.MainScreen has leaked IntentReceiver [email protected] that was originally registered here. Are you missing a call to unregisterReceiver()? 
03-21 02:41:41.875: E/ActivityThread(12529): android.app.IntentReceiverLeaked: Activity com.example.battleships.v2.MainScreen has leaked IntentReceiver [email protected] that was originally registered here. Are you missing a call to unregisterReceiver()? 
03-21 02:41:41.875: E/ActivityThread(12529): at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:799) 
03-21 02:41:41.875: E/ActivityThread(12529): at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:575) 
03-21 02:41:41.875: E/ActivityThread(12529): at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:865) 
03-21 02:41:41.875: E/ActivityThread(12529): at android.app.ContextImpl.registerReceiver(ContextImpl.java:852) 
03-21 02:41:41.875: E/ActivityThread(12529): at android.app.ContextImpl.registerReceiver(ContextImpl.java:846) 
03-21 02:41:41.875: E/ActivityThread(12529): at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:318) 
03-21 02:41:41.875: E/ActivityThread(12529): at com.example.battleships.v2.MainScreen.setUpUI(MainScreen.java:167) 
03-21 02:41:41.875: E/ActivityThread(12529): at com.example.battleships.v2.MainScreen.onCreate(MainScreen.java:160) 
03-21 02:41:41.875: E/ActivityThread(12529): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072) 
03-21 02:41:41.875: E/ActivityThread(12529): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1836) 
03-21 02:41:41.875: E/ActivityThread(12529): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893) 
+0

請更新您的帖子以包含您所得到的錯誤的描述。 – 2013-03-21 02:31:05

+0

我添加了一些我得到的錯誤,並明確了哪一部分給了我代碼中的錯誤。如果我刪除該代碼,它適用於字符串。另外,我對logcat錯誤的格式表示歉意。謝謝 – user1953208 2013-03-21 02:57:20

回答

0

看起來你已經被重新聲明它覆蓋了類變量「走出去」在writeObj()方法內部。刪除「ObjectOutput out = null;」來自writeObj()。

此外,當您不斷重置類變量「bos」時,您似乎還不確定要寫入哪個流。

您可能會遇到NullPointerException,因爲您正在嘗試使用不在您認爲的範圍內的變量。

+0

嗯,我會稍後再玩一下代碼,但我很困惑於通過藍牙發送對象。最糟糕的是我找不到類似項目的任何工作示例或解釋它的任何資源,就像我5 lol。謝謝你的時間壽! – user1953208 2013-03-21 17:42:48