1

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

在线程的 run() 中,如果我的互联网连接正常,则 ELSE 部分将执行并关闭该 ProgressBar。但是,如果我的互联网连接不工作,那么如果 pat 被执行,当我尝试关闭 ProgressBar 时,我得到空异常。我还添加了 null 检查if(m_ProgressDialog == null),并在 NULL 中打印 DialobBox ..

我的代码怎么了?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();
}
4

2 回答 2

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();
}

希望这可以帮助你..

于 2012-05-07T10:33:33.567 回答
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();

希望这有帮助...

于 2012-05-07T11:03:53.983 回答