2017-05-01 34 views
0

我在对话框片段中使用了firebase数据库。当下面的代码已运行FirebaseDatabase.getInstance()崩溃

private FirebaseDatabase firebaseDatabase=FirebaseDatabase.getInstance();

我找遍了所有的解决方案,如的gradle变化的依赖性和下载谷歌,service.json文件

等它越来越崩溃,但问题仍未解决。我附上我的代码的所有细节,请给出正确的解决方案。由于

public class FireViewFragment extends DialogFragment { 

TextView name, email, username; 
Button ok; 
MyDialogInterface myDialogInterface; 
private FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance(); 

public FireViewFragment(MyDialogInterface myDialogInterface) { 
    this.myDialogInterface = myDialogInterface; 
    Log.d("Interface", "Constractor"); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View view = inflater.inflate(R.layout.fragment_fire_view, container, false); 
    name = (TextView) view.findViewById(R.id.dis_name); 
    email = (TextView) view.findViewById(R.id.dis_email); 
    username = (TextView) view.findViewById(R.id.dis_uname); 
    ok = (Button) view.findViewById(R.id.ok); 
    ok.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      myDialogInterface.setMyDialogInterface("hi", false); 

     } 
    }); 
    return view; 
}} 

的build.gradle(Module.app)

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.crashlytics.sdk.android:crashlytics:[email protected]') { 
    transitive = true; 
} 
compile 'com.android.support:appcompat-v7:25.3.1' 
compile 'com.android.support.constraint:constraint-layout:1.0.2' 
compile 'com.android.support:recyclerview-v7:25.3.1' 
compile 'com.google.android.gms:play-services-maps:10.2.1' 
compile 'com.android.support:support-v4:25.3.1' 
compile 'com.google.firebase:firebase-database:9.2.0' 
testCompile 'junit:junit:4.12' 

}应用插件: 'com.google.gms.google服务'

google- service.json文件

"project_info": { 
"project_number": "961599319300", 
"firebase_url": "https://working-2ab0d.firebaseio.com", 
"project_id": "working-2ab0d", 
"storage_bucket": "working-2ab0d.appspot.com" }, 

logcat的

05-01 21:56:30.298 29506-29506/com.example.gowsik.working E/AndroidRuntime: FATAL EXCEPTION: main 
                     Process: com.example.gowsik.working, PID: 29506 
                     java.lang.NoSuchMethodError: No virtual method zzckb()Z in class Lcom/google/firebase/FirebaseApp; or its super classes (declaration of 'com.google.firebase.FirebaseApp' appears in /data/app/com.example.gowsik.working-1/split_lib_dependencies_apk.apk) 
                      at com.google.firebase.database.FirebaseDatabase.getInstance(Unknown Source) 
                      at com.google.firebase.database.FirebaseDatabase.getInstance(Unknown Source) 
                      at com.example.gowsik.working.firebase.FireViewFragment.<init>(FireViewFragment.java:22) 
                      at com.example.gowsik.working.activities.FirebaseActivity$2.onClick(FirebaseActivity.java:49) 
                      at android.view.View.performClick(View.java:4780) 
                      at android.view.View$PerformClick.run(View.java:19866) 
                      at android.os.Handler.handleCallback(Handler.java:739) 
                      at android.os.Handler.dispatchMessage(Handler.java:95) 
                      at android.os.Looper.loop(Looper.java:135) 
                      at android.app.ActivityThread.main(ActivityThread.java:5254) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at java.lang.reflect.Method.invoke(Method.java:372) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
+0

我猜猜它与不同版本的Firebase和地图有关。尝试使用'compile'c​​om.google.android.gms:play-services-maps:9.2.0',与firebase相同的版本是... – Opiatefuchs

+0

@Opiatefuchs您给出了很好的解决方案。没有这个,我已经改变了gradle依赖关系中的所有更改。 Yhaaa。,它的工作谢谢 –

+0

很高兴帮助... – Opiatefuchs

回答

0

不同版本的Google图书馆不能一起工作。您需要添加的版本相同,所以更改您的地图库9.2.0:

compile 'com.google.android.gms:play-services-maps:9.2.0' 
compile 'com.google.firebase:firebase-database:9.2.0' 
0

,而不是写

private FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance(); 

private DatabaseReference firebaseDatabase = FirebaseDatabase.getInstance(); 

希望这将解决您的问题

+0

我现在尝试这个代码显示吹警告消息不兼容的类型。 必需: com.google.firebase.database.DatabaseReference 实测值: com.google.firebase.database.FirebaseDatabase –

+0

私人DatabaseReference firebaseDatabase = FirebaseDatabase.getInstance()getReference();试试这个 –

0

问题解决了刚才我所用和谷歌播放服务的同一版本的火力依赖

compile 'com.google.android.gms:play-services-maps:9.2.0' 
compile 'com.google.firebase:firebase-database:9.2.0'