在Android应用程序奇怪NetworkOnMainThreadException? [英] Strange NetworkOnMainThreadException in Android app?

查看:267
本文介绍了在Android应用程序奇怪NetworkOnMainThreadException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的想法是开始聊天。所以,我在我的课有这样的特性:

The idea is to start a chat. So I have this properties in my class:

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

然后,我有这个code。在的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

下面是我的连接()方法:

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);
}

一切看起来我的权利,但是这是这样说的:

Everything looks right to me but this is what it says:

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(我的服务器地址就在这里); 和线38恰好是连接(); 什么是与它有关系吗?我已经测试与InetAddress类线在一个小的测试程序,它完美的作品,但这里有事情发生。

That Second.java:99 happens to be serverAddress = InetAddress.getByName("my server address is here"); and line 38 happens to be connect(); What's the matter with it? I have tested the line with the InetAddress in a small test program and it works perfectly but here something happens.

推荐答案

这行:

Caused by: android.os.NetworkOnMainThreadException

告诉你到底是怎么回事。

Tells you what is going on.

您试图访问网络功能的主(UI)线程上。首先是蜂窝系统提出了一个例外,当你做到这一点。

You are attempting to access a network function on the Main(UI) thread. starting with Honeycomb the system raises an Exception when you do this.

要解决,你只需要移动的是与网络接触到它自己的线程的任何事情。

To fix you just need to move any thing that is touching the network to its own thread.

这篇关于在Android应用程序奇怪NetworkOnMainThreadException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆