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 をブロックし、アプリが応答しなくなる可能性があるためです。

AsyncTaskにデータをロードすることで、これを回避するために を使用できますdoInBackground(..)

于 2013-02-06T23:57:02.033 に答える
0

ネットワーク アクティビティをメイン スレッドから切り離す必要があります。

AsyncTaskを見てください

于 2013-02-06T23:32:52.000 に答える