Android AsyncTask 线程
亲爱的 Stackoverflow 程序员,
我几个月前制作了一个应用程序,它运行得非常完美。然而,在新的 Android ICS 上它开始崩溃。我查了一下,发现主线程网络异常。我尝试重写我的代码,但我无法让它按照它的方式工作。我尝试了 AsyncTasking,但这也不起作用。请问有人可以帮助我吗?
我的代码:(如果您还需要 XMLFunctions.class ,请告诉我)
package test.lmc;
import java.util.ArrayList;
import java.util.HashMap;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class nieuwsflits extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nieuwsflits);
String xml = XMLfunctions.getXML();
Document doc = XMLfunctions.XMLfromString(xml);
int numResults = XMLfunctions.numResults(doc);
if((numResults <= 0)){
Toast.makeText(nieuwsflits.this, "Nog geen resultaten gevonden...", Toast.LENGTH_LONG).show();
finish();
}
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
NodeList nodes = doc.getElementsByTagName("message");
int max = 10;
//fill in the list items from the XML document
for (int i = 0; i < nodes.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element)nodes.item(i);
map.put("id", XMLfunctions.getValue(e, "id"));
map.put("datum", XMLfunctions.getValue(e, "datum"));
map.put("message-text", XMLfunctions.getValue(e, "message-text"));
map.put("url", XMLfunctions.getValue(e, "url"));
mylist.add(map);
}
//Make a new listadapter
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.listadapter,
new String[] { "message-text", "datum" },
new int[] { R.id.item_title, R.id.item_subtitle });
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
@SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);
String url = o.get("url");
String check ="";
if ((url == check)) {
Toast.makeText(nieuwsflits.this, "Geen website gevonden...", Toast.LENGTH_LONG).show();
}
else {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
/** Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(o.get("url")));
startActivity(browserIntent);
Toast.makeText(nieuwsflits.this, "ID '" + o.get("url") + "' was clicked.", Toast.LENGTH_LONG).show(); */
}
});
}
}
Dear Stackoverflow programmers,
I've made an app a few months ago and it is working flawelessly. However, on the new Android ICS it started crashing. I looked it up and i get a Network On Main Thread Exception. I tried to rewrite my code but i can't get it to work the way it did.. I tried AsyncTasking but that also didn't work.. Please can someone help me out??
my code: (if you also need XMLFunctions.class , please let me know)
package test.lmc;
import java.util.ArrayList;
import java.util.HashMap;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class nieuwsflits extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nieuwsflits);
String xml = XMLfunctions.getXML();
Document doc = XMLfunctions.XMLfromString(xml);
int numResults = XMLfunctions.numResults(doc);
if((numResults <= 0)){
Toast.makeText(nieuwsflits.this, "Nog geen resultaten gevonden...", Toast.LENGTH_LONG).show();
finish();
}
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
NodeList nodes = doc.getElementsByTagName("message");
int max = 10;
//fill in the list items from the XML document
for (int i = 0; i < nodes.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element)nodes.item(i);
map.put("id", XMLfunctions.getValue(e, "id"));
map.put("datum", XMLfunctions.getValue(e, "datum"));
map.put("message-text", XMLfunctions.getValue(e, "message-text"));
map.put("url", XMLfunctions.getValue(e, "url"));
mylist.add(map);
}
//Make a new listadapter
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.listadapter,
new String[] { "message-text", "datum" },
new int[] { R.id.item_title, R.id.item_subtitle });
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
@SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);
String url = o.get("url");
String check ="";
if ((url == check)) {
Toast.makeText(nieuwsflits.this, "Geen website gevonden...", Toast.LENGTH_LONG).show();
}
else {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
/** Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(o.get("url")));
startActivity(browserIntent);
Toast.makeText(nieuwsflits.this, "ID '" + o.get("url") + "' was clicked.", Toast.LENGTH_LONG).show(); */
}
});
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的猜测是,在您的
XMLfunctions.getXML();
方法中,您正在执行网络调用来检索 XML。这是从 UI 线程上执行的 onCreate() 调用的。 ICS 非常严格,不允许您在 UI 线程上执行网络请求,因此您需要将其移至AsyncTask
或Loader
。My guess would be that in your
XMLfunctions.getXML();
method you are performing a network call to retrieve your XML. This is being called from onCreate() which is executed on the UI thread. ICS is very strict about not letting you perform network requests on the UI thread, so you will need to move this to anAsyncTask
orLoader
.您需要对 UI 进行编码,以应对没有任何有效数据的情况。然后,在 AsyncTask 完成数据检索后,它可以请求刷新 UI 以显示有效数据。
我建议你的用户界面最初显示一个不确定的进度条(带有旋转的破折号),也许还伴随着文本“正在加载...”,让用户知道他们必须等待;一旦 UI 有有效数据可显示,这些就可以隐藏。
You will need to code your UI to cope with not having any valid data to begin with. Then after your AsyncTask has completed retrieving the data, it can request a refresh of the UI to display the valid data.
I’d suggest your UI initially show an indefinite progress bar (with the rotating dashes), perhaps along with the text “Loading...” to let the user know they have to wait; these can be hidden once the UI has valid data to show.