[英]Inter-class communication in Android
我有一个名为TCPClient
的类(在Android中), TCPClient
从服务器接收字符串。 我需要发送给函数printmsg
类Showmsg
,这是在同一个包中TCPClient
。
以下代码不起作用。
public class TCPClient implements Runnable {
..
..
public void run() {
..
Showmsg obj = new Showmsg();
obj.printmsg("Hello");
}
}
在Showmsg
类中:
public void printmsg(String str) {
Toast( . . );
}
由于TCPClient
是Runnable
,因此我在给定代码中没有看到的是TCPClient
.start()
。 另外,我不知道您的toast(str)
方法如何工作,但不要忘记.show()
。 此代码应运行。
public class TCPClient implements Runnable {
public void run() {
Showmsg obj = new Showmsg();
obj.printmsg("Hello");
}
}
public class MyActivity {
TCPClient tcp;
public void onCreate(Bundle b) {
super.onCreate(b);
tcp = new TCPClient();
}
public void onResume() {
super.onResume();
tcp.start();
}
}
public class Showmsg {
public void printmsg(String str) {
toast(str);
}
private void toast(String str) {
Log.d(TAG, str);
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
System.out.println(str);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.