【问题标题】:how to add progress bar while webservice call?webservice调用时如何添加进度条?
【发布时间】:2015-12-21 13:00:10
【问题描述】:

运行此代码时出现空指针错误!请给我解决方案。

\\............
new LoginCheck().execute(txt_username.getText().toString(),txt_password.getText().toString());
.............//

class LoginCheck extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        progressBar = (ProgressBar) findViewById(R.id.progressBar1);
        progressBar.setVisibility(View.VISIBLE);
        super.onPreExecute();

    }
    protected String         doInBackground(String... params) {

        String responsetring = "";
        try {
            SoapObject request = new SoapObject(NAMESPACE, "Logincheck");
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet = true;
            envelope.setOutputSoapObject(request);
            request.addProperty("username",params[0]);
            request.addProperty("password",params[1]);
            //Msg("Version"+appversion+"AppName"+Appname,Toast.LENGTH_LONG);
            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            try {
                androidHttpTransport.call(NAMESPACE + "Logincheck", envelope);
            } catch (IOException | XmlPullParserException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Msg(e.toString(), Toast.LENGTH_LONG);
            }
            SoapPrimitive response;
            try {
                response = (SoapPrimitive) envelope.getResponse();
                responsetring = response.toString();
            //    Msg(responsetring.toString(), 1);
            } catch (SoapFault e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Msg(e.toString(), Toast.LENGTH_LONG);
            }
            String[] splitrows = responsetring.split(";");
            String usernamestr = "";
            String deviceid = "";
            String isanydevice = "";

            if (responsetring.equals("false")) {
           //     Helper.InfoMsg("Alert", "Please Check userame and password and confirm device is valid", LoginActivity.this);

            } else {
                usernamestr = splitrows[0];
                deviceid = splitrows[1];
                isanydevice = splitrows[2];
                TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
                if (deviceid.equals(tm.getDeviceId().toString())) {
                    LoginActivity.this.username = usernamestr;
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent);
                    overridePendingTransition(R.anim.right_in, R.anim.left_out);
                } else {
                    TelephonyManager tm1 = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
              //      Helper.InfoMsg("Your Device Id", (tm1.getDeviceId().toString()), LoginActivity.this);
                    txt_warning.setText((tm1.getDeviceId().toString()));
                    if (isanydevice.equalsIgnoreCase("true")) {
                        LoginActivity.this.username = usernamestr;
                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                        startActivity(intent);
                        overridePendingTransition(R.anim.right_in, R.anim.left_out);
                    } else {
                    //    Helper.warning("Sorry", "Not Allowed To Login  Other devices", LoginActivity.this);
                        // return false;
                    }
                }

            }
            //return true;
        } catch (Exception e) {
            Msg(e.toString(), 1);
            //  return false;
        }
        return null;
    }


    protected void onPostExecute(String result) {

        progressBar.setVisibility(View.INVISIBLE);
    }






}

【问题讨论】:

  • 哪里出现空指针异常?
  • 请张贴您的 logcat @prabhu
  • java.lang.NullPointerException 在 android.app.Dialog.(Dialog.java:165) 在 android.app.AlertDialog.(AlertDialog.java:114) 在 android。 app.AlertDialog.(AlertDialog.java:110) 在 android.app.ProgressDialog.(ProgressDialog.java:82) 在 com.scm.androscm.LoginActivity$LoginCheck.(LoginActivity.java: 185)
  • 在 com.scm.androscm.LoginActivity$1$1.run(LoginActivity.java:165) 在 android.app.Activity.runOnUiThread(Activity.java:4713) 在 com.scm.androscm.LoginActivity $1.onClick(LoginActivity.java:162) at android.view.View.performClick(View.java:4438) at android.view.View$PerformClick.run(View.java:18422) at android.os.Handler.handleCallback (Handler.java:733) 在 android.os.Handler.dispatchMessage(Handler.java:95) 在 android.os.Looper.loop(Looper.java:136)

标签: android android-asynctask progressdialog


【解决方案1】:

您在 Asynctask 中唯一遗漏的是,您忘记了覆盖 onProgressUpdate() 方法。 onProgressUpdate() 方法用于在后台计算仍在执行时在用户界面中显示任何形式的进度。我在这里给出了一个示例代码。参考它。希望对您有所帮助。

 protected void onProgressUpdate(String... progress) 
{        
    progressBar.setProgress(Integer.parseInt(progress[0]));
} 

P.S: 另请参考以下链接。

http://www.coderanch.com/t/612017/Android/Mobile/AsyncTask-show-progress-bar

【讨论】:

  • 非常感谢您的支持。错误已清除,我的项目现在正在运行!
  • 如果对您有帮助,请接受答案。我很高兴能提供帮助。
【解决方案2】:

在 onPreExecute 中:

 AlertDialog progressDialog = new ProgressDialog(context, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
        progressDialog.setMessage("Please Wait...");
        progressDialog.show();

在 onPostExecute 中

 progressDialog.dismiss();

【讨论】:

    【解决方案3】:

    在 onPreExecute() 中

    private ProgressDialog pdia;
    
        @Override
        protected void onPreExecute(){
            super.onPreExecute();
            pdia = new ProgressDialog(LoginActivity.this);
            pdia.setMessage("Loading, Please Wait...");
            pdia.show();
        }
    

    在 onPostExecute() 中

       protected void onPostExecute(String result) {
    
        super.onPostExecute(result);
            pdia.dismiss();
    }
    

    所有代码:

    class LoginCheck extends AsyncTask<String, String, String> {
    
    private ProgressDialog pdia;
    
        @Override
        protected void onPreExecute(){
            super.onPreExecute();
            pdia = new ProgressDialog(LoginActivity.this);
            pdia.setMessage("Loading, Please Wait...");
            pdia.show();
        }
    
    protected String doInBackground(String... params) {
    
        String responsetring = "";
        try {
            SoapObject request = new SoapObject(NAMESPACE, "Logincheck");
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet = true;
            envelope.setOutputSoapObject(request);
            request.addProperty("username",params[0]);
            request.addProperty("password",params[1]);
            //Msg("Version"+appversion+"AppName"+Appname,Toast.LENGTH_LONG);
            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            try {
                androidHttpTransport.call(NAMESPACE + "Logincheck", envelope);
            } catch (IOException | XmlPullParserException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Msg(e.toString(), Toast.LENGTH_LONG);
            }
            SoapPrimitive response;
            try {
                response = (SoapPrimitive) envelope.getResponse();
                responsetring = response.toString();
            //    Msg(responsetring.toString(), 1);
            } catch (SoapFault e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Msg(e.toString(), Toast.LENGTH_LONG);
            }
            String[] splitrows = responsetring.split(";");
            String usernamestr = "";
            String deviceid = "";
            String isanydevice = "";
    
            if (responsetring.equals("false")) {
           //     Helper.InfoMsg("Alert", "Please Check userame and password and confirm device is valid", LoginActivity.this);
    
            } else {
                usernamestr = splitrows[0];
                deviceid = splitrows[1];
                isanydevice = splitrows[2];
                TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
                if (deviceid.equals(tm.getDeviceId().toString())) {
                    LoginActivity.this.username = usernamestr;
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent);
                    overridePendingTransition(R.anim.right_in, R.anim.left_out);
                } else {
                    TelephonyManager tm1 = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
              //      Helper.InfoMsg("Your Device Id", (tm1.getDeviceId().toString()), LoginActivity.this);
                    txt_warning.setText((tm1.getDeviceId().toString()));
                    if (isanydevice.equalsIgnoreCase("true")) {
                        LoginActivity.this.username = usernamestr;
                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                        startActivity(intent);
                        overridePendingTransition(R.anim.right_in, R.anim.left_out);
                    } else {
                    //    Helper.warning("Sorry", "Not Allowed To Login  Other devices", LoginActivity.this);
                        // return false;
                    }
                }
    
            }
            //return true;
        } catch (Exception e) {
            Msg(e.toString(), 1);
            //  return false;
        }
        return null;
    }
    
    
    protected void onPostExecute(String result) {
    
        super.onPostExecute(result);
            pdia.dismiss();
    }
    

    【讨论】:

      猜你喜欢
      • 2012-02-13
      • 2020-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多