我将字符串传递给片段java类时遇到问题。即使声明并设置为出现在textview上,我仍然保持null值。
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"
/>
答案 0 :(得分:0)
首先要尝试的是,检查您的TextView是否可以成功找到。
date = (TextView) rootView.findViewById(R.id.textDate);
if (date == null)
Log.e("x", "NOT FOUND");
查看是否显示此日志行。如果是,R.id.textDate
不是您的TextView
或rootView
是错误的父母。
如果您确定找到了TextView,我建议创建一个方法,而不是在eventcallback 1中设置日期引用并在callback2中指定一些内容。
像这样:
private void applyTextValues(Date date, float queueValue) {
TextView
date = (TextView) rootView.findViewById(R.id.textDate),
servingqueue = (TextView) rootView.findViewById(R.id.queueServing);
if (date != null)
date.setText(...); // Format this via SimpleDateFormat class
if (servingQueue != null)
servingQueue.setText(...); // Format your number.
}
然后在更改事件中,在onCreate中调用applyTextValues
以及您需要它的位置。通过这种方式,您可以独立于任何生命周期,Android端的任何解除分配,任何与活动相关的操作。当您调用此方法时,您可以在此处获得最新的参考 ,并且您已完成。
答案 1 :(得分:0)
正如我在评论中建议的那样,尝试查找文本视图并在onViewCreated
方法中设置其值。请注意,这与您已使用的onCreateView
方法不同。这是一些示例代码:
@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()));
}
另外,为了帮助调试,请确保date
成员变量未在类中的任何其他位置设置或使用,至少目前是这样。