Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.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,我有个问题。我想在我的应用程序从提要下载一些新闻时创建一个进度对话框。 这是我目前的代码: public class NyhedActivity extends Activity { String streamTitle = ""; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.o

我有个问题。我想在我的应用程序从提要下载一些新闻时创建一个进度对话框。 这是我目前的代码:

public class NyhedActivity extends Activity {
    String streamTitle = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.nyheder);

        TextView result = (TextView)findViewById(R.id.result);

           try {
       URL rssUrl = new URL("http://rss.tv2sport.dk/rss/*/*/*/248/*/*");
       SAXParserFactory mySAXParserFactory = SAXParserFactory.newInstance();
       SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
       XMLReader myXMLReader = mySAXParser.getXMLReader();
       RSSHandler myRSSHandler = new RSSHandler();
       myXMLReader.setContentHandler(myRSSHandler);
       InputSource myInputSource = new InputSource(rssUrl.openStream());
       myXMLReader.parse(myInputSource);

       result.setText(streamTitle);

      } catch (MalformedURLException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
       result.setText("Cannot connect RSS!");
      } catch (ParserConfigurationException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
       result.setText("Cannot connect RSS!");
      } catch (SAXException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
       result.setText("Cannot connect RSS!");
      } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
       result.setText("Cannot connect RSS!");
      }


       }

       private class RSSHandler extends DefaultHandler
       {
        final int stateUnknown = 0;
        final int stateTitle = 1;
        int state = stateUnknown;

        int numberOfTitle = 0;
        String strTitle = "";
        String strElement = "";

      @Override
      public void startDocument() throws SAXException {
       // TODO Auto-generated method stub
       strTitle = "Nyheder fra ";
      }

      @Override
      public void endDocument() throws SAXException {
       // TODO Auto-generated method stub
       strTitle += "";
       streamTitle = "" + strTitle;
      }

      @Override
      public void startElement(String uri, String localName, String qName,
        Attributes attributes) throws SAXException {
       // TODO Auto-generated method stub
       if (localName.equalsIgnoreCase("title"))
       {
        state = stateTitle;
        strElement = "";
        numberOfTitle++;
       }
       else
       {
        state = stateUnknown;
       }
      }

      @Override
      public void endElement(String uri, String localName, String qName)
        throws SAXException {
       // TODO Auto-generated method stub
       if (localName.equalsIgnoreCase("title"))
       {
        strTitle += strElement + "\n"+"\n";
       }
       state = stateUnknown;
      }

      @Override
      public void characters(char[] ch, int start, int length)
        throws SAXException {
       // TODO Auto-generated method stub
       String strCharacters = new String(ch, start, length);
       if (state == stateTitle)
       {
        strElement += strCharacters;
       }
      }
    }

}
我不知道如何使用进度对话框。任何人都可以向我展示在哪里定义progressdialog,以及如何实现它。 我找了很多地方,但每个人似乎都用不同的方式,我无法让他们中的任何一个工作:(

我甚至试着做一个在睡眠状态下运行的假程序,但我不知道我做错了什么。

不要在UI线程中做任何长时间运行的事情,比如网络IO(这就是调用你的
onCreate()
),Android会强制关闭你的应用程序。相反,请使用链接的javadoc查看一个例子

  • onPreExecute()
    中创建并显示进度对话框,并在字段中保留对该对话框的引用
  • 通过从
    doInBackground()
    调用
    publishProgress()
    更新进度,并在
    onProgressUpdate()
    中进行处理,例如,如上面链接的示例
  • 关闭进度对话框并更新
    onPostExecute()中的UI(文本字段等)
要在
onPreExecute()
中创建对话框,请将
上下文
传递给您的
异步任务
构造函数,例如您的活动
,并将其存储在字段中

如果希望数据在定向更改后继续存在或在活动重新启动时保持不变,请让您的
AsyncTask
将解析后的数据写入,然后仅显示活动中的数据库内容


对于实际的“显示进度对话框”部分,使用其静态工厂方法之一
show(…)

这将一步创建并显示一个对话框。

我的首选方式:

class MyActivity extends Activity{

final static int PROGRESS_DIALOG = 1;

ProgressDialog dialog;

@Override
protected Dialog onCreateDialog(int id){
   switch(id){
   case PROGRESS_DIALOG:
      dialog = new ProgressDialog(this);
      dialog.setMessage("Whatever you want to tell them.");
      dialog.setIndeterminate(true);
      dialog.setCancelable(true); // if you want people to be able to cancel the download
      dialog.setOnCancelListener(new DialogInterface.OnCancelListener(){
         @Override
         public void onCancel(DialogInterface dialog)
         {
             **** cleanup.  Not needed if not cancelable ****
         }});
      return dialog;
   default:
      return null;
   }
}

当你想让它出现时,你可以做
showDialog(PROGRESS\u DIALOG)
当你想让它消失时,你可以做
DIALOG.disease()

还有这个^^^^^^^^^^^。你可以在
onPreExecute()
中做
showDialog(PROGRESS\u DIALOG)
,然后在
onPostExecute()中关闭它
。是的,像DeeV建议的那样使用托管对话框是可行的。尝试两种方法:)谢谢。我读了一些关于它的东西,但我认为有一种“更”轻松的方式来做。由于上面的代码是有效的,我认为这样做是可以的。但我将尝试在异步任务中进行操作。桑克斯:我已经试过了。我不知道在代码中从何处启动ProgressDialog以使其工作。当我开始的时候,我不能忽视它。我想我可能把东西放在了错误的范围内,而不是在方法中创建引用,它应该是类的全局引用。我会更新的。要启动进度,请在要启动时调用
showDialog(进度对话框)
。然后调用
dialog.disclose()
将其删除。我将进行一些编辑。我让它开始了几次,但之后它不能使用“取消”,即使我在全局类中创建了“进度”对话框。但我对Java非常陌生,并不完全理解所有规则。期待着使用这个解决方案,ass我已经用了将近3个小时做了尝试和错误:)如果你在
dialog
创建对象之前在
dialog=newprogressdialog(this)
上调用任何东西,你的应用程序将会崩溃。通过将该行放入
onCreate()
,可以确保不会发生这种情况。除此之外,如果窗口没有消失,那么即使您认为是,您也不会调用
dialog.discouse()
。在创建类时,我无法将上述代码粘贴到我的项目中。我得到一个语法错误,在(new DialogInterface.OnCancelListener(){@Override public void onCancel(DialogInterface dialog){****cleanup.Not needed if Not cancelable****})上删除这些标记;我不知道我做错了什么。虽然我以前完成过更大的任务,但我却因为不能做这么简单的事情而感觉自己像个十足的白痴。
class MyActivity extends Activity{

final static int PROGRESS_DIALOG = 1;

ProgressDialog dialog;

@Override
protected Dialog onCreateDialog(int id){
   switch(id){
   case PROGRESS_DIALOG:
      dialog = new ProgressDialog(this);
      dialog.setMessage("Whatever you want to tell them.");
      dialog.setIndeterminate(true);
      dialog.setCancelable(true); // if you want people to be able to cancel the download
      dialog.setOnCancelListener(new DialogInterface.OnCancelListener(){
         @Override
         public void onCancel(DialogInterface dialog)
         {
             **** cleanup.  Not needed if not cancelable ****
         }});
      return dialog;
   default:
      return null;
   }
}