如果我尝试将"Test1."作为值插入EditText,则该应用程序将崩溃。但是,如果我删除了period(.)并重试输入值,那很好。为什么我不能在period(.)中插入值。下面是我的代码片段,包括xml文件。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="com.example.naveenbimsara.pharmacy.MainActivity"

    >

    <EditText
        android:id="@+id/Mediname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textUri|textWebEditText|textMultiLine|textNoSuggestions|textFilter|text|textPostalAddress|textEmailAddress|textEmailSubject"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.545"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.287"
        tools:text="@string/google_storage_bucket" />

    <Button
        android:id="@+id/insbtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Insert"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.691" />

    <Spinner
        android:id="@+id/availa"
        android:layout_width="330dp"
        android:layout_height="55dp"
        android:layout_marginStart="40dp"
        android:layout_marginLeft="40dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.465" />
</android.support.constraint.ConstraintLayout>

AddMedicine.java
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

public class AddMedicine extends AppCompatActivity {
    private static final String TAG = "AddMedicine";

    EditText mName;
    Spinner mySpinner;
    ArrayAdapter<String>myAdapter;
    Button mInsert;
    DatabaseReference reff;
    Medi medi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_medicine);
        mName=(EditText)findViewById(R.id.Mediname);
        mySpinner=(Spinner)findViewById(R.id.availa);

        myAdapter = new ArrayAdapter<String>(AddMedicine.this,android.R.layout.simple_list_item_1,getResources().getStringArray(R.array.ava));
        myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mySpinner.setAdapter(myAdapter);

        mInsert=(Button)findViewById(R.id.insbtn);
        medi=new Medi();
        reff= FirebaseDatabase.getInstance().getReference().child("Medicine");
        mInsert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                medi.setName(mName.getText().toString().trim());
                medi.setAvailable(mySpinner.getSelectedItem().toString());
                final String Name=mName.getText().toString().trim();
                if(Name.length()==0)
                {
                    mName.requestFocus();
                    mName.setError("FIELD CANNOT BE EMPTY");
                }


                else {
                    reff.child(mName.getText().toString().trim()).setValue(medi);
                    Toast.makeText(AddMedicine.this, "Data Inserted sucessfully", Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}

Medi.java
public class Medi {
    private String Name;
    private String Available;


    public Medi() {
    }

    public Medi(String name, String available) {
        Name = name;
        Available = available;
    }

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public String getAvailable() {
        return Available;
    }

    public void setAvailable(String available) {
        Available = available;
    }


}

Logcat

java - 无法为Android Studio插入值-LMLPHP

java - 无法为Android Studio插入值-LMLPHP

最佳答案

是的,我明白了。原因是您尝试将EditText值添加为Firebase数据库子引用。因此,有些特殊字符不能使用,包括period(.)。

如果点指定为在点之后没有其他文本,则将导致应用程序崩溃,因为点标识为另一个子节点。阅读more here

以下内容不能用作Firebase数据库密钥(子名称):

  • .(句号)
  • $(美元符号)
  • [(左方括号)
  • ](右方括号)
  • #(哈希或井号)
  • /(正斜线)
  • 10-07 12:40