在Activiy的onCreate()ProgressDialog问题:虚空中的Andr​​oid

问题描述:

解决方法:

好了,我已经找到了解决方案,从你的答案,因为你的答案是不完全的工作

Okay,I've found the solution from your answers since your answers are not completely working

首先,我们需要使用处理程序发表的Runnable 主/ UI线程来运行 UpdateDisplay()键,但定义 ProgressDialog UIThread下不会这也是至关重要的其他线程。

First we need to use Handler to post a Runnable to main/UI thread to run UpdateDisplay() and but define ProgressDialog under UIThread not other threads which was also crucial.

下面是最后的code:

Here is the final code :

public final String RSSFEEDOFCHOICE = "http://www.deals2buy.com/rssgen/tech.xml";

public final String tag = "RSSReader";
private RSSFeed feed = null;
private Handler handler = new Handler();
private ProgressDialog dialog;

/** Called when the activity is first created. */

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    dialog = ProgressDialog.show(RSSReader.this, "Loading",
            "Loading, please wait..");
    Thread t = new Thread() {
        public void run() {
            feed = getFeed(RSSFEEDOFCHOICE);
            handler.post(new Runnable() {
                public void run() {
                    dialog.dismiss();
                    UpdateDisplay();
                };
            });
        }
    };

    t.start();
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

问题:

在Android应用程序第一次加载,这是发生了什么:

When Android application first load, this is what happens :

  1. 从网址检索RSS内容,并解析成的RSSFeed通过饲料实例变量getFeed(字符串URL)
  2. 在显示这些饲料给用户的ListView通过 UpdateDisplay()方法。
  1. Retrieving RSS content from Url and parse it into RSSFeed feed instance variable via getFeed(String url).
  2. Showing these feeds to users in ListView via UpdateDisplay() method.

当这些发生的事情,我想表现一个ProgressDialog给用户,通知他们有关系统的retriving像加载中,请稍候......一旦 getFeed(字符串URL)的内容:了解RSSFeed 完成后,ProgressDialog将被开除和 UpdateDisplay():无效将被调用

When these are happening, I wanted to show a ProgressDialog to users to notify them about system are retriving content like "Loading, please wait..." Once getFeed(String Url):RSSFeed is done, ProgressDialog will be dismissed and UpdateDisplay():void will be invoked.

所以这是我第一次尝试:

public final String RSSFEEDOFCHOICE = "somewhere.xml"; //coming from http
public final String tag = "RSSReader";
private RSSFeed feed = null;
/** Called when the activity is first created. */

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    ProgressDialog dialog = ProgressDialog.show(RSSReader.this, "Loading...", "Loading, please wait");
    feed = getFeed(RSSFEEDOFCHOICE);
    dialog.dismiss();
    UpdateDisplay();
}

和它不显示 ProgressDialog 所以也没工作,我想要的方式。由于我belived这是因为ProgressDialog是继续进步,应该由其他发处理

And it doesn't show the ProgressDialog so it didn't work the way I want. Since I belived it is because ProgressDialog is continues progress, it should be handled by another Thread.

这是我第二次尝试:

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    Thread t = new Thread()
    {
        public void run()
        {
            ProgressDialog dialog = ProgressDialog.show(RSSReader.this, "Loading...", "Loading, please wait");
            feed = getFeed(RSSFEEDOFCHOICE);
            dialog.dismiss();
        }   
    };

    UpdateDisplay();
}

这是好的,直到 UpdateDisplay(),因为这种方法是由主线程处理,它不是在等待什么,所以我想,也许我应该把它放在 T 后,线程 dialog.dismiss()

It was okay till UpdateDisplay() since this method was handled by main thread and it wasn't waiting anything so I thought maybe I should have put it under t Thread after dialog.dismiss().

所以我这样做:

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    Thread t = new Thread()
    {
        public void run()
        {
            ProgressDialog dialog = ProgressDialog.show(RSSReader.this, "Loading...", "Loading, please wait");
            feed = getFeed(RSSFEEDOFCHOICE);
            dialog.dismiss();
                    UpdateDisplay();
        }   
    };
}

但后来调试器在运行时说:仅在主线程负责处理的onCreate():无效可以达到和使用View对象,因为它创建他们。

But then debugger said at run-time : only the main thread which handles onCreate():void can reach and use View objects because it creates them.

现在,我这么搞砸了,感觉就像戒烟节目。

Now, I am so messed up and feel like quitting programming.

有些人可以帮助我:/

Some one can help me :/

在此先感谢。

好吧,我发现从你的答案,因为你的答案是没有完全工作的解决方案

Okay,I've found the solution from your answers since your answers are not completely working

首先,我们需要使用处理程序发布一个Runnable主/ UI线程来运行 UpdateDisplay()和但定义 ProgressDialog UIThread 这也是至关重要的不是其他线程。

First we need to use Handler to post a Runnable to main/UI thread to run UpdateDisplay() and but define ProgressDialog under UIThread not other threads which was also crucial.

这是最后的code:

public final String RSSFEEDOFCHOICE = "http://www.deals2buy.com/rssgen/tech.xml";
public final String tag = "RSSReader";
private RSSFeed feed = null;
private Handler handler = new Handler();
private ProgressDialog dialog;

/** Called when the activity is first created. */

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    dialog = ProgressDialog.show(RSSReader.this, "Loading",
            "Loading, please wait..");
    Thread t = new Thread() {
        public void run() {
            feed = getFeed(RSSFEEDOFCHOICE);
            handler.post(new Runnable() {
                public void run() {
                    dialog.dismiss();
                    UpdateDisplay();
                };
            });
        }
    };

    t.start();
}