2012-01-31 68 views
-1

親愛的Stackoverflow程序員,Android AsyncTask線程

我已經做了一個應用程序幾個月前,它工作flawelessly。但是,在新的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(); */ 

      } 
     }); 


    } 

}

+0

得到一個堆棧跟蹤,或有關錯誤的更多信息? – Theblacknight 2012-01-31 14:53:52

回答

0

我的猜測是,在你的XMLfunctions.getXML();方法,你執行的是網絡調用來檢索你的XML。這是從UI線程上執行的onCreate()中調用的。 ICS對於不允許您在UI線程上執行網絡請求非常嚴格,因此您需要將其移至AsyncTaskLoader

+0

確實,我的'XMLfunctions.getXML();'正在執行網絡調用。問題是,我不能讓它與AsyncTask一起工作,因爲我的UI崩潰並且得到了很多NullPointerExceptions,我認爲是因爲我的xml被調用後我的UI被調用。我知道這一定是可能的,但我做錯了錯誤,我想..任何建議? – 2012-01-31 17:04:47

0

您將需要編寫您的用戶界面以應付沒有任何有效數據的情況。然後,在AsyncTask完成檢​​索數據之後,它可以請求刷新UI以顯示有效數據。

我建議你的用戶界面最初顯示一個不確定的進度條(帶有旋轉的破折號),也許還有文本「加載...」讓用戶知道他們必須等待;這些可以在用戶界面顯示有效數據後隱藏。