Android - Autobahn Websocket sending message error(NullPointerException)

混江龙づ霸主 提交于 2019-12-24 19:55:35

问题


I am working on websocket communication with Autobahn. On the main.class of my app, I set to call 'connect()' when users click a button.

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

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

            if(isChecked){

            }else{

            }

        }
    });

And after that, there is MyOffers.class, and if this class is accessed, MyOffers_Fragment class is produced four times automatically, because MyOffers.class contains 'carousel view' and there are four products.

On 'MyOffers_Fragment' class, when users click one of the image of products, message should be sent.

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);  
        }
    });
}

But 'mConnection.sendTextMessage(id1);' this line makes 'NullPointerException' error. There is a class '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); 
     }

 }

I called 'connect()' in the main page class, and after that try to send message. But, it's not working..Can you please help me out?


回答1:


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");
    }

 }

 }


来源:https://stackoverflow.com/questions/17190843/android-autobahn-websocket-sending-message-errornullpointerexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!