1

这是我的代码示例

     TextView textStatus = (TextView) findViewById(R.id.editText1);
     ServerSocket serverSocket = new ServerSocket(SERVERPORT);
     .
     .
     Socket client = serverSocket.accept();
     .
     .
     textStatus.append("TEXT");
     client.close();

这使得android应用程序强制关闭。如果我删除 textStatus.append,它就会起作用。同样,如果我删除Socket client = ...and client.close(),它会在屏幕上显示文本。

因此,套接字client = serverSocket.accept();以某种方式影响 textStatus 变量。

谁能告诉我出了什么问题?

4

1 回答 1

1

textStatus 可能为空。检查以确保 R.id.editText1 是您在 XML 文件中使用的实际 id。

此外,任何可能长时间运行的任务都不应该在 UI 线程上完成。你会有非常糟糕的应用程序性能。请参阅Painless Threading文章并尝试将您的服务器通信移至不同的线程。或者考虑改用IntentService。我更喜欢 IntentService。

于 2011-10-20T03:28:51.203 回答