我正面临这个问题,并在这个网站上看到了一些答案,但没有得到任何适当的解决方案.
我已经使用以前的版本中Firebase
,工作正常,但是当我尝试使用升级升级换代和更新Firebase
类DatabaseReference
它显示错误,无法正常工作.
我将整个代码添加到我的清单文件中,所以请帮我解决这个问题.
这是我的manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="firebasechat.com.firebasechat">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:name=".Activity.SimpleBlog"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Activity.RegisterActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Run Code Online (Sandbox Code Playgroud)
我Module app
在下面给出.
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion "26.0.0"
defaultConfig {
applicationId "firebasechat.com.firebasechat"
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
packagingOptions {
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE-FIREBASE.txt'
exclude 'META-INF/NOTICE'
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:26.+'
compile 'com.android.volley:volley:1.0.0'
compile "com.google.firebase:firebase-database:11.0.0"
compile 'com.google.android.gms:play-services:11.0.0'
compile 'com.android.support:recyclerview-v7:25.0.0'
testCompile 'junit:junit:4.12'
testCompile 'junit:junit:4.12'
}
Run Code Online (Sandbox Code Playgroud)
和 Project gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.google.gms:google-services:4.2.0'// Updated version of google service
}
}
allprojects {
repositories {
jcenter()
maven {
url "https://maven.google.com" // Google's Maven repository
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Run Code Online (Sandbox Code Playgroud)
以下是我的Activity
.
public class RegisterActivity extends AppCompatActivity {
EditText username, password;
Button registerButton;
String user, pass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
username = (EditText)findViewById(R.id.username);
password = (EditText)findViewById(R.id.password);
registerButton = (Button)findViewById(R.id.registerButton);
FirebaseApp.initializeApp(this);
registerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
user = username.getText().toString();
pass = password.getText().toString();
final ProgressDialog pd = new ProgressDialog(RegisterActivity.this);
pd.setMessage("Loading...");
pd.show();
String url = "https://pure-coda-174710.firebaseio.com/users.json";
StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>(){
@Override
public void onResponse(String s) {
// Firebase reference = new Firebase("https://pure-coda-174710.firebaseio.com/users");
DatabaseReference reference = FirebaseDatabase.getInstance()
.getReferenceFromUrl("https://pure-coda-174710.firebaseio.com/users");
if(s.equals("null")) {
reference.child(user).child("password").setValue(pass);
Toast.makeText(RegisterActivity.this, "registration successful", Toast.LENGTH_LONG).show();
}
else {
try {
JSONObject obj = new JSONObject(s);
if (!obj.has(user)) {
reference.child(user).child("password").setValue(pass);
Toast.makeText(RegisterActivity.this, "registration successful", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(RegisterActivity.this, "username already exists", Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
pd.dismiss();
}
},new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError volleyError) {
System.out.println("" + volleyError );
pd.dismiss();
}
});
RequestQueue rQueue = Volley.newRequestQueue(RegisterActivity.this);
rQueue.add(request);
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
use*_*040 42
从字面上看,整整一天.最后,这是解决方案.
吓到0而不是1,你不必使用初始化firebaseapp或类似的东西.
在Project gradle中,使用Google服务:4.0.0而不是4.1.0.
另外,根据我的经验,最后应用插件声明也是没有必要的.
(希望你从tools => firebase助手添加了firebase数据库.步骤1和步骤2是正确的,绿色.)
Flo*_*lin 41
在您的SimpleBlog
应用程序类中,在onCreate()
方法中初始化FirebaseApp 并将其删除RegisterActivity
,以便将Firebase初始化为整个应用程序,而不仅仅是一个Activity.
@Override
public void onCreate() {
super.onCreate();
FirebaseApp.initializeApp(this);
}
Run Code Online (Sandbox Code Playgroud)
同时apply plugin: 'com.google.gms.google-services'
在app gradle结束时添加:
dependencies {
....
}
apply plugin: 'com.google.gms.google-services'
Run Code Online (Sandbox Code Playgroud)
pw2*_*pw2 15
遇到同样的问题并解决了这个问题:
在活动中:
@Override
public void onCreate() {
super.onCreate();
FirebaseApp.initializeApp(this);
}
Run Code Online (Sandbox Code Playgroud)
在应用程序的gradle中(在文件末尾):
dependencies {
....
}
apply plugin: 'com.google.gms.google-services'
Run Code Online (Sandbox Code Playgroud)
是项目的平台:
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath 'com.google.gms:google-services:3.0.0'
}
Run Code Online (Sandbox Code Playgroud)
Rob*_*ust 12
根据FirebaseApp文档,除了您的应用需要访问另一个Firebase项目之外,您无需调用此初始化.
我调用了这个初始化,有时用户会在用户打开我的应用程序时遇到崩溃,所以我删除了这行代码然后一切正常.小心调用此初始化.
捕获FirebaseApp文档:
更新:如果您尝试添加此行,则会发生异常[某些广告网络需要此行,他们还会在其中添加一些流程AndroidManifest
]
无法获得对Firestore客户端的脱机持久性的独占锁定.这通常意味着您在应用程序中使用来自多个进程的Firestore.请记住,多进程Android应用程序在所有进程中执行Application类中的代码,因此您可能需要避免在Application类中初始化Firestore.如果您有意在多个进程中使用Firestore,则只能在其中一个进程中启用脱机持久性(即调用setPersistenceEnabled(true)).
要解决这个问题:
1)在你的Application
课程中添加以下方法:
private boolean isMainProcess(Context context) {
if (null == context) {
return true;
}
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
int pid = android.os.Process.myPid();
for (ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) {
String name = processInfo.processName;
if (!StringUtils.isEmpty(name) && pid == processInfo.pid && name.equals(APPLICATION_ID)) {
return true;
}
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
2)包裹onCreate
你的Application
@Override
public void onCreate() {
super.onCreate();
if (!isMainProcess(this)) {
FirebaseApp.initializeApp(this);
FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder()
.setPersistenceEnabled(false)
.build();
FirebaseFirestore.getInstance().setFirestoreSettings(settings);
// other things
return;
}
// other things
}
Run Code Online (Sandbox Code Playgroud)
更新:有时,我的应用程序抛出异常
无法创建应用程序我的应用程序类:java.lang.IllegalStateException:默认FirebaseApp未在此过程MY_APPLICATION_ID中初始化.确保首先调用FirebaseApp.initializeApp(Context).
修复它:让更新onCreate
方法:
@Override
public void onCreate() {
super.onCreate();
FirebaseApp.initializeApp(this);
boolean isMain = isMainProcess(this);
FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder().setPersistenceEnabled(isMain).build();
FirebaseFirestore.getInstance().setFirestoreSettings(settings);
if (!isMain) {
// other things
return;
}
// other things
}
Run Code Online (Sandbox Code Playgroud)
小智 10
我前段时间遇到过同样的问题.
您尝试在不初始化的情况下获取Firebase的实例.在尝试获取Firebase实例之前,请添加以下代码行:
只需导入最新版本的firebase和google play服务.
最新更新版本(示例):
添加到这些行build.gradle
dependencies {
implementation 'com.google.firebase:firebase-messaging:17.3.4'
implementation 'com.google.firebase:firebase-core:16.0.7'
implementation 'com.firebase:firebase-jobdispatcher:0.8.5'
}
apply plugin: 'com.google.gms.google-services'
dependencies {
classpath 'com.android.tools.build:gradle:3.3.1'
classpath 'com.google.gms:google-services:4.2.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
Run Code Online (Sandbox Code Playgroud)
} apply plugin:'com.google.gms.google-services'
dependencies {
implementation 'com.google.firebase:firebase-messaging:17.3.4'
implementation 'com.google.firebase:firebase-core:16.0.7'
implementation 'com.firebase:firebase-jobdispatcher:0.8.5'
}
apply plugin: 'com.google.gms.google-services'
dependencies {
classpath 'com.android.tools.build:gradle:3.3.1'
classpath 'com.google.gms:google-services:4.2.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
Run Code Online (Sandbox Code Playgroud)
就这样.享受您的编码.
首先第一件事:项目配置 JSON 可能会出错/过时,主要是在 Firebase 升级时。
转到Tools->Firebase->Cloud Messaging->Set up firebase cloud messages。
即使您过去这样做过,也值得再次检查这里因为事情会不时更新。我同步了我的项目,它解决了这个问题。
至于调用FirebaseApp.initializeApp
,没必要,至少FCM没必要。请参阅FCM 文档。
此外,如果它返回 null 则意味着它失败了,所以在调用之前调用它是getInstance
行不通的。
请注意,这是 FirebaseInstanceId 的 getInstance:
public static FirebaseInstanceId getInstance() {
return getInstance(FirebaseApp.getInstance());
}
Run Code Online (Sandbox Code Playgroud)
另一方面:
@Nullable
public static FirebaseApp initializeApp(Context var0) {
Object var1 = sLock;
synchronized(sLock) {
if (zzf.containsKey("[DEFAULT]")) {
return getInstance();
} else {
FirebaseOptions var2;
return (var2 = FirebaseOptions.fromResource(var0)) == null ? null : initializeApp(var0, var2);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这意味着如果initializeApp
返回null
,您可能会遇到配置问题。至少 - 值得检查。要知道您所处的位置,请在Context
可见的某个点放置一个断点,并尝试评估FirebaseApp.initialize(context)
以确保它不会返回 null。
请将项目中的 google 服务插件从 4.1.0 版本降级到 4.0.1 build.gradle
classpath 'com.google.gms:google-services:4.0.1'
Run Code Online (Sandbox Code Playgroud)
并将此代码添加到 onCreate()
FirebaseApp.initializeApp(this);
它对我有用。
归档时间: |
|
查看次数: |
61789 次 |
最近记录: |