无法将double值作为String转换为textview

时间:2016-11-10 13:42:44

标签: java android

我尝试在空对象引用上调用虚方法void android.widget.TextView.setText(java.lang.CharSequence)'时出现空指针执行错误。

public class MainActivity {
    TextView servingqueue;
}

这是onCreate

    DatabaseReference mRootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference mServingQueue = mRootRef.child("ServingQueue");

servingqueue = (TextView) findViewById(R.id.queueServing);

mServingQueue.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        Double number = dataSnapshot.getValue(Double.class);
        servingqueue.setText(String.valueOf(number));
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }

错误出现在

servingqueue.setText(String.valueOf(number));

它只是不想将值从Firebase传递到我的textview。加载活动后,我会从我的应用程序中关闭。

1 个答案:

答案 0 :(得分:0)

First Cast Double to double(primitive)然后将其转换为String

Double number = dataSnapshot.getValue(Double.class);
 double d=number.doubleValue();
        servingqueue.setText(String.valueOf(d));