无法启动活动ComponentInfo {e_homes.app.com.e_homes / e_homes.app.com.e_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();
}
}
});
}
}
}
WHole错误是:
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)
答案 0 :(得分:0)
正如我从您的logcat中看到的那样,错误实际上并非来自您MainActivity
活动的Location
。
您需要将以下代码添加到AndroidManifest.xml
文件中。
<activity android:name=".start" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MainActivity" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name= ".Location">
</activity>
尝试取消this
而不是getApplicationContext()
。
希望它有所帮助。