我有一个带有主线程和子线程的android应用程序。我需要子线程定期向主线程发送消息。主线程依次更新UI。我曾尝试使用处理程序、获取消息((和发送消息((等来完成此过程,但当主线程更新textView时,我的应用程序失败了,即使我将收到的消息打印到日志中,这也很好。
所以我尝试了另一种方法,在子线程中发布一个可运行的程序来更新UI
在主线程中:
public class Example extends Activity {
public TextView mMatchesText;
public Handler mHandler;
private ServerConnection conn;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mMatchesText = (TextView) Example.this.findViewById(R.id.matches);
mMatchesText.setText("Matches:n"); /*this text appears when I run the app*/
mHandler = new Handler();
SessionEvents.addAuthListener(new SampleAuthListener());
public class SampleAuthListener implements AuthListener {
public void onAuthSucceed() {
Example.this.runOnUiThread(new Runnable() {
public void run() {
conn = new ServerConnection(mProfile.toString());
}
});
}
}
在子线程(ServerConnection.java(中:
/* constructor */
public ServerConnection()
{
runner = new Thread(this);
runner.start();
}
public void run()
{
String fromServer;
/** Establish connection to the server */
/** Wait for messages from the server */
while((fromServer = inFromServer.readLine()) != null)
{
mHandler.post(new Runnable(){
@Override
public void run(){
Log.e("MY APP", fromServer);
mMatchesText.setText(fromServer);
}
});
}
}
应用程序在第56行再次失败,该行为mMatcheText.setText(fromServer(formServer不为空,因为我将其打印到LogCat,并且它确实包含服务器发送的数据。
11-24 08:02:49.622: ERROR/AndroidRuntime(14571): FATAL EXCEPTION: main
11-24 08:02:49.622: ERROR/AndroidRuntime(14571): java.lang.NullPointerException
11-24 08:02:49.622: ERROR/AndroidRuntime(14571): at com.facebook.android.ServerConnection$1.run(ServerConnection.java:56) <--this is mMatches..
11-24 08:02:49.622: ERROR/AndroidRuntime(14571): at android.os.Handler.handleCallback(Handler.java:587)
11-24 08:02:49.622: ERROR/AndroidRuntime(14571): at android.os.Handler.dispatchMessage(Handler.java:92)
11-24 08:02:49.622: ERROR/AndroidRuntime(14571): at android.os.Looper.loop(Looper.java:130)
11-24 08:02:49.622: ERROR/AndroidRuntime(14571): at
android.app.ActivityThread.main(ActivityThreads.java:3683(
为了以防万一,mMatchsText在main.xml中声明如下:
<TextView android:id="@+id/matches"
android:textColor="@drawable/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/Login"
android:text=""
/>
有人能帮忙吗?
mMatchesText
在此处使用时为空:mMatchesText.setText(fromServer);
除非你向我们展示你对它的初始化,否则我们只能帮你。
这就是我在所有应用程序中的做法。制作一个像这样的自定义Runnable类
public abstract class MyRunnable implements Runnable {
public Object data;
public MyRunnable setData(Object data){
this.data=data;
return this;
}
public MyRunnable setData(Object... dataArr){
data=dataArr;
return this;
}
}
然后您可以使用它将数据发送到Runnable
//this is you code from question, i modified the use of runnable and String fromServer
public void run()
{
String fromServer;
/** Establish connection to the server */
/** Wait for messages from the server */
while((fromServer = inFromServer.readLine()) != null)
{
mHandler.post(new MyRunnable(){
@Override
public void run(){
//here variable fromServer is not available inside the Runnable object
String fromServerCpy=(String)data;//getting the fromServer sent to MyRunnable
Log.e("MY APP", fromServerCpy);
mMatchesText.setText(fromServerCpy);
}
}.setData(fromServer));// !!! sending fromServer to MyRunnable data variable
}
}