2016-02-15 2 views
0

Я использую supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS) и для того, чтобы показать или скрыть ProgressBar, я использую setSupportProgressBarIndeterminateVisibility().
Мотив заключается в том, что адрес электронной почты отправляется на сервер после его проверки, а между тем ожидается ответ от сервера и отображается ProgressBar. При получении ответа, ProgressBar скрыт.
Мой код должен был работать. Пожалуйста, смотрите ниже.
Android - setSupportProgressBarIndeterminateVisibility (true) не работает

public class LoginActivity extends ActionBarActivity { 

String response = ""; 
String emailAddress, Password; 
EditText email, password; 
Button sign_in; 
TextView register, forgotPassword; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_login); 
    sign_in = (Button) findViewById(R.id.sign_in); 
    register = (TextView) findViewById(R.id.register); 
    forgotPassword = (TextView) findViewById(R.id.forgotPassword); 


    register.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Intent intent = new Intent(LoginActivity.this, RegisterActivity.class); 
      startActivity(intent); 
     } 
    }); 

    sign_in.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 

      email = (EditText) findViewById(R.id.email); 
      password = (EditText) findViewById(R.id.password); 
      emailAddress = email.getText().toString(); 
      Password = password.getText().toString(); 

      if (!emailAddress.equals("") && !Password.equals("")){ 
       setSupportProgressBarIndeterminateVisibility(true); 
        try { 
         response = new Connection().execute().get().toString(); 
        } catch (InterruptedException e) { 
         e.printStackTrace(); 
        } catch (ExecutionException e) { 
         e.printStackTrace(); 
        } 

       if (response.equals("Credentials Validated!")) { 
        setSupportProgressBarIndeterminateVisibility(false); 
        Toast.makeText(LoginActivity.this, response, Toast.LENGTH_SHORT).show(); 
        Intent intent = new Intent(LoginActivity.this, Home.class); 
        intent.putExtra("emailAddress", emailAddress); 
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); 
        finish(); 
        startActivity(intent); 
       } else { 
        setSupportProgressBarIndeterminateVisibility(false); 
        final AlertDialog alertDialog = new AlertDialog.Builder(
          LoginActivity.this).create(); 

        // Setting Dialog Title 
        alertDialog.setTitle("We need your attention!"); 

        // Setting Dialog Message 
        alertDialog.setMessage(response); 

        // Setting Icon to Dialog 
        alertDialog.setIcon(R.drawable.ic_launcher); 

        // Setting OK Button 
        alertDialog.setButton("OK", new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int which) { 
          // Write your code here to execute after dialog closed 
          alertDialog.cancel(); 
         } 
        }); 

        // Showing Alert Message 
        alertDialog.show(); 
       } 
      } 
      else { 
       if(emailAddress.equals("")) { 
        email.setError("Email Address Cannot Be Empty!"); 
       } 
       else 
        if(Password.equals("")){ 
         password.setError("Password Cannot Be Empty!"); 
        } 
      } 
     } 
    }); 


} 


private class Connection extends AsyncTask { 
    @Override 
    protected String doInBackground(Object[] objects) { 
     LoginJSONParser loginJsonParser = new LoginJSONParser(); 
     return loginJsonParser.getJson(emailAddress, Password); 
    } 
} 

}

ответ

0

заменить:

setSupportProgressBarIndeterminateVisibility(true); 

вместо

setProgressBarIndeterminateVisibility(true); 

, потому что вы расширяете ActionBarActivity. (потому что вы используете supportRequestWindowFeature вместо requestWindowFeature)

+0

Я уже пробовал 'setProgressBarIndeterminateVisibility (true)' сбой, который я переключил на 'setSupportProgressBarIndeterminateVisibility (true)'. –

+0

и вы также замените supportRequestWindowFeature на запросWindowFeature? – Saif

+0

Да! Я даже сделал это. –