0

每当我在 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 系绳的电话连接的。

4

1 回答 1

1

好吧,试着在 Android 上搜索更多关于网络的信息,首先你不能在最新版本的 android 上使用 UIThread 上的网络。其次,您是否正确设置了权限?你现在最好做更多的研究而不是使用一些糟糕的解决方案。

希望这对您有所帮助并享受您的工作。

于 2013-05-20T23:34:32.947 回答