Home » Java » java – I was trying to sign in and rearrange Android Studio, I used firebase. but when i run the screen turns on and off immediately-Exceptionshub

java – I was trying to sign in and rearrange Android Studio, I used firebase. but when i run the screen turns on and off immediately-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

/AndroidRuntime: FATAL EXCEPTION: main
Process: com.aytacmelihkilic.badboy, PID: 26142
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.aytacmelihkilic.badboy/com.aytacmelihkilic.badboy.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method ‘void android.widget.TextView.setOnClickListener(android.view.View$OnClickListener)’ on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2977)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3114)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:113)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:71)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1859)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6831)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:927)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method ‘void android.widget.TextView.setOnClickListener(android.view.View$OnClickListener)’ on a null object reference
at com.aytacmelihkilic.badboy.MainActivity.onCreate(MainActivity.java:67)
at android.app.Activity.performCreate(Activity.java:7224)
at android.app.Activity.performCreate(Activity.java:7213)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2957)

MainActivity

    package com.aytacmelihkilic.badboy;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;

public class MainActivity extends AppCompatActivity {
    EditText emailId,password;
    Button btnSignUp;
    TextView tvSignIn;
    FirebaseAuth mAuth;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
   mAuth=FirebaseAuth.getInstance();
   emailId=findViewById(R.id.editText);
   password=findViewById(R.id.editText2);
   btnSignUp=findViewById(R.id.button);
   tvSignIn=findViewById(R.id.textView);
   btnSignUp.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
           String email = emailId.getText().toString();
           String pwd=password.getText().toString();
           if(email.isEmpty()){
               emailId.setError("lütfen e maili tekrar girin");
           }
           else if (pwd.isEmpty()){
               password.setError("şifre yanlış");
               password.requestFocus();
           }
           else if (email.isEmpty()&&pwd.isEmpty()){
               Toast.makeText(MainActivity.this,"alanlar boş",Toast.LENGTH_SHORT).show();
           }
           else if (!(email.isEmpty()&&pwd.isEmpty())){
               mAuth.createUserWithEmailAndPassword(email,pwd).addOnCompleteListener(MainActivity.this, new OnCompleteListener<AuthResult>() {
                   @Override
                   public void onComplete(@NonNull Task<AuthResult> task) {
                       if (!task.isSuccessful()){
                           Toast.makeText(MainActivity.this,"kayıt başarısız lütfen tekrar deneyiniz",Toast.LENGTH_SHORT).show();
                       }
                       else {
                           startActivity(new Intent(MainActivity.this,HomeActivity.class));
                       }
                   }
               });
           }
           else {
               Toast.makeText(MainActivity.this,"ERROR!",Toast.LENGTH_SHORT).show();

           }
       }
   });
   tvSignIn.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
           Intent i =new Intent(MainActivity.this,LoginActivity.class);
       startActivity(i);
       }
   });
    }
}

HomeActivity

package com.aytacmelihkilic.badboy;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.google.firebase.auth.FirebaseAuth;

public class HomeActivity extends AppCompatActivity {
    Button btnLogout;
    FirebaseAuth mAuth;
    private FirebaseAuth.AuthStateListener mAuthStateListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        btnLogout=findViewById(R.id.logout);
        btnLogout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FirebaseAuth.getInstance().signOut();
                Intent inToMailn=new Intent(HomeActivity.this,MainActivity.class);
                startActivity(inToMailn);
            }
        });


    }
}

LoginActivity

package com.aytacmelihkilic.badboy;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

public class LoginActivity extends AppCompatActivity {
    EditText emailId,password;
    Button btnSignIn;
    TextView tvSignUp;
    FirebaseAuth mAuth;
    private FirebaseAuth.AuthStateListener mAuthStateListener;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        mAuth=FirebaseAuth.getInstance();
        emailId=findViewById(R.id.editText);
        password=findViewById(R.id.editText2);
        btnSignIn=findViewById(R.id.button);
        tvSignUp=findViewById(R.id.textView);
        mAuthStateListener=new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                FirebaseUser user = mAuth.getCurrentUser();
                if (user != null) {

                    Toast.makeText(LoginActivity.this, "giriş yaptın", Toast.LENGTH_SHORT).show();
                    Intent i =new Intent(LoginActivity.this,HomeActivity.class);
                    startActivity(i);
                }
                else {
                    Toast.makeText(LoginActivity.this, "Lütfen giriş yapın", Toast.LENGTH_SHORT).show();

                }
            }

        };
        btnSignIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                {
                    String email = emailId.getText().toString();
                    String pwd=password.getText().toString();
                    if(email.isEmpty()){
                        emailId.setError("lütfen e maili tekrar girin");
                    }
                    else if (pwd.isEmpty()){
                        password.setError("şifre yanlış");
                        password.requestFocus();
                    }
                    else if (email.isEmpty()&&pwd.isEmpty()){
                        Toast.makeText(LoginActivity.this,"alanlar boş",Toast.LENGTH_SHORT).show();
                    }
                    else if (!(email.isEmpty()&&pwd.isEmpty())){
                        mAuth.signInWithEmailAndPassword(email,pwd).addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
                            @Override
                            public void onComplete(@NonNull Task<AuthResult> task) {
                                if(!task.isSuccessful()){
                                    Toast.makeText(LoginActivity.this,"girişde bir hata oluşdu,yekrar girmeyi deneyin",Toast.LENGTH_SHORT).show();

                                }
                                else {
                                    Intent intToHome=new Intent(LoginActivity.this,HomeActivity.class);
                                    startActivity(intToHome);
                                }
                            }
                        });
                    }
                    else {
                        Toast.makeText(LoginActivity.this,"ERROR!",Toast.LENGTH_SHORT).show();

                    }
                }
            }
        });
        tvSignUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intSingnUp=new Intent(LoginActivity.this,MainActivity.class);
                startActivity(intSingnUp);
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        mAuth.addAuthStateListener(mAuthStateListener);
    }
}
How to&Answers: