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 につながり、デバッガーはデバッガーの視点のスクリーンショットを(私が思うに) キャッチしませんでした