我正在尝试在我的项目中包含Firebase实时数据库。 我想将我在编辑文本中写的任何内容都保存在Firebase数据库中 我在项目中使用了2个依赖项
implementation 'com.google.firebase:firebase-database:19.2.0'
implementation 'com.google.firebase:firebase-storage:19.1.0'
这是我的主要课程
public class MainActivity extends AppCompatActivity implements
ValueEventListener {
public EditText editText;
public Button button;
// Write a message to the database
private FirebaseDatabase database = FirebaseDatabase.getInstance();
private DatabaseReference myRef = database.getReference();
private DatabaseReference mName = myRef.child("First");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.name);
button = findViewById(R.id.button);
}
public void button_click (View view){
String name1 = editText.getText().toString();
mName.setValue(name1);
}
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
@Override
protected void onStart() {
super.onStart();
mName.addValueEventListener(this);
}
}
这是我的xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="171dp"
android:layout_marginTop="45dp"
android:layout_marginEnd="152dp"
android:layout_marginBottom="263dp"
android:onClick="button_click"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/name" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="148dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="176dp"
android:text="TextView"
app:layout_constraintBottom_toTopOf="@+id/name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
这是我的清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.firebasedatabase">
<uses-permission android:name="android.permission.INTERNET"></uses-
permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这是logcat
2019-11-02 11:53:10.224 10194-10194/? I/irebasedatabas: Late-enabling
- Xcheck:jni
2019-11-02 11:53:10.660 10194-10194/com.example.firebasedatabase
I/Perf:
Connecting to perf service.
2019-11-02 11:53:10.703 10194-10194/com.example.firebasedatabase
I/FirebaseInitProvider: FirebaseApp initialization successful
2019-11-02 11:53:10.835 10194-10231/com.example.firebasedatabase
D/NetworkSecurityConfig: No Network Security Config specified, using
platform default
2019-11-02 11:53:10.838 10194-10194/com.example.firebasedatabase
W/irebasedatabas: Accessing hidden method
Landroid/graphics/drawable/Drawable;-
>getOpticalInsets()Landroid/graphics/Insets; (light greylist, linking)
2019-11-02 11:53:10.838 10194-10194/com.example.firebasedatabase
W/irebasedatabas: Accessing hidden field Landroid/graphics/Insets;-
>left:I (light greylist, linking)
2019-11-02 11:53:10.838 10194-10194/com.example.firebasedatabase
W/irebasedatabas: Accessing hidden field Landroid/graphics/Insets;-
>right:I (light greylist, linking)
2019-11-02 11:53:10.838 10194-10194/com.example.firebasedatabase
W/irebasedatabas: Accessing hidden field Landroid/graphics/Insets;->top:I
(light greylist, linking)
2019-11-02 11:53:10.838 10194-10194/com.example.firebasedatabase
W/irebasedatabas: Accessing hidden field Landroid/graphics/Insets;-
>bottom:I (light greylist, linking)
2019-11-02 11:53:10.963 10194-10194/com.example.firebasedatabase
W/irebasedatabas: Accessing hidden method Landroid/view/View;-
>computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z
(light greylist, reflection)
2019-11-02 11:53:10.966 10194-10194/com.example.firebasedatabase
W/irebasedatabas: Accessing hidden method Landroid/view/ViewGroup;-
>makeOptionalFitsSystemWindows()V (light greylist, reflection)
2019-11-02 11:53:10.977 10194-10194/com.example.firebasedatabase
W/irebasedatabas: Accessing hidden method Landroid/widget/TextView;-
>getTextDirectionHeuristic()Landroid/text/TextDirectionHeuristic;
(light greylist, linking)
2019-11-02 11:53:13.502 10194-10230/com.example.firebasedatabase
W/SyncTree: Listen at /First failed: DatabaseError: Permission denied
2019-11-02 11:53:18.674 10194-10228/com.example.firebasedatabase
I/irebasedatabas: ProcessProfilingInfo new_methods=1780 is saved
saved_to_disk=1 resolve_classes_delay=8000
有蓝线出现
2019-11-02 11:53:13.502 10194-10230/com.example.firebasedatabase W/SyncTree: Listen at /First failed: DatabaseError: Permission denied
我想要创建一个数据库,无论在编辑文本中写入什么值,都应将其存储并显示在firebase控制台上。