Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:应用异步进度对话框后,文本未显示在文本视图中_Android_Progressdialog - Fatal编程技术网

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

Android:应用异步进度对话框后,文本未显示在文本视图中,android,progressdialog,Android,Progressdialog,我正在从事一个项目,在该项目中,我正在解析来自internet的XML文件,并在TextView中显示其内容 过程:当我单击按钮时,我将自己转发到从internet获取xml的预测类,但现在我在其中添加了异步进度对话框。。 编码中没有错误,但当我在emulator中运行应用程序时,它会显示catch异常消息“error”,而不会显示在添加异步进度对话框之前显示的“Information” public class Prediction extends Activity{ static fina

我正在从事一个项目,在该项目中,我正在解析来自internet的XML文件,并在TextView中显示其内容

过程:当我单击按钮时,我将自己转发到从internet获取xml的预测类,但现在我在其中添加了异步进度对话框。。 编码中没有错误,但当我在emulator中运行应用程序时,它会显示catch异常消息“error”,而不会显示在添加异步进度对话框之前显示的“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();
    }

}

}
公共类预测扩展活动{
静态最终字符串baseUrl=”http://astroyogi.in/testandroid/Tarot/Tarot_Android.xml";
文本视图tv1;
ImageView iv;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE\u进度);
this.requestWindowFeature(Window.FEATURE\u NO\u TITLE);
setContentView(R.layout.prediction);
抓取URL(“http://astroyogi.in/testandroid/Tarot/Tarot_Android.xml");
tv1=(TextView)findViewById(R.id.tv1);
}
公共url(字符串url){
新建GrabURL().execute(url);
}
私有类GrabURL扩展异步任务{
private ProgressDialog=新建ProgressDialog(Prediction.this);
静态最终字符串baseUrl=”http://astroyogi.in/testandroid/Tarot/Tarot_Android.xml";
受保护的void onPreExecute(){
setMessage(“下载源…”);
Dialog.show();
}
@凌驾
受保护的Void doInBackground(字符串…参数){
//TODO自动生成的方法存根
试一试{
URL网站=新URL(基本URL);
SAXParserFactory spf=SAXParserFactory.newInstance();
SAXParser sp=spf.newSAXParser();
XMLReader xr=sp.getXMLReader();
Handlingpastxml doingWork=新Handlingpastxml();
xr.setContentHandler(doingWork);
xr.parse(新输入源(website.openStream());
字符串信息=doingWork.getInformation();
tv1.setText(信息);
}
捕获(例外e){
tv1.setText(如getMessage());
}
返回null;
}
受保护的void onPostExecute(未使用的void){
Dialog.dismise();
}
}
}

这是一个线程问题:

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


“doInBackground”是为你的繁重任务准备的。当这些任务结束时,将调用“onPostExecute”。当您处于“onPostExecute”中时,您处于UI线程中,您可以根据需要修改UI。

而不是
new GrabURL()。执行(url)只需编写
newgrabURL().execute()

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


另一件事是,当您想在线程中更新UI时,我们使用处理程序,所以这里尝试保留onPostExecute。

doinBackGround是非UI线程,所以永远不要用此方法更新UI

如果要更新
onProgressUpdate或onPostExecute方法

在您的情况下,使用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);
    }
字符串信息=null;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//你的代码块
}
try块中的in-doinBackGround方法

information=doingWork.getInformation() 而不是下面的代码

@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();
    }
仅供参考, 在doInBackground()方法中不能(直接)执行显示/更新UI类操作

解决方案1: 从doInBackground()中删除下面的行,并将其粘贴到
onPostExecute()

解决方案2: 如果要在中的doInBackground()中显示/更新UI,请在
runOnUiThread()中编写此类操作。

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

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

删除

tv1.setText(information); 
从doInbackround()开始!!把它放进去

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

}

信息也应该是全局变量

请帮助我。。我被困在这里面超过4-5个小时了..当你崩溃或强制关闭时发布logcat输出。当我运行上面的代码**无法在没有调用looper.prepare()asynctask的线程内创建处理程序**是的,那么下面已经列出了解决方案。tv1.setText(信息);无法在do in BackgroundThank@Samir Mangrolia.中执行。但当我将代码更新为urs时,它显示了在doingwork中获取局部变量的错误。getInformation。。。那么现在该做什么呢?
runOnUiThread()
不是解决方案有
onProgressUpdate(用于显示进度)
onPostExecute(用于结束工作…
),我认为不需要运行UI…检查上面的解决方案-1,并同意onProgressUpdate().应用此代码后,我将关闭eception和应用程序强制..感谢您的ans@parth,但这不起作用。。在**返回信息**和post execute方法中显示错误。请将受保护的void onPostExecute(void响应)替换为受保护的void onPostExecute(字符串响应),而不是在主类中的doInBackground()方法声明信息中声明字符串信息@公共类预测扩展活动{String information=null;静态最终字符串baseUrl=“”;TextView tv1;ImageView iv;
protected void onPostExecute(Void unused) {
    Dialog.dismiss();
    tv1.setText(information);

}