【发布时间】:2016-12-07 12:58:12
【问题描述】:
我刚刚将 firebase 数据库添加到我的应用程序中,当调用活动时(用户登录后)我收到致命错误
ProfileActivity.java
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.firebase.client.Firebase;
import com.google.firebase.FirebaseApp;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class ProfileActivity extends AppCompatActivity implements View.OnClickListener {
//firebase auth object
private FirebaseAuth firebaseAuth;
private Firebase firebase;
//view objects
private TextView textViewUserEmail;
private TextView emailText;
private Button buttonLogout,button2;
private EditText nameEdit, ageEdit;
private DatabaseReference mRootRef;
private Firebase myFirebaseRef = new Firebase("https://********************.com");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Previous versions of Firebase
firebase.setAndroidContext(this);
//Newer version of Firebase
if(!FirebaseApp.getApps(this).isEmpty()) {
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
}
// remove title
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_profile);
//initializing firebase authentication object
firebaseAuth = FirebaseAuth.getInstance();
//if the user is not logged in
//that means current user will return null
if(firebaseAuth.getCurrentUser() == null){
//closing this activity
finish();
//starting login activity
startActivity(new Intent(this, LoginActivity.class));
}
//getting current user
FirebaseUser user = firebaseAuth.getCurrentUser();
//initializing views
textViewUserEmail = (TextView) findViewById(R.id.textViewProfile);
emailText = (TextView) findViewById(R.id.emailEditText);
nameEdit = (EditText) findViewById(R.id.entername);
ageEdit = (EditText) findViewById(R.id.ageedit);
buttonLogout = (Button) findViewById(R.id.buttonLogout);
button2 = (Button) findViewById(R.id.button2);
mRootRef = FirebaseDatabase.getInstance().getReference();
//displaying logged in user name
textViewUserEmail.setText("Welcome To Your Profile");
emailText.setText(user.getEmail());
//adding listener to button
buttonLogout.setOnClickListener(this);
button2.setOnClickListener(this);
}
@Override
public void onClick(View view) {
//if logout is pressed
if(view == buttonLogout){
//logging out the user
firebaseAuth.signOut();
//closing activity
finish();
//starting login activity
startActivity(new Intent(this, LoginActivity.class));
}
if (view == button2) {
myFirebaseRef.setValue(nameEdit.getText().toString());
myFirebaseRef.setValue(ageEdit.getText().toString());
finish();
startActivity(new Intent(this, Main2Activity.class));
}
}
}
抛出的异常是:
12-07 12:42:41.855 26718-26718/com.vb.myapplication E/AndroidRuntime: 致命例外:主要 进程:com.vb.myapplication,PID:26718 java.lang.RuntimeException:无法实例化活动 组件信息{com.vb.myapplication/com.vb.food4thought.ProfileActivity}: java.lang.RuntimeException:您需要使用设置 Android 上下文 使用 Firebase 之前的 Firebase.setAndroidContext()。 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3094) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350) 在 android.app.ActivityThread.access$1100(ActivityThread.java:222) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795) 在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper.java:158) 在 android.app.ActivityThread.main(ActivityThread.java:7229) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 引起:java.lang.RuntimeException:你需要设置Android 在使用 Firebase 之前使用 Firebase.setAndroidContext() 的上下文。 在 com.firebase.client.core.Context.getPlatform(Context.java:45) 在 com.firebase.client.core.Context.ensureLogger(Context.java:218) 在 com.firebase.client.core.Context.initServices(Context.java:105) 在 com.firebase.client.core.Context.freeze(Context.java:92) 在 com.firebase.client.core.RepoManager.getLocalRepo(RepoManager.java:55) 在 com.firebase.client.core.RepoManager.getRepo(RepoManager.java:19) 在 com.firebase.client.Firebase.(Firebase.java:172) 在 com.firebase.client.Firebase.(Firebase.java:177) 在 com.firebase.client.Firebase.(Firebase.java:155) 在 com.vb.food4thought.ProfileActivity.(ProfileActivity.java:32) 在 java.lang.Class.newInstance(本机方法) 在 android.app.Instrumentation.newActivity(Instrumentation.java:1095) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3084) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350) 在 android.app.ActivityThread.access$1100(ActivityThread.java:222) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795) 在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper.java:158) 在 android.app.ActivityThread.main(ActivityThread.java:7229) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
我不知道我做错了什么。非常感谢任何帮助。 谢谢。
【问题讨论】:
-
您似乎将 Firebase 2.x 与较新版本的 Firebase SDK 混合使用。我强烈建议只使用新版本并遵循此处的文档:firebase.google.com/docs/android/setup
-
我的代码的哪一部分暗示了这一点?抱歉,android 开发新手,从未使用过 firebase
-
很多文章建议,第一个是
import com.firebase.client.Firebase; import com.google.firebase.FirebaseApp;第一行是2.x SDK,下一个是9或更高版本。如前所述:使用我链接的文档,将其他代码扔掉。 -
感谢您指出这一点,这就是谷歌搜索教程的问题,哈哈。你不知道哪个是“新”
-
如有疑问,请从创建该技术的人员的文档开始:firebase.google.com/docs/android/setup。还有一个方便的代码实验室:codelabs.developers.google.com/codelabs/firebase-android
标签: android firebase firebase-realtime-database