从firebase数据库中的textview保存数据

时间:2017-09-08 15:43:09

标签: java android firebase firebase-realtime-database

我正在尝试在firebase数据库中保存textview的内容,但是当我按下必须执行此操作的按钮时,它不会执行操作,但应用程序不会停止或任何事情。

这是我的代码

XML

    <TextView
    android:id="@+id/emailTextView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/nameTextView"
    android:layout_marginTop="16dp"
    android:text="@string/app_name"
    android:textAlignment="center"
    android:textSize="16sp" />

爪哇

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

    // Mostramos el webview

    WebView myWebView = (WebView) findViewById(R.id.terminos);
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    myWebView.setWebViewClient(new WebViewClient());
    myWebView.loadUrl("http://zxxxxxxxxx.saq.es");


    // Boton pulsado

    // Insertamos datos en la base de datos


    userEmail = (TextView) findViewById(emailTextView);
    btacepto = (Button) findViewById(R.id.cambiarhome);

    btacepto.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Firebase ref = new Firebase("https://payads-2c114.firebaseio.com/users");
            String enteredUserEmail = userEmail.getText().toString();
            ref.child("users").setValue(enteredUserEmail);
        }
    });
}

}

错误

09-08 16:35:19.790 5341-5341/albertonio.payads E/AndroidRuntime: FATAL EXCEPTION: main
                                                             Process: albertonio.payads, PID: 5341
                                                             java.lang.RuntimeException: You need to set the Android context using Firebase.setAndroidContext() before using Firebase.
                                                                 at com.firebase.client.core.Context.getPlatform(Context.java:45)
                                                                 at com.firebase.client.core.Context.ensureLogger(Context.java:218)
                                                                 at com.firebase.client.core.Context.initServices(Context.java:105)
                                                                 at com.firebase.client.core.Context.freeze(Context.java:92)
                                                                 at com.firebase.client.core.RepoManager.getLocalRepo(RepoManager.java:55)
                                                                 at com.firebase.client.core.RepoManager.getRepo(RepoManager.java:19)
                                                                 at com.firebase.client.Firebase.<init>(Firebase.java:172)
                                                                 at com.firebase.client.Firebase.<init>(Firebase.java:177)
                                                                 at com.firebase.client.Firebase.<init>(Firebase.java:155)
                                                                 at albertonio.payads.TerminosActivity$1.onClick(TerminosActivity.java:48)
                                                                 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)

第48行:Firebase ref = new Firebase("https://payads-2c114.firebaseio.com");

当代码在metod Onclick

中时,我在Android Monitor中播放此错误

我改变了

          Firebase ref = new Firebase("https://payads-2c114.firebaseio.com/users");
        String enteredUserEmail = userEmail.getText().toString();
        ref.child("users").setValue(enteredUserEmail);

DatabaseReference databaseRef = FirebaseDatabase.getInstance().getReference(); databaseRef.child("users").setValue(enteredUserEmail);

现在当我按下按钮时节点用户的所有日期都被删除但是你没有写出我想要的东西

2 个答案:

答案 0 :(得分:0)

我建议改用FirebaseDatabaseReference

DatabaseReference databaseRef = FirebaseDatabase.getInstance().getReference();

databaseRef.child("users").setValue(enteredUserEmail);

这样你可以绕过硬编码的东西,如果firebase设置正确,它应该可以正常工作

还要记住这些在你的gradle中(模块:应用程序)

compile 'com.google.firebase:firebase-core:11.0.4'
compile 'com.google.firebase:firebase-database:11.0.4'

看起来你的btnClickListener出现了什么问题?

btacepto.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Firebase ref = new Firebase("https://payads-2c114.firebaseio.com/users");
        String enteredUserEmail = userEmail.getText().toString();
        ref.child("users").setValue(enteredUserEmail);
    }
});

将其移至onCreate()。

删除周围的public void btn(View view) {}

尝试添加Firebase.setAndroidContext(this); onCreate()

答案 1 :(得分:0)

正如我们在Firebase Quickstart Documentation中看到的那样:

  

必须使用Android上下文初始化Firebase库一次。这必须在创建或使用任何Firebase参考之前发生。

您的活动类应如下所示:

Try Using this

<div class = "row">
  <div class = "col-sm-6">
     <div class = "col-sm-12">
        Your Content 
     </div>
  </div>
  <div class = "col-sm-6">
     <div class = "col-sm-12">
        Your Content 
     </div>
  </div>
</div>