这段代码有什么问题?
抱歉,我问了这样一个问题,但我试图让这个运行几个小时,但我没有发现错误...
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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?