0

該應用程序在Android 2.2中工作得很好。但在4.2應用程序意外關閉。 該應用使用客戶端服務器體系結構將消息發送到充當服務器的筆記本電腦。 的logcat的顯示:應用程序在Android 2.2中正常工作,但意外關閉在4.2

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

'NetworkOnMainThreadException',堆棧跟蹤說明了一切。 [更多信息](http://www.androiddesignpatterns.com/2012/06/app-force-close-honeycomb-ics.html)。 –

+0

它在老版本的Android上工作正常,因爲舊版本的框架代碼永遠不會檢查這種情況。 –

+0

顯然。我知道例外情況並不總是可以理解的,但是這個說明了一切。把你的服務器的網絡通信放在一個單獨的線程中(例如AsyncTask ..) – psykhi

回答

0

您需要將您的網絡活動關閉主線程。

看看AsyncTask

+0

你能否詳細說明一下?我在問題中添加了我的客戶端代碼.. – user2048724

+3

你就像第100萬個人一樣問這個問題。嘗試谷歌的AsyncTask,看看如何使用它。請閱讀由A-C發佈的鏈接。 –

+0

@ user2048724 Android不再支持主(GUI)線程上的網絡操作。這意味着所有網絡操作都需要移動到另一個線程。 AsyncTask是Android開發團隊給我們提供的一個很好的工具。做一些關於什麼線程和多線程的研究。 – MrZander

1

你在你的主UI線程中執行網絡操作。如果您的目標SDK爲11(Honeycomb)或更高,則會拋出NetworkOnMainThreadException,因爲此行爲可能會阻止UI並導致應用程序無響應。

您可以使用AsyncTask來解決此問題,方法是將數據加載到doInBackground(..)中。

相關問題