java错误:安卓。操作系统。NetworkOnMainThreadException
我正在制作一个简单的安卓应用程序。现在我做了一个活动,它从web加载信息,并使用这些信息设置listview。如果运行应用程序,它工作正常,但如果我打开活动(我在上面描述过),他会给出错误:安卓。操作系统。NetworkOnMainThreadException
活动文件:
package com.a3gaatleren;
import 安卓.support.v7.app.ActionBarActivity;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Arrays;
import 安卓.os.AsyncTask;
import 安卓.os.Bundle;
import 安卓.view.Menu;
import 安卓.view.MenuItem;
import 安卓.widget.ArrayAdapter;
import 安卓.widget.ListView;
import com.a3gaatleren.ReadFile;
public class Agenda extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_agenda);
doInBackground();
}
protected void doInBackground(){
try {
ListView mainListView ;
// Find the ListView resource.
mainListView = (ListView) findViewById( R.id.listView2);
String file_name = "http://3gaatleren.16mb.com/appagenda/agenda.html";
// Create and populate a List of planet names.
ReadFile file = new ReadFile(file_name);
String[] huiswerk = file.OpenFile();
ArrayList<String> huiswerklist = new ArrayList<String>();
huiswerklist.addAll( Arrays.asList(huiswerk) );
// Create ArrayAdapter using the planet list.
ArrayAdapter<String> listAdapter;
listAdapter = new ArrayAdapter<String>(Agenda.this, R.layout.simplerow, huiswerklist);
// Set the ArrayAdapter as the ListView's adapter.
mainListView.setAdapter( listAdapter );
}catch(MalformedURLException e){
System.out.println(e);
}catch(IOException f){
System.out.println(f);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.agenda, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
定义readfile函数的java文件:
package com.a3gaatleren;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Scanner;
import org.jsoup.Jsoup;
import java.io.FileReader;
import java.io.BufferedReader;
public class ReadFile {
private String path;
public ReadFile (String file_path){
path= file_path;
}
public String[] OpenFile() throws IOException{
URL url = new URL(path);
BufferedReader textReader = new BufferedReader(new InputStreamReader(url.openStream()));
int numberOfLines=readLines();
String[] textData = new String[numberOfLines];
int i;
for (i=0; i< numberOfLines; i++){
String html = textReader.readLine();
org.jsoup.nodes.Document doc;
doc = Jsoup.parse(html, "utf-8");
String text = doc.body().text();
textData[i] =text;
}
textReader.close();
return textData;
}
int readLines() throws IOException{
URL file_to_read = new URL(path);
BufferedReader bf = new BufferedReader(new InputStreamReader(file_to_read.openStream()));
int numberOfLines = 0;
String str;
while ((str = bf.readLine()) != null){
numberOfLines++;
}
bf.close();
return numberOfLines;
}
}
谁能告诉我有什么问题吗
附:英语不是我的第一语言,我不知道它是不是正确的英语
# 1 楼答案
基本上,UI线程是应用程序的一部分,处理屏幕上显示的内容,让用户使用它。如果用户尝试某个东西时没有响应,那么充其量用户会认为应用程序运行缓慢,最坏的情况是用户会认为应用程序崩溃,Android不好,这是我们不希望看到的
为了解决这个问题,我们可以在另一个线程上做任何需要很长时间或大量处理能力的事情。最简单的方法是使用异步任务
以上内容绝对是赤裸裸的,但应该能帮助你解决问题