將字符串傳遞給我的片段java類時遇到了問題。即使它已被聲明並設置爲出現在textview上,我仍然獲得空值。將字符串傳遞給textview
TextView date;
String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
date.setText(mydate);
我已經插入此行中我onCreateView
date = (TextView) rootView.findViewById(R.id.textDate);
下面的代碼的其餘部分供參考:
:public class Notifications extends Fragment {
TextView servingqueue;
TextView date;
DatabaseReference mRootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference mServingQueue = mRootRef.child("ServingQueue");
public Notifications() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
date.setText(mydate);
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) {
}
});
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
}
@Override
public void onDetach() {
super.onDetach();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_notifications, container, false);
// Inflate the layout for this fragmentid.queueServing);
servingqueue = (TextView) rootView.findViewById(R.id.queueServing);
date = (TextView) rootView.findViewById(R.id.textDate);
return rootView;
}
}
這裏有我的日誌顯示錯誤
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at rogue.queue.Notifications.onCreateView(Notifications.java:74)
這裏是碎片換貨布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="rogue.queue.Notifications"
android:id="@+id/notification_relative_layout">
<!-- TODO: Update blank fragment layout -->
<TextView
android:text="Your Number:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="72dp"
android:id="@+id/textView3"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:text="0000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView3"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:id="@+id/myqueue"
android:textSize="40sp" />
<TextView
android:text="Current Number:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView9"
android:layout_marginTop="62dp"
android:layout_below="@+id/myqueue"
android:layout_toStartOf="@+id/myqueue" />
<TextView
android:text="PLEASE BE SEATED"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="78dp"
android:id="@+id/textView15"
android:layout_below="@+id/queueServing"
android:layout_centerHorizontal="true" />
<TextView
android:text="-"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/queueServing"
android:layout_alignBaseline="@+id/textView9"
android:layout_toEndOf="@+id/myqueue" />
<TextView
android:text="YOU WILL BE SERVED SHORTLY"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="19dp"
android:id="@+id/textView14"
android:layout_below="@+id/textView15"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Welcome to Sunway Education Group"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="68dp"
android:id="@+id/textDate"
tools:text="00 - JAN - 0000"
android:layout_below="@+id/textView14"
android:layout_alignEnd="@+id/textView9" />
<Button
android:text="CANCEL QUEUE"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="21dp"
android:id="@+id/buttonQueueCancel"
android:background="@android:color/holo_red_light"
/>
檢查android文檔中的片段生命週期。 – Raghunandan
'onCreateView'在'onCreate'後面調用,所以'date'在你嘗試使用它時沒有被設置。而是嘗試從'onViewCreated'方法內查找並設置'date'上的文本。 – clownba0t
@ clownba0t同樣的錯誤,我仍然嘗試調用虛方法空對象引用錯誤。 –