之前是2.2开发的项目,在4.0手机上程序是出现了错误android.os.NetworkOnMainThreadException,
网上找了解决办法,在程序入口加上以下代码。项目直接换了4.0的SDK
public void onCreate(){
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads()
.detectDiskWrites()
.detectNetwork() // or .detectAll() for all detectable problems
.penaltyLog()
.build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects()
.detectLeakedClosableObjects()
.penaltyLog()
.penaltyDeath()
.build());
}
又报以下错误,大侠们看看、是什么原因
10-09 21:50:28.907: E/StrictMode(14062): A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
10-09 21:50:28.907: E/StrictMode(14062): java.lang.Throwable: Explicit termination method 'close' not called
10-09 21:50:28.907: E/StrictMode(14062): at dalvik.system.CloseGuard.open(CloseGuard.java:184)
10-09 21:50:28.907: E/StrictMode(14062): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:300)
------解决方案--------------------
原因是在主线程中访问了网络。
------解决方案--------------------
有些Activity比较老(如:MapActivity)不适合StrictMode,你不启用StrictMode试试。