0

该应用程序在 android 2.2 中运行良好。但在 4.2 中,应用程序意外关闭。该应用程序使用客户端服务器架构将消息发送到充当服务器的笔记本电脑。logcat 显示:

02-07 04:49:45.683: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:45.683: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:45.690: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:46.160: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:46.203: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:47.803: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:47.810: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:48.000: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:48.050: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:48.182: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:48.190: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:48.262: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:48.440: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:48.440: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:48.470: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:48.480: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:48.480: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:48.500: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:48.550: D/gralloc_goldfish(829): Emulator without GPU emulation detected.
02-07 04:49:49.030: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:49.060: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:49.210: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:49.210: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:50.433: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:50.433: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:50.473: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:50.473: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:50.860: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:50.860: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:50.993: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:50.993: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:51.040: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:51.040: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:51.490: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:51.490: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:51.530: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:51.530: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:52.003: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:52.003: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:52.070: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:52.070: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:52.503: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:52.503: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:52.563: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:52.563: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:53.011: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:53.011: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:53.071: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:53.071: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:53.540: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:53.540: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:53.600: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:53.600: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:54.020: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:54.020: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:54.100: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:54.100: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:54.550: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:54.570: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:54.750: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:54.750: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:55.070: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:50:20.590: D/dalvikvm(829): GC_CONCURRENT freed 193K, 11% free 2566K/2876K, paused 92ms+42ms, total 1402ms
02-07 04:50:20.590: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:50:20.610: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:50:20.653: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:50:20.660: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:50:20.750: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:50:20.750: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:50:21.284: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:50:21.290: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:50:21.355: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:50:21.360: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:50:21.560: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:50:21.560: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:50:21.610: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:50:21.768: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:50:22.083: D/AndroidRuntime(829): Shutting down VM
02-07 04:50:22.090: W/dalvikvm(829): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
02-07 04:50:22.190: E/AndroidRuntime(829): FATAL EXCEPTION: main
02-07 04:50:22.190: E/AndroidRuntime(829): android.os.NetworkOnMainThreadException
02-07 04:50:22.190: E/AndroidRuntime(829):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
02-07 04:50:22.190: E/AndroidRuntime(829):  at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
02-07 04:50:22.190: E/AndroidRuntime(829):  at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
02-07 04:50:22.190: E/AndroidRuntime(829):  at libcore.io.IoBridge.connect(IoBridge.java:112)
02-07 04:50:22.190: E/AndroidRuntime(829):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
02-07 04:50:22.190: E/AndroidRuntime(829):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
02-07 04:50:22.190: E/AndroidRuntime(829):  at java.net.Socket.startupSocket(Socket.java:566)
02-07 04:50:22.190: E/AndroidRuntime(829):  at java.net.Socket.tryAllAddresses(Socket.java:127)
02-07 04:50:22.190: E/AndroidRuntime(829):  at java.net.Socket.<init>(Socket.java:177)
02-07 04:50:22.190: E/AndroidRuntime(829):  at java.net.Socket.<init>(Socket.java:149)
02-07 04:50:22.190: E/AndroidRuntime(829):  at com.example.sample2.MainActivity$1.onClick(MainActivity.java:39)
02-07 04:50:22.190: E/AndroidRuntime(829):  at     android.view.View.performClick(View.java:4202)
02-07 04:50:22.190: E/AndroidRuntime(829):  at android.view.View$PerformClick.run(View.java:17340)
02-07 04:50:22.190: E/AndroidRuntime(829):  at android.os.Handler.handleCallback(Handler.java:725)
02-07 04:50:22.190: E/AndroidRuntime(829):  at android.os.Handler.dispatchMessage(Handler.java:92)
02-07 04:50:22.190: E/AndroidRuntime(829):  at android.os.Looper.loop(Looper.java:137)
02-07 04:50:22.190: E/AndroidRuntime(829):  at android.app.ActivityThread.main(ActivityThread.java:5039)
02-07 04:50:22.190: E/AndroidRuntime(829):  at java.lang.reflect.Method.invokeNative(Native Method)
02-07 04:50:22.190: E/AndroidRuntime(829):  at java.lang.reflect.Method.invoke(Method.java:511)
02-07 04:50:22.190: E/AndroidRuntime(829):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-07 04:50:22.190: E/AndroidRuntime(829):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-07 04:50:22.190: E/AndroidRuntime(829):  at dalvik.system.NativeStart.main(Native Method)
02-07 04:50:30.530: I/Process(829): Sending signal. PID: 829 SIG: 9

我的客户代码是

public class MainActivity extends Activity {

 private Socket client;
 private PrintWriter printwriter;
 private EditText textField;
 private Button button;
 private String messsage;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textField = (EditText) findViewById(R.id.editText1);
    button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            messsage = textField.getText().toString(); 
            textField.setText("");    
            try {

                client = new Socket("10.0.2.2", 4444);  
                printwriter = new PrintWriter(client.getOutputStream(),true);
                printwriter.write(messsage);  
                printwriter.flush();
                printwriter.close();
                client.close();  
               } catch (UnknownHostException e) {
                e.printStackTrace();
               } catch (IOException e) {
                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.activity_main, menu);
    return true;
}

}

请帮我解决它。

4

2 回答 2

1

您正在主 UI 线程中执行网络操作。如果您的目标 SDK 是 11 (Honeycomb) 或更高版本,则会抛出NetworkOnMainThreadException,因为此行为可能会阻塞 UI 并导致应用程序无响应。

您可以使用 anAsyncTask来解决这个问题,方法是将数据加载到doInBackground(..).

于 2013-02-06T23:57:02.033 回答
0

您需要将网络活动从主线程中移除。

看看AsyncTask

于 2013-02-06T23:32:52.000 回答