代码之家  ›  专栏  ›  技术社区  ›  Mez

如何在应用程序启动时显示ProgressDialog?

  •  1
  • Mez  · 技术社区  · 15 年前

    阿卡,这怎么了code:-

    package com.mez.appofjaq;
    
    import com.mez.appofjaq.RssParser.RssFeed;
    
    import android.app.Activity;
    import android.app.ProgressDialog;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.webkit.WebView;
    
    public class AppOfJaq extends Activity {
    
        protected static final int REFRESH = 0;
    
        String streamTitle = "";
        protected ProgressDialog pd;
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            pd.show(this, "Loading..", "Loading Feed");
            setContentView(R.layout.main);
            new Thread(new Runnable(){
                public void run() {
                    loadFeed();
                    pd.dismiss();
                }
            }).start();
    
        }
    
        private void loadFeed()
        {
            RssParser rp = new RssParser("http://shotofjaq.org/feed");
            rp.parse();
    
            RssFeed feed = rp.getFeed();
    
            if (feed.getItems() != null)
            {
                streamTitle = feed.getItems().get(0).content;
            }
            WebView result = (WebView)findViewById(R.id.result);
            result.loadData(streamTitle, "text/html", "utf-8");
        }
    
    }
    
    3 回复  |  直到 15 年前
        1
  •  1
  •   kiswa    15 年前

    马克斯和亚历克斯都是对的(而且都遗漏了信息)。

    true 最后,它通过调用ProgressDialog.show而不是pd.show来修复空指针异常(NPE)。

    我知道这本身并不是一个确切的答案,但我想这会帮助其他人知道为什么亚历克斯和麦克阿瑟的答案都是正确的。

    基于上述原因,最终结果是您应该拥有以下代码:

    pd = ProgressDialog.show(this, "Loading...", "Loading Feed", true);
    

    或者(如果对字符串使用strings.xml):

    pd = ProgressDialog.show(this, getString(R.string.loading_title),
                             getString(R.string.loading_msg), true); 
    
        2
  •  0
  •   Macarse    15 年前

    从我的代码:

    pd = ProgressDialog.show(this, "", getString(R.string.loading_msg), true);
    
        3
  •  0
  •   yanchenko    15 年前

    pd.show(this, "Loading..", "Loading Feed");

    应该是

    pd = ProgressDialog.show(this, "Loading..", "Loading Feed");
    

    正如麦克阿瑟所说。