2011-11-24 31 views
-2

我有一个带有主线程和子线程的android应用程序。我需要子线程定期发送消息到主线程。主线程依次更新UI。我试图使用处理程序,acquireMessage()和sendMessage()等来执行此过程,但是当主线程更新textView时,即使我将收到的消息打印到日志中,它仍然很好,但是我的应用程序失败。从子线程到主线程的Android通信

所以,我想发布一个可运行的子线程更新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是mMatchesText.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(ActivityThread.java:3683)

万一有帮助,将是mMatchesText 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="" 
/> 

灿有人帮忙?

+0

好像来自服务器是空的 – ngesh

+0

发布nMatchesText初始化的代码 – Jack

回答

1

mMatchesText当它用在这里是空:mMatchesText.setText(fromServer);

除非你告诉我们,你对它的初始化,这就是我们可以帮助你。

+0

我在 – NewToAndroid

+0

之上添加了一些编辑mMatchesText为null。请在提及的地方发布更多代码。你可以用XML声明它,但你仍然需要在你的Java中的某个地方初始化mMatchesText变量。 – John

+0

我在主线程中初始化mMatchesText。另外,为了确保它不为空,我写了一个测试字符串,它确实出现在屏幕上。但是,在子线程中,即使我尝试用虚拟字符串更新UI时也会发生错误。除了在子线程中尝试mHandler.post外,我还尝试在主线程中定义一个handleMessage()方法,该方法从子接收消息并更新UI,但也失败了。这让我疯狂!我在上面添加了更多代码。请在这个问题上帮助我。 – NewToAndroid