无法启动View.onclicklistener类型

时间:2015-01-22 18:56:40

标签: java

我在第174行收到此错误 代码工作正常,但在上次更新后,我收到此错误,无法启动类型View.onclicklistner 这是使用websockets的聊天功能的客户端代码

如何解决这个问题?

包de.lauridmeyer.com.tests;

import org.jwebsocket.api.WebSocketClientEvent;
import org.jwebsocket.api.WebSocketClientTokenListener;
import org.jwebsocket.api.WebSocketPacket;
import org.jwebsocket.client.plugins.rpc.Rpc;
import org.jwebsocket.client.plugins.rpc.RpcListener;
import org.jwebsocket.client.plugins.rpc.Rrpc;
import org.jwebsocket.client.token.BaseTokenClient;
import org.jwebsocket.kit.WebSocketException;
import org.jwebsocket.token.Token;
import org.jwebsocket.token.TokenFactory;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class SimpleJWAndroidClientActivity extends Activity implements WebSocketClientTokenListener{

    //GUI Elements
    private static TextView log;
    private static TextView Chatlog;
    private static EditText ipadress;
    private static EditText Message;
    private static Button send;
    private static Button conBtn;

    private String entered_msg=null;
    TextView customDialog_TextView;
    Button customDialog_Yes, customDialog_No;
    static final int CUSTOM_DIALOG_ID = 0;
    public static String decision=null;

    //stores if the SLider is changed by the user or the server
    private static Boolean isDragging=false;

    //stores the connection status
    private static Boolean connected=false;

    //used for the connection
    private static BaseTokenClient btc = new BaseTokenClient();//create a new instance of the base token client

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        log = (TextView)findViewById(R.id.log);//Textview to Display Messages
        ipadress=(EditText)findViewById(R.id.ipadress);//inputfield for the ipadress
        Chatlog=(TextView)findViewById(R.id.chat);
        conBtn= (Button)findViewById(R.id.conBtn);//(dis)connection button

        Message=(EditText)findViewById(R.id.Message);
        send= (Button)findViewById(R.id.Send);

        //add the listener for the slider and the (dis)connection Button
        conBtn.setOnClickListener(buttonConnectOnClickListener);

        //add the listener for the websocket connection
        btc.addListener(this);//add this class as a listener
        btc.addListener(new RpcListener());//add an rpc listener
        Rpc.setDefaultBaseTokenClient(btc);//set it to the default btc
        Rrpc.setDefaultBaseTokenClient(btc);//same here
        //set the connection status to "not connected" on startup
        changeConnectionStatus(false);

        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                entered_msg = Message.getText().toString();   
                Token firstToken = TokenFactory.createToken("com.lauridmeyer.tests.LauridsPlugIn","Welcomemessage");
                firstToken.setString("message",entered_msg);//add the message to the token
                try {
                    btc.sendToken(firstToken);//try to send the token to the server
                } catch (WebSocketException ex) {
                    outputText("error:"+ex.getMessage());//log errors
                }
            }
        });

    }
    private OnClickListener buttonConnectOnClickListener = new OnClickListener() {
        public void onClick(View v){
            if(!connected){//if not connected
                try{
                    String ipAdress=ipadress.getText().toString();//get the ip-adress from the inputfield
                    outputText("connecting to "+ipAdress+" ...");//debug
                    btc.open(ipAdress);//try to open the connection to your server
                }catch(Exception e){
                    outputText("Error while connecting...");//log errors
                }
            }else{//if connected
                try{
                    btc.close();//try to close
                }catch(Exception e){
                    outputText(e.toString());//log errors
                }
            }
        }
    };
//    private static SeekBar.OnSeekBarChangeListener seekbarchangedListener = new SeekBar.OnSeekBarChangeListener(){
//        //Method is fired everytime the seekbar is changed
//        @Override
//        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//          if(isDragging && connected){//if the ssekbar is changed by the user and not by the server
//              //create a new Token with the namspace, and type
//                Token tempToken = TokenFactory.createToken("com.lauridmeyer.tests.LauridsPlugIn","sliderChanged");
//                tempToken.setInteger("value",progress);//add an integer to the token
//                try {
//                  btc.sendToken(tempToken);//try to send the token to the server
//              } catch (WebSocketException ex) {
//                  outputText("error:"+ex.getMessage());//log errors
//              }
//          }
//        }
//
//        @Override
//        public void onStartTrackingTouch(SeekBar seekBar) {
//          isDragging=true;//turn the sending mechanism on
//        }
//
//        @Override
//        public void onStopTrackingTouch(SeekBar seekBar) {
//          isDragging=false;//turn the sending mechanism off
//        }
//    };

    /* Method is called when the connection status has changed
     * connected <-> disconnected */
    public static void changeConnectionStatus(Boolean isConnected) {
        connected=isConnected;//change variable
        //seekBar.setEnabled(isConnected);//enable/disable seekbar
        if(isConnected){//if connection established
            outputText("successfully connected to server");//log
            conBtn.setText("DISCONNECT");//change Buttontext
        }else{
            outputText("disconnected from Server!");//log
            conBtn.setText("CONNECT");//change Buttontext
        }
    }

    /* Method prints text to the log textfield*/
    public static void outputText(String msg) {
        log.append(msg+"\n");
    }

    /* Message handler is used to process incoming tokens from the server*/
    private Handler messageHandler = new Handler() {
        @Override
        public void handleMessage(Message aMessage) {
            if(aMessage.what==0){//if it's a token
                Token aToken =(Token) aMessage.obj;//get the received token

                //if the namespace matches my plugin
                if(aToken.getNS().equals("com.lauridmeyer.tests.LauridsPlugIn")){
                    //and it's a command that the slider has changed
                    //String wM = aToken.getString("Welcomemessage");
                    if(aToken.getString("reqType").equals("sliderHasChanged")){
                        int value=aToken.getInteger("value");//get the slider value
                        isDragging=false;//make sure that the slider changes are not recognized as user inputs
                        //seekBar.setProgress(value);//move the slider to the recieved position
                    }
                    if(aToken.getString("reqType").equals("Welcomemessage")){
                        String message=aToken.getString("Welcomemessage");//get the slider value (here)

                         Chatlog.setOnClickListener(new View.OnClickListener());
                        Toast.makeText(getApplicationContext(),message,Toast.LENGTH_LONG).show();

                    }

                }
            }

        }
    };


    @Override
    public void processOpened(WebSocketClientEvent aEvent) {
        changeConnectionStatus(true);//when connected change the status
    }

    @Override
    public void processClosed(WebSocketClientEvent aEvent) {
        changeConnectionStatus(false);//when disconnected change the status
    }

    @Override
    public void processToken(WebSocketClientEvent aEvent, Token aToken) {
        //for some reason you can't process the token directly
        //you have to use the messagehandler
        Message lMsg = new Message();//create a new mess
        lMsg.what = 0;
        lMsg.obj = aToken;
        messageHandler.sendMessage(lMsg);
    }

    //following Methods are not used in this example, but have to be there :)
    @Override
    public void processPacket(WebSocketClientEvent aEvent, WebSocketPacket aPacket) {
    }

    @Override
    public void processOpening(WebSocketClientEvent aEvent) {
    }

    @Override
    public void processReconnecting(WebSocketClientEvent aEvent) {
    }
}

1 个答案:

答案 0 :(得分:0)

该行:

Chatlog.setOnClickListener(new View.OnClickListener());

您似乎没有覆盖onClick()方法。