2010-10-25 124 views
0

對不起,我要問這樣一個問題,但我試着做了幾個小時這一個跑,我沒有找到的錯誤...這段代碼有什麼問題?

public class Main extends ListActivity { 
/** Called when the activity is first created. */ 

ProgressDialog dialog; 

@Override 
public synchronized void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);  
    new WebLoader().doInBackground("http://sample.sample.com/sample.xml"); 
} 

public class WebLoader extends AsyncTask<String, Void, String> { 

    @Override 
    protected String doInBackground(String... params) { 
     String result = ""; 

     try{ 
      URL url = new URL(params[0]); 
      URLConnection conn = url.openConnection(); 
      InputStream is = conn.getInputStream(); 
      BufferedInputStream bis = new BufferedInputStream(is); 
      ByteArrayBuffer baf = new ByteArrayBuffer(2048); 

      int current = 0; 
      while((current = bis.read()) != -1) 
      { 
       baf.append((byte)current); 
      } 

      result = new String(baf.toByteArray()); 
     } 

     catch(Exception e) 
     { 
      Log.e("gullinews", e.getMessage()); 
     } 


     return result; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     dialog.dismiss(); 
    } 

    @Override 
    protected void onPreExecute() { 
     dialog = ProgressDialog.show(getApplicationContext(), "", 
       "Loading. Please wait...", true); 
    }  
    } 

}

與運行調試器顯示,xml數據已下載,但只有黑屏。當我嘗試「setContenView(R.layout.main);」與main.xml中:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <ListView android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:id="@android:id/list" /> 
</LinearLayout> 

//編輯: 好吧,我解決了一個錯誤,沒有解決不了的休息。來源已更新。

我現在的主要問題是,我沒有得到爲什麼ProgressDialog不顯示的想法。休息應該是黑色的,沒錯。

+1

你期待什麼?你的佈局是空的。 – EboMike 2010-10-25 06:14:15

+0

什麼錯誤?請剪切和粘貼堆棧痕跡等 – 2010-10-25 06:14:56

+0

好的,錯誤是那些: – EnflamedSoul 2010-10-25 06:48:42

回答

0

new WebLoader().doInBackground("http://sample.sample.com/sample.xml");

這不是你如何使用的AsyncTask。你讀過任何文件嗎?

+0

Mh。抱歉。舊的源代碼,犯了這個錯誤,但現在我不再這樣做了。我知道它是.execute(「」); – EnflamedSoul 2010-11-03 15:44:44