【问题标题】:DatabaseReference and FirebaseDatabase - cannot resolve symbolDatabaseReference 和 FirebaseDatabase - 无法解析符号
【发布时间】:2017-01-16 14:19:28
【问题描述】:

我目前正在开发 Android 上的机构级聊天消息应用程序。我使用 Firebase 作为我的云服务器。目前,我在MainActivity 的以下行中的DatabaseReferenceFirebaseDatabase 面临Cannot Resolve Symbol error

private DatabaseReference root = FirebaseDatabase.getInstance().getReference().getRoot();

我想使用 Firebase 创建聊天室。我知道如何做到这一点,以及如何链接我的 Firebase 数据库的键和值。我还知道如何通过 Firebase Cloud 从 Android 设备更新键和值。

但我无法解析Cannot resolve Symbol error。 以下是我的MainActivity 的代码。我仍处于制作此应用的初始阶段。

    package com.ranatalha.realtimechat;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private Button buttonAddRoom;
    private EditText editTextAddaChatRoom;
    private ListView listViewChatRooms;

    private ArrayAdapter<String> arrayAdapter;
    private ArrayList<String> list_of_rooms = new ArrayList<>();

    private String Entered_Username;


    private DatabaseReference root = FirebaseDatabase.getInstance().getReference().getRoot();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        buttonAddRoom = (Button) findViewById(R.id.buttonAddRoom);
        editTextAddaChatRoom = (EditText) findViewById(R.id.editTextAddaChatRoom);
        listViewChatRooms = (ListView) findViewById(R.id.listViewChatRooms);

//************Creation of an array list to store the list of active chat rooms************
        arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, list_of_rooms);

        listViewChatRooms.setAdapter(arrayAdapter);
//*****************************************************************************************
        request_user_name();

        buttonAddRoom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
    }
    private void request_user_name(){

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Enter Your Name:");

        final EditText inputUsername = new EditText(this);
        builder.setView(inputUsername);

        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Entered_Username = inputUsername.getText().toString();

            }
        });

        builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.cancel();
                request_user_name();
            }
        });

        builder.show();

    }
}

【问题讨论】:

  • 向我们展示您的应用级 build.gradle
  • 对于this guide,特别注意adding the SDK
  • @Arel 提出了一个答案,它就像一个魅力。我在 build.gradle (Module:app) 文件中添加了另外 2 个依赖项 compile 'com.google.firebase:firebase-core:9.4.0' and compile 'com.google.firebase:firebase-database:9.4.0' ... 非常感谢您对我的问题 @qbix 和 @AbAppletic 的回复和关注 :)

标签: android firebase firebase-realtime-database


【解决方案1】:

能否请您仔细检查您的build.gradle (Module:app) 文件中是否有这些依赖项?

implementation 'com.google.firebase:firebase-analytics:17.2.1'    
implementation 'com.google.firebase:firebase-database:19.2.0'

添加后错误消失。

【讨论】:

  • 这行得通。非常感谢@Arel 帮助我解决这个问题。真的很感谢你:)
  • 没问题。快乐编码!
  • 这确实解决了它。我在 firebase.google.com walkthru 设置指南中没有看到这一点。我猜它缺少了?
  • 嗨@daylight 如果你还没有看过文档,你可以在这里阅读它firebase.google.com/docs/android/setup#manually_add_firebase
  • Firebase 应该聘请您来回答这个问题。救命稻草!!!
【解决方案2】:
implementation 'com.google.firebase:firebase-core:11.0.4'
implementation 'com.google.firebase:firebase-database:11.0.4'

【讨论】:

    【解决方案3】:

    您需要将这些依赖项添加到您的

    1) build.gradle (Module:app) 文件。

    implementation 'com.google.firebase:firebase-database:11.6.2'
    

    2) build.gradle(顶层构建)

    classpath 'com.google.gms:google-services:3.1.0'
    

    3) 在 Android Studio 的 app 模块中添加 google-services.json

    【讨论】:

      猜你喜欢
      • 2021-02-04
      • 2018-11-11
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 2019-02-14
      • 2019-01-14
      • 2020-06-17
      • 1970-01-01
      相关资源
      最近更新 更多