Android - Autobahn Websocket发送消息错误(NullPointerException)

时间:2013-06-19 12:11:34

标签: android nullpointerexception websocket autobahn

我正在与Autobahn进行websocket通信。 在我的应用程序的main.class上,当用户单击按钮时,我设置为调用'connect()'。

// Toggle Button event
    tButton = (ToggleButton) findViewById(R.id.toggleButton1);    
    tButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {

            if(isChecked){

            }else{

            }

        }
    });

之后,有MyOffers.class,如果访问此类, MyOffers_Fragment类自动生成四次,因为MyOffers.class包含'carousel view',并且有四个产品。

在“MyOffers_Fragment”类中,当用户单击其中一个产品图像时,应发送消息。

if (pos == 0) {
    product_photo.setImageResource(R.drawable.myoffers_0);
    product_photo.setOnClickListener(new ImageButton.OnClickListener(){
        public void onClick(View v){
            String id = "Product0";
            Log.d(TAG, "Current product is : " + id);
            A.sendMessage(id);  
        }
    });
}

但'mConnection.sendTextMessage(id1);'这一行会导致'NullPointerException'错误。 有一个类'Websocket_Connector.class'

public class WebSocket_Connector {

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

    public void connect(final String wsuri) {

          Log.d(TAG, "Connecting to: " + wsuri); 

          try {
             mConnection.connect(wsuri, new WebSocketHandler() {

                @Override
                public void onOpen() {
                   Log.d(TAG, "Status: Connected to " + wsuri ); 
                   Log.d(TAG, "Connection successful!\n");
                }

                @Override
                public void onTextMessage(String payload) {
                   Log.d(TAG, "Got echo: " + payload);
                }

                @Override
                public void onClose(int code, String reason) {
                   Log.d(TAG, "Connection closed.");
                }
             });
          } catch (WebSocketException e) {

             Log.d(TAG, e.toString());
          }

     public void sendMessage(String message) {
        connect("ws://192.168.x.xxx:xxxx");
        mConnection.sendTextMessage(message); 
     }

 }

我在主页面类中调用了'connect()',之后尝试发送消息。 但是,它没有用。你可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

public class WebSocket_Connector {

private static final String TAG = "ECHOCLIENT";
public final WebSocketConnection mConnection = new WebSocketConnection();
private String tmpString = "";
public void connect(final String wsuri) {

      Log.d(TAG, "Connecting to: " + wsuri); 

      try {
         mConnection.connect(wsuri, new WebSocketHandler() {

            @Override
            public void onOpen() {
               Log.d(TAG, "Status: Connected to " + wsuri ); 
               Log.d(TAG, "Connection successful!\n");
               mConnection.sendTextMessage(tmpString); 
               tmpString = "";
            }

            @Override
            public void onTextMessage(String payload) {
               Log.d(TAG, "Got echo: " + payload);
            }

            @Override
            public void onClose(int code, String reason) {
               Log.d(TAG, "Connection closed.");
            }
         });
      } catch (WebSocketException e) {

         Log.d(TAG, e.toString());
      }

 public void sendMessage(String message) {

    if (mConnection.isConnected()) 
            mConnection.sendTextMessage(message); 
    else {
       tmpString = message;
       connect("ws://192.168.x.xxx:xxxx");
    }

 }

 }