简体   繁体   English

此代码有什么问题?

[英]What is wrong with this code?

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. 使用调试器运行将显示xml数据已下载,但是只有黑屏。 When I tried "setContenView(R.layout.main);" 当我尝试“ setContenView(R.layout.main);”时 with main.xml: 与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. 我现在的主要问题是,我不知道为何无法显示ProgressDialog。 rest should be black, that's right. 休息应该是黑色的,是的。

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

That's not how you use an asynctask. 那不是您使用asynctask的方式。 Did you read any documentaton at all? 您是否阅读过任何文献资料?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM