android.os.NetworkOnMainThreadException
来源:互联网 发布:种族主义 知乎 编辑:程序博客网 时间:2024/06/06 02:53
android.os.NetworkOnMainThreadException
转自:http://geeksun.iteye.com/blog/1447708
在android 2.3上设计的下载程序,在android 4.0上运行时报android.os.NetworkOnMainThreadException异常,原来在4.0中,访问网络不能在主程序中进行,有两个方法可以解决,一个是在主程序中增加:
- // 详见StrictMode文档
- 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());
// 详见StrictMode文档 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());
另一种是启动线程执行下载任务:
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();
- }
- };
super.onCreate(savedInstanceState); setContentView(R.layout.main); // 启动线程执行下载任务 new Thread(downloadRun).start(); } /** * 下载线程 */ Runnable downloadRun = new Runnable(){@Overridepublic void run() {// TODO Auto-generated method stubupdateListView();} };
- android.os.NetworkOnMainThreadException问题
- android.os.NetworkOnMainThreadException问题
- android.os.NetworkOnMainThreadException
- android.os.NetworkOnMainThreadException
- android.os.NetworkOnMainThreadException
- android.os.NetworkOnMainThreadException异常
- 关于android.os.NetworkOnMainThreadException
- android.os.NetworkOnMainThreadException
- android.os.NetworkOnMainThreadException
- android.os.NetworkOnMainThreadException
- 异常:android.os.NetworkOnMainThreadException
- android.os.NetworkOnMainThreadException
- android.os.NetworkOnMainThreadException异常
- android.os.NetworkOnMainThreadException
- exception: android.os.NetworkOnMainThreadException
- 异常:android.os.NetworkOnMainThreadException
- android.os.NetworkOnMainThreadException
- 异常android.os.NetworkOnMainThreadException
- OCP-1Z0-052-V8.02-4题
- OpenCV的GrabCut函数使用和源码解读
- 怎样从一个标准url里取出文件的扩展名?
- my list of cool machine learning books
- 你的阅读造就了你 You are what you read
- android.os.NetworkOnMainThreadException
- checkstyle之如何配置
- ContentProvider数据共享(附源码)
- Android系统移植
- c# datagridview 添加行
- 数组与链表的优缺点
- Java Thread join() 的用法
- magento -- 推荐插件 -- 智能版Js和Css合并 --Lesti::Merge(Smart Merging of Js and Css)
- 给定一个数字m和数字n,输出相应的所有排列