我有一个名为RandoMessenger的课程。该类有一个名为message的字符串数据成员。我有一个EditText组件。该组件有一个监听器,用于指示用户何时按下Enter键。执行此操作后,变量消息将设置为用户键入的文本。稍后在程序中(侦听器外部),我有一个if语句,用于检查消息是否为空。如果验证,则将消息传递给服务器。这个if语句永远不会验证,除非我在声明时将messenger设置为某个东西。相关评论全部包含在下面的代码中。
我正在测试Andy模拟器4.4.2和三星Galaxy S3 4.4.2
package com.Rando;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.Editable;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.EditText;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class RandoMessenger extends ActionBarActivity {
public PrintWriter out;
public BufferedReader in;
//THE IF STATEMENT VALIDATES IF I SET MESSAGE TO SOMETHING HERE. IT DOES NOT VALIDATE
// IF MESSAGE IS SET IN THE LISTENER BELOW
public String message="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rando_messenger);
new Thread(){
public void run(){
final EditText edit1 = (EditText) findViewById(R.id.editText2);
final EditText edit = (EditText) findViewById(R.id.editText1);
edit.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
//MESSAGE IS INITIALIZED HERE INSIDE THE LISTENER BUT THE IF STATEMENT
//BELOW NEVER VALIDATES INDICATING MESSAGE IS "" OUTSIDE THE LISTENER
message=edit.getText().toString();
edit1.append("\n");
edit1.append("Listener Works");
edit.setText("");
return true;
}
return false;
}
});
//Edited out the actual IP
String serverAddress = "00.000.00.000";
Socket socket;
try {
socket = new Socket(serverAddress, 9001);
in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
while (true) {
String line = in.readLine();
//THIS IF STATEMENT DOES NOT VALIDATE WHEN MESSAGE IS SET
//INSIDE THE LISTENER ABOVE
if(message != "")
{
out.println("Test");
edit1.setText(message);
message="";
}
if (line.startsWith("SUBMITNAME")) {
out.println("User");
} else if (line.startsWith("NAMEACCEPTED")) {
edit1.append("Connection Successful");
} else if (line.startsWith("MESSAGE")) {
//Append message to output field here
edit1.append("\n");
edit1.append(line);
}
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
}.start();
//Temporary dialog for server IP input
/*******************************************************************/
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Server");
alert.setMessage("Enter the server IP address:");
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Editable value = input.getText();
//edit1.append(value);
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.rando_messenger, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:0)
我认为主要的错误是使用message != ""
来比较消息。在Java中,应使用!string1.equals(string2)
或本例!message.equals("")
进行字符串比较。