2012年8月23日

【Android】Android 3.0 使用WebService 發生android.os.NetworkOnMainThreadException異常

WebService (包含HTTP Request等)在2.x以前可以正常運作,但在Android 3.0以後會出現
下列這問題: android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
原因在於從API 9開始,android 對 Thread Policy有所改變,導致對Network存取會發生不允許的現像。

Solution:
 在onCreate()加入下段程式


    if (android.os.Build.VERSION.SDK_INT > 9) {
      StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
      StrictMode.setThreadPolicy(policy);
    }
Reference:


2012年8月2日

【Android】Create a new Activity



You can use the "New Class" dialog, but that leaves other steps you need to do by hand (e.g. adding an entry to the manifest file). If you want those steps to be automated, you can create the activity via the manifest editor like this:
  1. Double click on AndroidManifest.xml  in the package explorer.