我有AsyncTask
查询数据库,然后返回boolean
。
我试图在onPostExecute
方法上设置进度条可见性。我得到了
nullpointerexception: Attempt to invoke virtual method 'void android.widget.ProgressBar.setVisibility(int)' on a null object reference
因为我无法正确引用它。
我该怎么做?
的AsyncTask :
public class TarefaValidaEmail extends AsyncTask<String, Void, Boolean> {
private Context context;
private Cadastro cadastro = new Cadastro();
public TarefaValidaEmail(Context context){
this.context = context;
}
@Override
protected Boolean doInBackground(String... params) {
String email = params[0];
Log.d("STRING BACKGROUND: ", email);
UsuarioDAO dao = new UsuarioDAO();
boolean isEmailValido = dao.isEmailValido(email);
return isEmailValido;
}
@Override
protected void onPostExecute(Boolean isEmailValido){
cadastro.isEmailValido(isEmailValido);
cadastro.progress.setVisibility(View.INVISIBLE);
}
}
活动(Cadastro):
public class Cadastro extends AppCompatActivity {
//Setando variáveis
//STANDS-FOR: Conferir password
private boolean senhaOk = false;
private boolean senhaOkFinal = false;
boolean emailValido;
EditText etEmail;
ProgressBar progress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cadastro);
//Cacheing EditTexts
final EditText etNome = (EditText) findViewById(R.id.etNome);
final EditText etSenha = (EditText) findViewById(R.id.etSenha);
final EditText etSenhaVerify = (EditText) findViewById(R.id.etSenhaVerify);
progress = (ProgressBar) findViewById(R.id.progress_email);
etEmail = (EditText) findViewById(R.id.etEmail);
//Ajustando fonte e inputType dos passwords
etSenha.setTypeface(Typeface.DEFAULT);
etSenha.setTransformationMethod(new PasswordTransformationMethod());
etSenhaVerify.setTypeface(Typeface.DEFAULT);
etSenhaVerify.setTransformationMethod(new PasswordTransformationMethod());
... ( Some code ) ...
//Adiciona TextWatchers (Listeners) aos campos de senha
etSenha.addTextChangedListener(twSenha);
etSenhaVerify.addTextChangedListener(twSenhaVerify);
//Loader animation & check-email
etEmail.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
progress.setVisibility(View.VISIBLE);
TarefaValidaEmail tarefa = new TarefaValidaEmail(Cadastro.this);
tarefa.execute(etEmail.getText().toString());
}
}
});
}
//Metodo que recebe verificação do email
public void isEmailValido(Boolean isValido){
progress.setVisibility(View.INVISIBLE);
}
}
已经在Cadastro活动和Tarefa AsyncTask类上尝试Cadastro.this.progress.setVisib..
。谢谢!
答案 0 :(得分:1)
取消onpostexecute()方法中的进度条
progress.dismiss();
答案 1 :(得分:1)
下面:
Cadastro.this
导致问题。
因为您要向TarefaValidaEmail
课程发送progress
,所以请访问public TarefaValidaEmail(Context context){
this.context = context;
this.cadastro=((Cadastro)context);
}
:
select case when Empname is null then 'NULL' else Empname end
from
(
select null as Empname
union
select Empname from EmpComp
) a