0
I have a web-based system where his database is with Mysql. I’m starting now to develop the application on Android using Android Studio. I wonder if you have how to create Mysql connection in Android Studio, taking advantage of the tables of this system that are already ready and populated. I have seen some models using Sqlite, but it would not be my case. I tried with the code below, but when I run, it shows that the app stopped working:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tela_login);
editEmail = (EditText)findViewById(R.id.editEmail);
editSenha = (EditText)findViewById(R.id.editSenha);
btnAcessar = (Button)findViewById(R.id.btnAcessar);
txtLembrarSenha = (TextView)findViewById(R.id.txtLembrarSenha);
btnAcessar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ConnectivityManager connectivityManager =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if(networkInfo != null && networkInfo.isConnected()){
String email = editEmail.getText().toString();
String senha = editSenha.getText().toString();
if(email.isEmpty() || senha.isEmpty()){
Toast.makeText(getApplicationContext(),"Favor preencher corretamente os campos",Toast.LENGTH_LONG).show();
}else{
url = "http://192.168.0.13/projeto/conexao-mobile/logar.php";
parametros = "email=" + email + "&senha=" + senha;
new SolicitaDados().execute(url);
}
}else {
Toast.makeText(getApplicationContext(), "Nenhuma conexão ativa", Toast.LENGTH_LONG).show();
}
}
});
}
private class SolicitaDados extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls){
return Conexao.postDados(urls[0], parametros);
}
@Override
protected void onPostExecute(String resultado){
// textView.setText(result);
txtErro.setText(resultado);
}
}
The error that appears is:
01-21 17:20:21.947 16889-16889/br.com.projeto.acessosistema E/AndroidRuntime: FATAL EXCEPTION: main
Process: br.com.projeto.acessosistema, PID: 16889
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at br.com.projeto.acessosistema.TelaLogin$SolicitaDados.onPostExecute(TelaLogin.java:78)
at br.com.projeto.acessosistema.TelaLogin$SolicitaDados.onPostExecute(TelaLogin.java:66)
at android.os.AsyncTask.finish(AsyncTask.java:660)
at android.os.AsyncTask.-wrap1(AsyncTask.java)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:677)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
The Connection I’m using in a basic way:
<?php
$email = $_POST["Login"];
$senha = $_POST["Senha"];
$conexao = mysqli_connect('192.168.0.13','root','','projeto');
$sql = mysqli_query($conexao,"SELECT * FROM pe_acessos Email = '".$email."' AND SenhaAcessos = '".$senha."'");
if(mysqli_num_rows($sql) == 0){
echo "Login ou senha inválidos";
}else{
echo "Vamos conectar";
}
?>
The database as well as the tables exist.
The variable
txtErroisnula. Maybe you don’t add theTextViewor theTextViewthat you tried to add does not exist.– Valdeir Psr
Thank you Valdeir, that was exactly it.
– user24136