firebase实时数据库(不上传)

cvxl0en2  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(405)

我在尝试上传到firebase时遇到了一个问题,我已经示例化了每个变量,所以我认为这不是问题所在,我得到的错误是java.lang.nullpointerexception:尝试在空对象引用上调用虚拟方法'android.text.editable android.widget.edittext.gettext()',正好在这一行string milkquantity=milkq.gettext().tostring();位于:

private void createPopupDialog(){
    dialogBuilder = new AlertDialog.Builder(this);
    View view = getLayoutInflater().inflate(R.layout.popup2, null);

    dialogBuilder.setView(view);
    dialogBuilder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

            //start posting
            String milkQuantity = milkq.getText().toString();
            String note = noteText.getText().toString();

            if(!milkQuantity.equals("") && !noteText.equals("")){
                BlogCalendar detailsInfo = new BlogCalendar(milkQuantity, note);
                m

databasereference.child(“mmilkdetails”).child(userid).setvalue(detailsinfo);}其他{

}

        }
    });

在我的.xml文件中,编辑文本是:

<EditText
            android:id="@+id/milkQuantity"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/title"
            android:layout_alignParentStart="true"
            android:layout_alignParentEnd="true"
            android:layout_marginStart="25dp"
            android:layout_marginEnd="25dp"
            android:hint="@string/milkQty"
            android:textColorHint="#5E5E5E" />

        <EditText
            android:id="@+id/noteText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/milkQuantity"
            android:layout_alignParentStart="true"
            android:layout_alignParentEnd="true"
            android:layout_marginStart="25dp"
            android:layout_marginEnd="25dp"
            android:hint="@string/notes_hint"
            android:paddingTop="20dp"
            android:textColorHint="#5E5E5E" />

如果你想知道,变量是示例化的

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

    mAuth = FirebaseAuth.getInstance();
    mUser = mAuth.getCurrentUser();
    mDatabase = FirebaseDatabase.getInstance();
    mDatabaseReference = 
    FirebaseDatabase.getInstance().getReference().child("MmilkDetails");
    userId = mUser.getUid();

    blogCalendarList = new ArrayList<>();

    recyclerView2 = (RecyclerView) findViewById(R.id.recyclerViewCalendarActivity);
    recyclerView2.setHasFixedSize(true);
    recyclerView2.setLayoutManager(new LinearLayoutManager(this));
    mDatabaseReference.keepSynced(true);

    milkq =  findViewById(R.id.milkQuantity);
    noteText =  findViewById(R.id.noteText);
bxfogqkk

bxfogqkk1#

获取nullpointerexception的原因是,milkquantity是在popup.xml中定义的,并且您正在从r.layout.activity\u calendar\ux中执行findviewbyid(inside oncreate()方法)

milkq =  findViewById(R.id.milkQuantity);//this will assign null to milkq, hence the 
                                         //nullpointer exception later.

您需要在r.layout.activity\u calendar\u中输入牛奶量,或者在oncreate()方法中执行以下代码:

setContentView(R.layout.popup.xml); //instead of setContentView(R.layout.activity_calendar_);

快乐的编码!

相关问题