Anonymous Vagrant Anonymous Vagrant - 27 days ago 9
Java Question

Passing date as string to textview

I have a problem with passing a string to my fragment java class. I kept getting null value even though it is declared and set to appear on textview.

TextView date;
String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
date.setText(mydate);


I've inserted this line on my onCreateView

date = (TextView) rootView.findViewById(R.id.textDate);


Here's the rest of the code for reference:

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;
}

}


Here's the error showing on my log:

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)


Here's the fragment layout:

<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"
/>



Answer

As I suggested in the comments, try finding the text view and setting its value in the onViewCreated method. Please note that this is different to the onCreateView method which you are already using. Here's some example code:

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    date = (TextView) view.findViewById(R.id.textDate);
    date.setText(DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime()));
}

Also, to aid in debugging, please make sure that the date member variable is not being set or used anywhere else in the class, at least for now.