Onclick crashing app, in database connection

Asked

Viewed 50 times

2

Hello. I am developing a simple app with login and password, but there are always problems when trying to log in. It is worth saying that I am using Okhttp.

This is Java:

EditText txt_Email, txt_Pass;
TextView reg;
Button btn_Login;

String url = "";
String parametros = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login_screen);

    txt_Email = (EditText) findViewById(R.id.email);
    txt_Pass = (EditText) findViewById(R.id.password);
    btn_Login = (Button) findViewById(R.id.button);
    reg = (TextView) findViewById(R.id.textView2);

    reg.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent abreCadastro = new Intent(LoginScreen.this, SplashScreen.class);
            startActivity(abreCadastro);
        }
    });

    /*btn_Login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


        }
    });*/

}

public void ClickLogin(View view){
    ConnectivityManager connMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    if (networkInfo != null && networkInfo.isConnected()) {
        String email = txt_Email.getText().toString();
        String senha = txt_Pass.getText().toString();

        if(email.isEmpty() || senha.isEmpty()) {
            Toast.makeText(getApplicationContext(), "Preencha todos os dados", Toast.LENGTH_SHORT).show();
        }else {
            url = "http://meudominio.host.com/login.php?email=" + email + "&senha=" + senha;
            parametros = url;
            new SolicitaDados().execute(url);
        }

    } else {
        Toast.makeText(getApplicationContext(), "Falha de Conexão", Toast.LENGTH_SHORT).show();
    }
}

private class SolicitaDados extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {

        return Conector.postDados(urls[0], parametros);
    }
    // onPostExecute displays the results of the AsyncTask.
    @Override
    protected void onPostExecute(String resultado) {
        reg.setText(resultado);
        if(resultado.contains("login_ok")) {
            Toast.makeText(getApplicationContext(), "Login Efetuado", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(getApplicationContext(), "Dados Incorretos", Toast.LENGTH_SHORT).show();
        }
    }
}


/*@Override
protected void onPause() {
    super.onPause();
    finish();
}*/

This is the XML of Activity:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="12dp"
    tools:context="com.axis.guardiao.LoginScreen">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <EditText
            android:id="@+id/email"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Email"
            android:inputType="textPersonName" />

        <EditText
            android:id="@+id/password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Senha"
            android:inputType="textPassword" />

        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="ClickLogin"
            android:text="Logar" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Cadastre-se gratuitamente"
            android:textAlignment="center"
            android:textColor="@android:color/holo_blue_dark"
            android:textSize="16sp" />
    </LinearLayout>

</RelativeLayout>

This is the mistake:

06-05 18:36:47.882 5005-5005/com.axis. EXCEPTION: main Process: com.axis.Uardiao, PID: 5005 java.lang.Illegalstateexception: Could not execute method for android:onClick at android.support.v7.app.Appcompatviewinflater$Declaredonclicklistener.onClick(Appcompatviewinflater.java:293) at android.view.View.performClick(View.java:5637) at android.view.View$Performclick.run(View.java:22429) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.Activitythread.main(Activitythread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.Internal.os.Zygoteinit$Methodandargscaller.run(Zygoteinit.java:886) at com.android.Internal.os.Zygoteinit.main(Zygoteinit.java:776) Caused by: java.lang.reflect.Invocationtargetexception at java.lang.reflect.Method.invoke(Native Method) at android.support.v7.app.Appcompatviewinflater$Declaredonclicklistener.onClick(Appcompatviewinflater.java:288) at android.view.View.performClick(View.java:5637)  at android.view.View$Performclick.run(View.java:22429)  at android.os.Handler.handleCallback(Handler.java:751)  at android.os.Handler.dispatchMessage(Handler.java:95)  at android.os.Looper.loop(Looper.java:154)  at android.app.Activitythread.main(Activitythread.java:6119)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.Internal.os.Zygoteinit$Methodandargscaller.run(Zygoteinit.java:886)  at com.android.Internal.os.Zygoteinit.main(Zygoteinit.java:776)  Caused by: java.lang.Securityexception: Connectivityservice: Neither user 10071 nor Current process has android.permission.ACCESS_NETWORK_STATE. at android.os.Parcel.readException(Parcel.java:1684) at android.os.Parcel.readException(Parcel.java:1637) at android.net.Iconnectivitymanager$Stub$Proxy.getActiveNetworkInfo(Iconnectivitymanager.java:1170) at android.net.Connectivitymanager.getActiveNetworkInfo(Connectivitymanager.java:745) at com.axis.Guardiao.LoginScreen.Clicklogin(Loginscreen.java:55) at java.lang.reflect.Method.invoke(Native Method)  at android.support.v7.app.Appcompatviewinflater$Declaredonclicklistener.onClick(Appcompatviewinflater.java:288)  at android.view.View.performClick(View.java:5637)  at android.view.View$Performclick.run(View.java:22429)  at android.os.Handler.handleCallback(Handler.java:751)  at android.os.Handler.dispatchMessage(Handler.java:95)  at android.os.Looper.loop(Looper.java:154)  at android.app.Activitythread.main(Activitythread.java:6119)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.Internal.os.Zygoteinit$Methodandargscaller.run(Zygoteinit.java:886)  at com.android.Internal.os.Zygoteinit.main(Zygoteinit.java:776)

Thank you in advance

1 answer

1


Next, I realized that I had not put the access permission to the internet status, just open the Androidmanifest.xml file and add the following line

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

The file is in the directory "Manifest/Androidmanifest.xml"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.