【问题标题】:Can I use Firebase on a fragment in android studio?我可以在 android studio 的片段上使用 Firebase 吗?
【发布时间】:2020-10-08 16:16:15
【问题描述】:

我正在尝试为我的 android 应用程序制作一个联系表。联系信息应该存储在我的 Firebase 中 联系表格存在于一个片段中(我不确定这是否是问题所在)。 我使用的代码:

public class ContactFragment extends Fragment {

    private Firebase Ref;
    private EditText name,lastname,phone,email;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_contact, container, false);// Inflate the layout for this fragment


        name = (EditText) view.findViewById(R.id.name);
        lastname = (EditText) view.findViewById(R.id.lastname);
        phone = (EditText) view.findViewById(R.id.phone);
        email = (EditText) view.findViewById(R.id.email);
        Firebase.setAndroidContext(this);
        Ref = new Firebase("my-url-goes-here");





        return view;
    }


    public void save(View view) {
        String nameinput = name.getText().toString();
        String lastnameinput = lastname.getText().toString();
        String phoneinput = phone.getText().toString();
        String emailinput = email.getText().toString();
        Firebase Rname = Ref.child("name");
        Rname.seValue(nameinput);
        Firebase Rlastname = Ref.child("lastname");
        Rlastname.setValue(lastnameinput);
        Firebase Rphone = Ref.child("phone");
        Rphone.setValue(phoneinput);
        Firebase Remail = Ref.child("email");
        Remail.setValue("email");





    }



}

除了FirebasesetValue() 是红色的之外,一切似乎都很好。 当我尝试构建应用程序时,我收到此错误消息

error: cannot find symbol
    private Firebase Ref;
            ^
  symbol:   class Firebase
  location: class ContactFragment

【问题讨论】:

  • 是的,您可以在片段/活动中执行此操作。您收到的错误信息是什么?在问题本身中添加错误堆栈跟踪。
  • 很抱歉我忘记了我在其中编辑的错误消息
  • 如果应用程序崩溃,会有一个堆栈跟踪。请在 logcat 上查找,并将其添加到您的问题中。请回复@AlexMamo
  • @AlexMamo 它没有显示在日志跟踪中,因为由于上述错误,应用程序甚至无法开始构建
  • 您似乎根本没有正确使用 Firebase 实时数据库 API。您可能使用的是非常旧版本的 Firebase SDK。我建议重新开始并使用文档来添加 SDK。 firebase.google.com/docs/database/android/start

标签: android firebase android-fragments firebase-realtime-database


【解决方案1】:

你应该做下一个

  1. 在您的应用程序活动中应该在您的应用程序开始之前调用实例

    包 com.prueba.analytics

    导入 android.app.Application 导入 com.google.firebase.FirebaseApp 导入 com.google.firebase.analytics.FirebaseAnalytics

    有趣的 Application.setupFireBase() { fireBaseDatabase = FirebaseApp.initializeApp(this)!! firebaseTracker = FirebaseAnalytics.getInstance(this) }

    包 com.prueba

    导入 android.app.Application 导入 com.prueba.analytics.setupFireBase

    类 PruebaApplication : Application() { 覆盖有趣的 onCreate() { 超级.onCreate() setupFireBase() } }

  2. 您应该拥有 google-services.json 和 google-services 的库

    类路径'com.google.gms:google-services:4.0.1'

【讨论】:

  • 您好,感谢您的快速回复!我是否将您的代码放在我的片段的 java 文件中?
猜你喜欢
  • 1970-01-01
  • 2018-10-10
  • 1970-01-01
  • 2020-11-29
  • 1970-01-01
  • 2020-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多