套接字创建导致力量关闭

时间:2013-05-20 23:29:48

标签: java android

每当我在android中打开一个套接字时,程序立即强行关闭。这是代码:

package com.bensherman.acc;

import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



            try {
                Socket socket = new Socket("192.168.43.253", 4452);
            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

这是我的清单:

  <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.bensherman.acc"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="14" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.bensherman.acc.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

这是日志:

    05-20 19:26:59.539: E/Trace(26558): error opening trace file: No such file or directory (2)
05-20 19:26:59.649: D/AndroidRuntime(26558): Shutting down VM
05-20 19:26:59.649: W/dalvikvm(26558): threadid=1: thread exiting with uncaught exception (group=0x2b545300)
05-20 19:26:59.669: E/AndroidRuntime(26558): FATAL EXCEPTION: main
05-20 19:26:59.669: E/AndroidRuntime(26558): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bensherman.acc/com.bensherman.acc.MainActivity}: android.os.NetworkOnMainThreadException
05-20 19:26:59.669: E/AndroidRuntime(26558):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2190)
05-20 19:26:59.669: E/AndroidRuntime(26558):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2215)
05-20 19:26:59.669: E/AndroidRuntime(26558):    at android.app.ActivityThread.access$600(ActivityThread.java:145)
05-20 19:26:59.669: E/AndroidRuntime(26558):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1211)
05-20 19:26:59.669: E/AndroidRuntime(26558):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-20 19:26:59.669: E/AndroidRuntime(26558):    at android.os.Looper.loop(Looper.java:137)
05-20 19:26:59.669: E/AndroidRuntime(26558):    at android.app.ActivityThread.main(ActivityThread.java:4978)
05-20 19:26:59.669: E/AndroidRuntime(26558):    at java.lang.reflect.Method.invokeNative(Native Method)
05-20 19:26:59.669: E/AndroidRuntime(26558):    at java.lang.reflect.Method.invoke(Method.java:511)
05-20 19:26:59.669: E/AndroidRuntime(26558):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
05-20 19:26:59.669: E/AndroidRuntime(26558):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
05-20 19:26:59.669: E/AndroidRuntime(26558):    at dalvik.system.NativeStart.main(Native Method)
05-20 19:26:59.669: E/AndroidRuntime(26558): Caused by: android.os.NetworkOnMainThreadException
05-20 19:26:59.669: E/AndroidRuntime(26558):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
05-20 19:26:59.669: E/AndroidRuntime(26558):    at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
05-20 19:26:59.669: E/AndroidRuntime(26558):    at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
05-20 19:26:59.669: E/AndroidRuntime(26558):    at libcore.io.IoBridge.connect(IoBridge.java:112)
05-20 19:26:59.669: E/AndroidRuntime(26558):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
05-20 19:26:59.669: E/AndroidRuntime(26558):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
05-20 19:26:59.669: E/AndroidRuntime(26558):    at java.net.Socket.startupSocket(Socket.java:566)
05-20 19:26:59.669: E/AndroidRuntime(26558):    at java.net.Socket.tryAllAddresses(Socket.java:127)
05-20 19:26:59.669: E/AndroidRuntime(26558):    at java.net.Socket.<init>(Socket.java:177)
05-20 19:26:59.669: E/AndroidRuntime(26558):    at java.net.Socket.<init>(Socket.java:149)
05-20 19:26:59.669: E/AndroidRuntime(26558):    at com.bensherman.acc.MainActivity.onCreate(MainActivity.java:22)
05-20 19:26:59.669: E/AndroidRuntime(26558):    at android.app.Activity.performCreate(Activity.java:5008)
05-20 19:26:59.669: E/AndroidRuntime(26558):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
05-20 19:26:59.669: E/AndroidRuntime(26558):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
05-20 19:26:59.669: E/AndroidRuntime(26558):    ... 11 more

有谁知道这是什么问题?如果我说Socket socket = new Socket(),这不是问题。虽然笔记本电脑和开发设备都通过运行wifi系绳的手机连接,但我的计算机上运行的是服务器。

1 个答案:

答案 0 :(得分:1)

尝试更多关于Android上的网络搜索,首先不允许在最新版本的Android上使用UIThread上的网络。第二,你是否正确设置了权限?现在做一些研究比做一些糟糕的解决方案更好。

希望这有助于并享受您的工作。