Android:应用异步进度对话框后,文本未在TextView中显示

时间:2012-05-10 10:42:16

标签: android progressdialog

我正在开发一个项目,我正在从互联网上解析XML文件并在TextView中显示其内容。

程序:当我点击按钮时,我将自己转发到Prediction类,我从互联网上获取xml,但现在我将Async Progress对话框添加到其中。 编码中没有错误,但是当我在模拟器中运行应用程序时,它显示了“ERROR”的catch异常消息,它没有显示在添加ASync进度对话框之前显示的“Information”。

public class Prediction extends Activity{

static final String baseUrl = "http://astroyogi.in/testandroid/Tarot/Tarot_Android.xml";
TextView tv1;
ImageView iv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.prediction);
    grabURL("http://astroyogi.in/testandroid/Tarot/Tarot_Android.xml");
    tv1 = (TextView) findViewById(R.id.tv1);
    }

public void grabURL(String url) {
    new GrabURL().execute(url);
}

private class GrabURL extends AsyncTask<String, Void, Void> {
    private ProgressDialog Dialog = new ProgressDialog(Prediction.this);
    static final String baseUrl = "http://astroyogi.in/testandroid/Tarot/Tarot_Android.xml";


    protected void onPreExecute() {
        Dialog.setMessage("Downloading source..");
        Dialog.show();
    }

    @Override
    protected Void doInBackground(String... params) {
        // TODO Auto-generated method stub

        try{
            URL website = new URL(baseUrl);
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLReader xr = sp.getXMLReader();
            Handlingpastxml doingWork = new Handlingpastxml();
            xr.setContentHandler(doingWork);  
            xr.parse(new InputSource(website.openStream()));
            String information = doingWork.getInformation();
            tv1.setText(information);
            }

        catch(Exception e){
            tv1.setText(e.getMessage());

        }
        return null;
    }


    protected void onPostExecute(Void unused) {
        Dialog.dismiss();
    }

}

}

6 个答案:

答案 0 :(得分:1)

doinBackGround is non UI thread so never update your UI in this method

如果您想更新onProgressUpdate or onPostExecute method

在您的情况下,使用onPostExecute方法

String information=null;

@Override
protected void onCreate(Bundle savedInstanceState) {
     //your code block
 }

 in doinBackGround method in try block    

 information = doingWork.getInformation();<---Remove `String`

protected void onPostExecute(Void unused) {
        Dialog.dismiss();

        if(information!=null&&information.trim().length!=0)
        tv1.setText(information);
    }

答案 1 :(得分:1)

FYI, 你不能(直接)在doInBackground()方法中显示/更新UI类型的操作。

解-1:

从doInBackground()中删除以下行并将其粘贴到onPostExecute()

 tv1.setText(information);

解-2:

如果要在 中的doInBackground()内显示/更新UI,请在 runOnUiThread() 中编写此类操作:

 runOnUiThread(new Runnable() {

            public void run() {
                // TODO Auto-generated method stub
                tv1.setText(information);
            }
        });

答案 2 :(得分:0)

这是线程问题:

当您使用“doInBackground”方法时,您不再位于UI线程中。所以你不能说“tv1.setText(信息);”因为它不会更新你的用户界面。

“doInBackground”适用于繁重的任务。当这些任务结束时,将调用“onPostExecute”。当你在“onPostExecute”时,你在UI线程中,你可以根据需要修改你的用户界面。

答案 3 :(得分:0)

而不是new GrabURL().execute(url);只写new GrabURL().execute();

因为您已经在GrabURL中创建了对象。

另外一点是,当你想在线程中更新UI时,我们使用Handler,所以这里尝试保留onPostExecute。

答案 4 :(得分:0)

而不是代码

@Override
    protected Void doInBackground(String... params) {
        // TODO Auto-generated method stub

        try{
            URL website = new URL(baseUrl);
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLReader xr = sp.getXMLReader();
            Handlingpastxml doingWork = new Handlingpastxml();
            xr.setContentHandler(doingWork);  
            xr.parse(new InputSource(website.openStream()));
            String information = doingWork.getInformation();
            tv1.setText(information);
            }

        catch(Exception e){
            tv1.setText(e.getMessage());

        }
        return null;
    }


    protected void onPostExecute(Void unused) {
        Dialog.dismiss();
    }

尝试按照以下方式执行

@Override
    protected Void doInBackground(String... params) {
        // TODO Auto-generated method stub

        try{
            URL website = new URL(baseUrl);
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLReader xr = sp.getXMLReader();
            Handlingpastxml doingWork = new Handlingpastxml();
            xr.setContentHandler(doingWork);  
            xr.parse(new InputSource(website.openStream()));
            String information = doingWork.getInformation();

            }

        catch(Exception e){
            String information = e.getMessage();

        }
        return information;
    }


    protected void onPostExecute(Void response) {
         tv1.setText(response);
        Dialog.dismiss();
    }

答案 5 :(得分:0)

您不应该从非UI线程更新您的视图, 在方法doInbackground()中你不能使用tv1.setText(信息);

删除

tv1.setText(information); 
来自doInbackround()的

!!并把它放在

protected void onPostExecute(Void unused) {
    Dialog.dismiss();
    tv1.setText(information);

}

信息也应该是一个全局变量