我正在尝试在我的服务器计算机和我的Android智能手机之间创建一个通信系统。我终于得到了很好的工作成果。但是,我的android视图有问题。具体来说,我试图从没有扩展的类使用textview的方法“.setText()”。我将我的textview对象传递给该类,但方法“textview.setText()”没有任何失败并且对UI没有影响。让我在代码中向下,我想你会更清楚地理解并有机会尝试代码。
public class NetworkingActivity extends Activity {
public static String message;
public static EditText editext;
public static TextView textview;
private Connector connector;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void buttonClicked(View v){
editext = (EditText) findViewById(R.id.editText1);
textview = (TextView) findViewById(R.id.textview);
message = editext.getText().toString();
connector = new Connector(NetworkingActivity.this);
connector.start();
}
public void yenile(String respond){
textview.setText(respond);
editext.setText("");
}
public void yenileClicked(View v){
textview.setText(Connector.respond);
editext.setText("");
}
public static class Connector extends Thread{
private static final int PORT = 3588;
private static final String IpAddress = "XXX.XXX.XXX.XXX"; // of course X's have a int value
public static String respond;
private NetworkingActivity activity;
public Connector(NetworkingActivity activity){
this.activity = activity;
}
public void run() {
try {
Socket socket = new Socket(IpAddress, PORT);
PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
Scanner scanner = new Scanner(socket.getInputStream());
writer.println(NetworkingActivity.message);
respond = scanner.nextLine();
activity.yenile(respond);
socket.close();
} catch (Exception e) {
}
}
}
}
最后,我还要告诉你一件事。 japileClicked()方法由xml文件中的代码分配,该代码是通过按钮“android:onClick =”yenileClicked“”。问题是其他类的残疾继续几乎相同的方法yenile();