无法启动活动组件信息{e\u homes.app.com.e\u homes/e\u homes.app.com.e\u homes.location}:java.lang.nullpointerexception。这个错误出现在我的代码中。我使用了firebase身份验证,但是当我登录时,它给出了上面的错误。这是密码
public class MainActivity extends AppCompatActivity {
private FirebaseAuth fb;
private ProgressDialog progressDialog;
private FirebaseAuth.AuthStateListener mAuthListener;
Button txtRegister;
EditText etEmail,etPassword;
Button Register;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fb = FirebaseAuth.getInstance();
txtRegister = (Button) findViewById(R.id.signup);
etEmail = (EditText) findViewById(R.id.username);
etPassword = (EditText) findViewById(R.id.password);
Register = (Button) findViewById(R.id.signin);
progressDialog = new ProgressDialog(this);
mAuthListener=new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if(firebaseAuth.getCurrentUser()!=null) {
startActivity(new Intent(getApplicationContext(),
Location.class));
}
}
};
Register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
userLogin();
}
});
txtRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
Intent intent = new
Intent(MainActivity.this,Registration.class);
startActivity(intent);
}
});
}
@Override
protected void onStart() {
fb.addAuthStateListener(mAuthListener);
super.onStart();
}
private void userLogin() {
String email_id = etEmail.getText().toString().trim();
String password = etPassword.getText().toString().trim();
if (TextUtils.isEmpty(email_id)) {
Toast.makeText(this, "please enter your Email_Id",
Toast.LENGTH_SHORT).show();
return;
} else if (TextUtils.isEmpty(password)) {
Toast.makeText(this, "please enter your password",
Toast.LENGTH_SHORT).show();
return;
} else {
progressDialog.setMessage("Logining in....Please wait....");
progressDialog.show();
fb.signInWithEmailAndPassword(email_id, password).
addOnCompleteListener(this, new
OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
progressDialog.dismiss();
if (task.isSuccessful()) {
// startActivity(new
Intent(getApplicationContext(), Location.class));
// finish();
} else {
Toast.makeText(MainActivity.this, "Invalid email
Id or password", Toast.LENGTH_SHORT).show();
}
}
});
}
}
}
整个错误是:
FATAL EXCEPTION: main
Process: e_homes.app.com.e_homes, PID: 7302
java.lang.RuntimeException: Unable to start activity ComponentInfo{e_homes.app.com.e_homes/e_homes.app.com.e_homes.Location}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at e_homes.app.com.e_homes.Location.onCreate(Location.java:31)
at android.app.Activity.performCreate(Activity.java:5264)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
1条答案
按热度按时间u91tlkcl1#
正如我从你的日志中看到的,这个错误实际上并不是来自你的
MainActivity
是你的Location
活动。您需要将下面的代码添加到
AndroidManifest.xml
文件。试着解开
this
代替getApplicationContext()
.希望有帮助。