我可以在Android Studio中的片段上使用Firebase吗?

时间:2020-06-18 14:50:53

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

我正在尝试为我的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

1 个答案:

答案 0 :(得分:0)

您应该执行下一个

  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(){ 覆盖fun onCreate(){ super.onCreate() setupFireBase() } }

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

    enter image description here

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