我可能会纠缠于此,但似乎看不出任何不起作用的原因。
在MenuActivity中,菜单项及其功能正常运行。注销非常完美。当我将MenuActivity扩展到ProfileActivity时,出于某种原因注销按钮无法正常工作,该应用程序崩溃并显示消息“我的应用程序已停止x关闭该应用程序”。
这里是所有课程:
MenuActivity.java:
package com.example.myapplication;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import com.google.firebase.auth.FirebaseAuth;
public class MenuActivity extends AppCompatActivity {
private FirebaseAuth firebaseAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
firebaseAuth = FirebaseAuth.getInstance();
}
private void Logout(){
firebaseAuth.signOut();
finish();
startActivity(new Intent(MenuActivity.this, MainActivity.class));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflate = getMenuInflater();
inflate.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.logoutMenu:{
Logout();
return true;
}
case R.id.profileMenu:
startActivity(new Intent(MenuActivity.this, ProfileActivity.class));
}
return super.onOptionsItemSelected(item);
}
}
ProfileActivity.java
package com.example.myapplication;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
public class ProfileActivity extends MenuActivity {
private TextView userName, userEmail, phoneNumber;
private Button editProfile, btnChangePassword;
private FirebaseAuth firebaseAuth;
private FirebaseDatabase firebaseDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
userName = findViewById(R.id.tvName3);
userEmail = findViewById(R.id.tvEmail3);
phoneNumber = findViewById(R.id.tvPhone3);
editProfile = findViewById(R.id.btnEditProfile);
btnChangePassword = findViewById(R.id.btnChangePassword);
firebaseAuth = FirebaseAuth.getInstance();
firebaseDatabase = FirebaseDatabase.getInstance();
final DatabaseReference databaseReference = firebaseDatabase.getReference(firebaseAuth.getUid());
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
UserProfile userProfile = dataSnapshot.getValue(UserProfile.class);
userName.setText("Name: " + userProfile.getUserName());
userEmail.setText("Email: " + userProfile.getUserEmail());
phoneNumber.setText("Phone: " + userProfile.getUserPhone());
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(ProfileActivity.this, databaseError.getCode(), Toast.LENGTH_SHORT);
}
});
editProfile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(ProfileActivity.this, UpdateProfile.class));
}
});
btnChangePassword.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(ProfileActivity.this, ChangePassword.class));
}
});
}
}
和menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
android:id="@+id/main_menu" >
<item
android:id="@+id/logoutMenu"
android:orderInCategory="100"
app:showAsAction="always"
android:title="Logout">
</item>
<item
android:id="@+id/profileMenu"
android:orderInCategory="200"
app:showAsAction="never"
android:title="Profile">
</item>
<item
android:id="@+id/refreshMenu"
android:orderInCategory="300"
app:showAsAction="never"
android:title="Refresh">
</item>
</menu>
这是logcat消息
2019-05-31 09:15:56.742 20660-20660/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 20660
android.content.res.Resources$NotFoundException: String resource ID #0xfffffffd
at android.content.res.Resources.getText(Resources.java:338)
at android.widget.Toast.makeText(Toast.java:304)
at com.example.myapplication.ProfileActivity$1.onCancelled(ProfileActivity.java:58)
at com.google.firebase.database.core.ValueEventRegistration.fireCancelEvent(com.google.firebase:firebase-database@@17.0.0:80)
at com.google.firebase.database.core.view.CancelEvent.fire(com.google.firebase:firebase-database@@17.0.0:40)
at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@17.0.0:55)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:169)
at android.app.ActivityThread.main(ActivityThread.java:6585)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
在ProfileActivity中我做错了什么事来干扰菜单?
答案 0 :(得分:0)
private void Logout(){
firebaseAuth.signOut();
startActivity(new Intent(MenuActivity.this, MainActivity.class));
}
尝试此注销功能
答案 1 :(得分:0)
问题可能出在告诉MenuActivity按下注销时该怎么做的行中。
private void Logout(){
firebaseAuth.signOut();
finish();
startActivity(new Intent(MenuActivity.this, MainActivity.class));
}
告诉我,如果我理解得很清楚,崩溃仅在您处于ProfileActivity时才会发生?因为在这里您要启动一个从MenuActivity传递到MainActivity的Intent,所以如果您在ProfileActivity中,它可能会导致问题...
答案 2 :(得分:0)
据我说,我认为您应该添加一个检查,是否有当前用户已经登录,如果该用户未登录,则会抛出空指针异常。 像这样添加支票
if(FirebaseAuth.getCurrentUser()!=null){
//Your sign out code
}