2

这个想法是开始聊天。所以我在我的课上有这个属性:

private MulticastSocket so;
private EditText messageBoard;
private InetAddress serverAddress;
private int port;

然后我在方法中有这个代码onCreate()

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second);

    // connect to server
    connect();

    // Associate a variable with the Button on the interface
    final Button sendButton = (Button) findViewById(R.id.button2);
    sendButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // when the button is clicked the next screen is loaded
            sendMessage();
        }
    });

}   // end of onCreate

这是我的connect()方法:

private void connect() {

    port = 4456;

    // convert the host name to InetAddress
    try {
        serverAddress = InetAddress.getByName("my server address is here");
    } catch (Exception e) {}

    // create socket and start communicating
    try {
        so = new MulticastSocket(port);
        so.joinGroup(serverAddress);
    } catch (IOException e) {}

    // start listening for incoming messages
    new Receiver(so, messageBoard);
}

一切对我来说都是正确的,但这就是它所说的:

01-24 23:33:16.277: W/dalvikvm(569): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
01-24 23:33:16.357: E/AndroidRuntime(569): FATAL EXCEPTION: main
01-24 23:33:16.357: E/AndroidRuntime(569): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.regeduser00x.proj1/com.regeduser00x.proj1.Second}: android.os.NetworkOnMainThreadException
01-24 23:33:16.357: E/AndroidRuntime(569):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
01-24 23:33:16.357: E/AndroidRuntime(569):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
01-24 23:33:16.357: E/AndroidRuntime(569):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
01-24 23:33:16.357: E/AndroidRuntime(569):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
01-24 23:33:16.357: E/AndroidRuntime(569):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-24 23:33:16.357: E/AndroidRuntime(569):  at android.os.Looper.loop(Looper.java:137)
01-24 23:33:16.357: E/AndroidRuntime(569):  at android.app.ActivityThread.main(ActivityThread.java:4424)
01-24 23:33:16.357: E/AndroidRuntime(569):  at java.lang.reflect.Method.invokeNative(Native Method)
01-24 23:33:16.357: E/AndroidRuntime(569):  at java.lang.reflect.Method.invoke(Method.java:511)
01-24 23:33:16.357: E/AndroidRuntime(569):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-24 23:33:16.357: E/AndroidRuntime(569):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-24 23:33:16.357: E/AndroidRuntime(569):  at dalvik.system.NativeStart.main(Native Method)
01-24 23:33:16.357: E/AndroidRuntime(569): Caused by: android.os.NetworkOnMainThreadException
01-24 23:33:16.357: E/AndroidRuntime(569):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
01-24 23:33:16.357: E/AndroidRuntime(569):  at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
01-24 23:33:16.357: E/AndroidRuntime(569):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
01-24 23:33:16.357: E/AndroidRuntime(569):  at java.net.InetAddress.getByName(InetAddress.java:295)
01-24 23:33:16.357: E/AndroidRuntime(569):  at com.regeduser00x.proj1.Second.connect(Second.java:99)
01-24 23:33:16.357: E/AndroidRuntime(569):  at com.regeduser00x.proj1.Second.onCreate(Second.java:38)
01-24 23:33:16.357: E/AndroidRuntime(569):  at android.app.Activity.performCreate(Activity.java:4465)
01-24 23:33:16.357: E/AndroidRuntime(569):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-24 23:33:16.357: E/AndroidRuntime(569):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
01-24 23:33:16.357: E/AndroidRuntime(569):  ... 11 more

Second.java:99恰好是,第serverAddress = InetAddress.getByName("my server address is here");38 行恰好是 有connect();什么问题吗?我已经在一个小型测试程序中使用 InetAddress 测试了该行,它运行良好,但这里发生了一些事情。

4

3 回答 3

10

这一行:

Caused by: android.os.NetworkOnMainThreadException

告诉你发生了什么。

您正在尝试访问 Main(UI) 线程上的网络功能。从 Honeycomb 开始,系统会在您执行此操作时引发异常。

要解决此问题,您只需将任何与网络接触的事物移至其自己的线程即可。

于 2012-01-24T22:47:12.807 回答
1

这样做是为了确保您不会阻止 UI 线程处理来自用户的任何输入事件。通过阻止 UI 线程,您的应用程序将无法执行任何事件处理例程。

通常大多数 UI 系统都有一个看门狗计时器,它会持续监视 UI 线程上的任何长时间操作,如果 UI 线程被阻塞超过阈值(在 Android 设备中可能为 10-20 秒,因制造商/操作系统版本而异),则看门狗中断并导致“应用程序无响应”(又名 ANR)弹出。

于 2012-01-24T23:13:17.763 回答
1

如果您只想轻松解决此问题,请指定 Froyo 或 Gingerbread 的最低 SDK 版本并省略 targetSdkVersion:

android:minSdkVersion="8"
于 2013-01-06T13:04:41.110 回答