它适用于 wifi 或 3g,但在断开连接时强制关闭。我正在从教程中学习,所以不确定,但我相信代码中考虑了互联网断开连接。
完成 logcat http://pastebin.com/aAEGwfFk
代码:
package org.tatvamoksh.tml;
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.app.ProgressDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class Updates extends ListActivity {
public ArrayList<HashMap<String, String>> mylist;
public ListAdapter adapter;
public ProgressDialog progressDialog;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.updates_item);
mylist = new ArrayList<HashMap<String, String>>();
adapter = new SimpleAdapter(this, mylist , R.layout.updates,
new String[] { "event", "notice" },
new int[] { R.id.event_title, R.id.notice });
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);
Toast.makeText(Updates.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_LONG).show();
}
});
*/
progressDialog = ProgressDialog.show(Updates.this, "Getting updates", "Please wait...");
new GetDataTask().execute();
}
private Boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if(ni != null && ni.isConnected())
return true;
return false;
}
private class GetDataTask extends AsyncTask<Void, Void, Integer> {
@Override
protected Integer doInBackground(Void... params) {
// Simulates some delay
try {
Thread.sleep(2000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
if(isOnline()){
mylist.clear();
String xml = XMLfunctions.getXML();
Document doc = XMLfunctions.XMLfromString(xml);
int numResults = XMLfunctions.numResults(doc);
if((numResults <= 0)){
Toast.makeText(Updates.this, "Geen resultaten gevonden", Toast.LENGTH_LONG).show();
finish();
}
NodeList nodes = doc.getElementsByTagName("update");
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("event", "Event:" + XMLfunctions.getValue(e, "event"));
map.put("notice", "Update: " + XMLfunctions.getValue(e, "notice"));
mylist.add(map);
}
}
else{
Toast.makeText(Updates.this, "No connection..", Toast.LENGTH_LONG).show();
}
return 1;
}
@Override
protected void onPostExecute(Integer result) {
setListAdapter(adapter);
progressDialog.dismiss();
super.onPostExecute(result);
}
}
}
我确实尝试搜索类似的问题,大多数导致 NullPointerException,调试器没有捕获任何(我认为) 调试器透视图