在android 2.3上设计的下载程序,在android 4.0上运行时报android.os.NetworkOnMainThreadException异常,原来在4.0中,访问网络不能在主程序中进行,有两个方法可以解决,一个是在主程序中增加
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads()
.detectDiskWrites()
.detectNetwork()
.penaltyLog()
.build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects()
.detectLeakedClosableObjects()
.penaltyLog()
.penaltyDeath()
.build());
}
另一种是启动线程执行下载任务:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 启动线程执行下载任务
new Thread(downloadRun).start();
}
/**
* 下载线程
*/
Runnable downloadRun = new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
updateListView();
}
};
转自:
http://geeksun.iteye.com/blog/1447708
分享到:
相关推荐
好久不写Android代码手都生了,找出自己之前写的程序发现跑不了了,也没啥特别的错误提示,就看到一句有用的错误Caused by: android.os.NetworkOnMainThreadException,查了下原因上在4.0之后在主线程里面执行Http...
在Android中,如果想退出Android程序,一般都是调用finish()、System.exit(0)android.os.Process.killProcess(android.os.Process.myPid())等方法来实现退出程序功能,可是在实际开发中,并不能达到完全退出应用程序...
最近在Android N 上 安装Apk时报错:android.os.FileUriExposedException: file:///storage/emulated/0/Download/appName-2.3.0.apk exposed beyond app through Intent.getData(),通过查找相关的资料终于找到了...
Android新编译规则Android.bp文件语法规则详细介绍,条件编译的配置案例。 Android.bp 文件首先是 Android 系统的一种编译配置文件,是用来代替原来的 Android.mk 文件的。在 Android7.0 以前,Android 都是使用 ...
This is a development tool to show values of android.os.Build.* Try it Installing android SDK See the official android doc Create emulator The command line for creating an AVD has the following syntax...
cannot find symbol import android.os.SystemProperties 前言 最近在搞Android高版本的9和10开发,正在敲着代码唱着歌,发现在Android源码目录的app下面导入import android.os.SystemProperties这个包老是不行...
Android webview 遇到android.os.FileUriExposedException错误解决办法 1. 在 Manifest 文件中添加: <provider android:name=android.support.v4.content.FileProvider android:authorities=${...
android.util.Base64类
Apress.Pro.Android.Web.Apps.Developing.HTML5.JavaScript.CSS.and.Chrome.OS.Web.Apps
当出现提示说: Call requires API level 9 (current min is 8): android.os.StrictMode#setThrea 也就是说版本低
使用android.hardware.camera2写的新的自定义相机
完整的android.support.annotation.jar包含Nullable、Keep、XmlRes、TransitionRes、WorkerThread等共49个类
android.os.build.serial 在API 9 中引入,如果低于9的系统可以通过这个方法获取这个信息值 java代码如下: public static String getDeviceSerial() { String serial = unknown; try { Class clazz = Class.for...
import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android....
问题: 在百度地图页面(Activity为singleTask)跳转到别的页面 ,离开地图页面。偶发性空指针,具体日志信息如下: 解决: 1、原本我在生命周期onStop里调用的mlocationClient.stop();...作者:Android_Mr_Zhao
或许困扰很多Android开发者最大的问题莫过于Bug的修改了,调Bug 改Bug,想想都头疼,以下就是一位对Bug很有经验的开发者总结出来的有关Android错误的解决方法。