这段代码有什么问题?

发布于 2024-09-28 14:02:17 字数 2101 浏览 3 评论 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 不显示。其余的应该是黑色的,没错。

Sorry that I'm asking such a question, but I'm tryin to make this one run for hours, and I'm not finding the mistake...

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);
    }     
  }

}

Running with a debugger shows, that the xml data is downloaded, but there's just black screen. When I tried "setContenView(R.layout.main);" with 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>

//Edit:
okay, I solved one error, didn't solve the rest. Source updated.

My Main problem now is, that I ain't got an Idea why the ProgressDialog doesnt show up. rest should be black, that's right.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抽个烟儿 2024-10-05 14:02:17

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

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

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

That's not how you use an asynctask. Did you read any documentaton at all?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文