2013-06-19 57 views
3

我正在websocket通信。 從android設備(客戶端)到基於linux的PC(服務器)。 我成功地將websocket連接到服務器。但問題是我發送數據失敗(字符串值)Android - NullPointerException錯誤

有一個傳送帶視圖與四個產品。所以,當我點擊product0的照片時,我將字符串設置爲「product0」並將此字符串值發送給服務器。 我正在使用Autobahn庫。

的代碼是在這裏

import de.tavendo.autobahn.WebSocketConnection; 

public class Myoffers_Fragment extends Fragment { 

    private static final String TAG = "Philips"; 
    private final WebSocketConnection mConnection = new WebSocketConnection(); 

    public static Fragment newInstance(Myoffers context, int pos, float scale) 
    { 
     Bundle b = new Bundle(); 
     b.putInt("pos", pos); 
     b.putFloat("scale", scale); 
     return Fragment.instantiate(context, Myoffers_Fragment.class.getName(), b); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     if (container == null) { 
      return null; 
     } 

     LinearLayout l = (LinearLayout) inflater.inflate(R.layout.mf, container, false); 

     int pos = this.getArguments().getInt("pos"); 
     TextView tv = (TextView) l.findViewById(R.id.text); 
     tv.setText("Product " + pos); 



     ImageButton product_photo = (ImageButton) l.findViewById(R.id.myoffer_image); 


     if (pos == 0) { 
      product_photo.setImageResource(R.drawable.myoffers_0); 
      product_photo.setOnClickListener(new ImageButton.OnClickListener(){ 
       public void onClick(View v){ 
        String id1 = "Product0"; 
        Log.d(TAG, "Current product is : " + id1); 
        mConnection.sendTextMessage(id1); 
        Log.d(TAG, id1 + "is sent to server!"); 
       } 
      }); 
     } 

是否有可能「擴展片段」,使得錯誤?..是發生像下面 錯誤..當我點擊發生

06-19 12:02:01.310: E/AndroidRuntime(2712): FATAL EXCEPTION: main 
06-19 12:02:01.310: E/AndroidRuntime(2712): java.lang.NullPointerException 
06-19 12:02:01.310: E/AndroidRuntime(2712):  at de.tavendo.autobahn.WebSocketConnection.sendTextMessage(WebSocketConnection.java:137) 
06-19 12:02:01.310: E/AndroidRuntime(2712):  at com.example.philips.Myoffers_Fragment$1.onClick(Myoffers_Fragment.java:56) 
06-19 12:02:01.310: E/AndroidRuntime(2712):  at android.view.View.performClick(View.java:3511) 
06-19 12:02:01.310: E/AndroidRuntime(2712):  at android.view.View$PerformClick.run(View.java:14105) 
06-19 12:02:01.310: E/AndroidRuntime(2712):  at android.os.Handler.handleCallback(Handler.java:605) 
06-19 12:02:01.310: E/AndroidRuntime(2712):  at android.os.Handler.dispatchMessage(Handler.java:92) 
06-19 12:02:01.310: E/AndroidRuntime(2712):  at android.os.Looper.loop(Looper.java:137) 
06-19 12:02:01.310: E/AndroidRuntime(2712):  at android.app.ActivityThread.main(ActivityThread.java:4446) 
06-19 12:02:01.310: E/AndroidRuntime(2712):  at java.lang.reflect.Method.invokeNative(Native Method) 
06-19 12:02:01.310: E/AndroidRuntime(2712):  at java.lang.reflect.Method.invoke(Method.java:511) 
06-19 12:02:01.310: E/AndroidRuntime(2712):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
06-19 12:02:01.310: E/AndroidRuntime(2712):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
06-19 12:02:01.310: E/AndroidRuntime(2712):  at dalvik.system.NativeStart.main(Native Method) 

錯誤照片發送字符串值。

+0

你在哪裏用mConnection連接? –

+0

'newInstance()'的最後一行應該替換爲:'Myoffers_Fragment result = new Myoffers_Fragment(); result.setArguments(b)中;返回結果;'...因爲你似乎返回一個帶有空參數列表的片段。此外,該錯誤在'WebSocketConnection.sendTextMessage()'中拋出。你可以發佈該代碼嗎? – gunar

+0

@ gunar你想讓我發佈哪個代碼?.. Myoffers_Fragment是代碼頂部 – user2500696

回答

3

錯誤是發生在這裏你onClick

mConnection.sendTextMessage(id1); 

看起來你已經宣佈mConnection在頂部,但沒有取得任何聯繫。

看着​​,您需要在使用前致電mConnection,致電.connect()

WebSocketConnection.java行137:

public void sendTextMessage(String payload) { 
    mWriter.forward(new WebSocketMessage.TextMessage(payload)); 
} 

mWriter爲空,直到您調用.connect()Source code

因此,在使用mConnection對象之前,請確保您具有有效的連接(通過調用.connect())。

+0

錯誤然後,我應該在我的代碼上調用.connect()。因爲當我訪問轉盤視圖時,Myoffers_Fragment.class中會生成4個。 – user2500696

+0

不知道你的架構是什麼以及你想要做什麼,這很難說。就個人而言,如果我需要訪問這個連接很多,我會讓我的活動控制它,而不是各種碎片。 –

+0

而實際上,我做了Websocket_connector.class,並在該代碼中,我宣佈'公共無效連接(最終字符串wsuri)' – user2500696