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 没有出现。休息应该是黑色的,没错。

4

1 回答 1

0

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

这不是你使用异步任务的方式。您是否阅读过任何文档?

于 2010-10-29T06:12:56.267 回答