为什么ProgressDialog Box给出NULL?

时间:2012-05-07 10:11:59

标签: android progressdialog

这是我的代码。这里我运行一个带有标题的ProgressBar的线程。

在线程的run()中,如果我的互联网连接正常,那么ELSE部分将获得执行&解雇ProgressBar。但是,如果我的互联网连接不起作用,那么IF pat会执行,这里我尝试解除ProgressBar时会出现null异常。我还添加了null检查 if(m_ProgressDialog == null),并将DialobBox打印为NULL ..

我的代码发生了什么事。 ProgressBar在ELSE部分中解雇,但在IF中抛出NULL异常......

public void onCreate(Bundle savedInstanceState) {
.....................
.....................
//some CODE //
.....................
.....................
viewOrders = new Runnable(){
                @Override
                public void run() {
                    try {
                        if(!utilityFunctions.HaveNetworkConnection(SplashScreenActivity.this)) //checking internet connection
                        {
                            Log.i(UtilityFunctions.APP_TAG, "NO Connecttion");
                            //updateUI(0);
                            if(m_ProgressDialog == null)
                                Log.i(UtilityFunctions.APP_TAG, "DialogBox is NULL");
                            else
                                m_ProgressDialog.dismiss();

                            Log.i(UtilityFunctions.APP_TAG, "Dismissed");
                            handler.sendEmptyMessage(0);
                        }
                        else
                        {                                  
                            try {                                   
                                m_ProgressDialog.dismiss();                                                                 
                                }
                            } catch (Exception e1) {
                                // TODO Auto-generated catch block
                                e1.printStackTrace();
                            }                       


                            Intent mainIntent = new Intent().setClass(SplashScreenActivity.this, MainActivity.class);
                            Log.i(UtilityFunctions.APP_TAG, "Starting Program");                    

                            startActivity(mainIntent);
                            finish();
                        }
                    }
                    catch(Exception e) {
                        e.printStackTrace();
                    }

                }

                private Handler handler = new Handler()
                {
                    @Override
                    public void handleMessage(Message msg) {
                        //super.dispatchMessage(msg);
                        super.handleMessage(msg);
                        updateUI(msg.what);
                    }
                };  
            };

            Thread thread =  new Thread(null, viewOrders, "MagentoBackground");
            thread.start();
            m_ProgressDialog = ProgressDialog.show(SplashScreenActivity.this, "Please wait...", "Getting required data from server. This is an one time activity...", true);
}



public void updateUI(int code){
        Log.i(UtilityFunctions.APP_TAG, "updateUI");
        if(code == 0)
            Toast.makeText(SplashScreenActivity.this, "Unable to verify application signature. Please Check your internet connection & try again", Toast.LENGTH_LONG).show();
        else
            Toast.makeText(SplashScreenActivity.this, "Unable to process request. ", Toast.LENGTH_LONG).show();
}

2 个答案:

答案 0 :(得分:4)

尝试在顶部声明ProgressDialog()

public void onCreate(Bundle savedInstanceState) {
....................
.....................
//some CODE //
m_ProgressDialog = ProgressDialog.show(SplashScreenActivity.this, "Please wait...", 
"Getting required data from server. This is an one time activity...", true);
.....................
viewOrders = new Runnable(){
            @Override
            public void run() {
                try {
                      if(!utilityFunctions.HaveNetworkConnection(SplashScreenActivity.this)) //checking internet connection
                    {
                        Log.i(UtilityFunctions.APP_TAG, "NO Connecttion");
                        //updateUI(0);
                        if(m_ProgressDialog == null)
                            Log.i(UtilityFunctions.APP_TAG, "DialogBox is NULL");
                        else
                            m_ProgressDialog.dismiss();

                        Log.i(UtilityFunctions.APP_TAG, "Dismissed");
                        handler.sendEmptyMessage(0);
                    }
                    else
                    {                                  
                        try {                                   
                            m_ProgressDialog.dismiss();                                                                 
                            }
                        } catch (Exception e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }                       


                        Intent mainIntent = new Intent().setClass(SplashScreenActivity.this, MainActivity.class);
                        Log.i(UtilityFunctions.APP_TAG, "Starting Program");                    

                        startActivity(mainIntent);
                        finish();
                    }
                }
                catch(Exception e) {
                    e.printStackTrace();
                }

            }

            private Handler handler = new Handler()
            {
                @Override
                public void handleMessage(Message msg) {
                    //super.dispatchMessage(msg);
                    super.handleMessage(msg);
                    updateUI(msg.what);
                }
            };  
        };

        Thread thread =  new Thread(null, viewOrders, "MagentoBackground");
        thread.start();

}



public void updateUI(int code){
    Log.i(UtilityFunctions.APP_TAG, "updateUI");
    if(code == 0)
        Toast.makeText(SplashScreenActivity.this, "Unable to verify application   
signature. Please Check your internet connection & try again", 
Toast.LENGTH_LONG).show();
    else
        Toast.makeText(SplashScreenActivity.this, "Unable to process request. ",  
Toast.LENGTH_LONG).show();
}

希望这可以帮助你..

答案 1 :(得分:1)

在调用线程之前初始化进度对话框。

m_ProgressDialog = ProgressDialog.show(SplashScreenActivity.this, "Please wait...", "Getting required data from server. This is an one time activity...", true);
    Thread thread =  new Thread(null, viewOrders, "MagentoBackground");
    thread.start();

希望这有用...