【问题标题】:Passing date as string to textview将日期作为字符串传递给 textview
【发布时间】:2017-03-26 11:17:03
【问题描述】:

我在将字符串传递给我的片段 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"
    />

【问题讨论】:

  • 在 android 文档中检查片段生命周期。
  • onCreateViewonCreate 之后被调用,所以date 在您尝试使用它时尚未设置。尝试从 onViewCreated 方法中查找并设置 date 上的文本。
  • @clownba0t 同样的错误,我仍然得到尝试调用虚方法空对象引用错误。
  • 请发布您看到的完整堆栈跟踪。也请发布布局文件R.layout.fragment_notifications
  • @clownba0t 编辑了主帖以显示错误。片段通知包含通常的元素以及名为 textDate 的 textview id。

标签: java android android-fragments parameter-passing


【解决方案1】:

正如我在 cmets 中建议的那样,尝试查找文本视图并在 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 成员变量没有在类的其他任何地方设置或使用,至少现在是这样。

【讨论】:

  • 谢谢。我插入了这个,没有错误。但是该字符串没有将其值传递给 textview,因此 textview 保持为空。
  • 好吧,好吧,希望这是朝着正确方向迈出的一步。剩下的问题可能是由许多事情引起的。如果您需要进一步的帮助,请再次发布您的布局文件 (R.layout.fragment_notifications) 并使用 Notifications 类的当前代码更新您的问题。
  • 谢谢。我在本地查看了您的布局,似乎 textDate TextView 实际上显示在“取消队列”按钮下方。这就是为什么即使在正确设置之后也看不到文本的原因。至少现在,尝试从 XML 文件中删除 android:layout_marginTop="68dp" 属性。这至少应该将它带到屏幕上以便您可以看到它,然后您可以正确定位它。
  • 已修复! xml 是这里的问题。我删除了 layout_marginTop 并且日期可见。这是一个菜鸟的错误。 :) 谢谢伙计。
  • 好的,我意识到通过在 onViewCreated 上放置这些额外的行,每次我移动到这个片段时它都会改变时间和日期。我希望日期保持不变并保持不变。我该怎么做?
【解决方案2】:

首先要尝试的是,检查是否可以成功找到您的 TextView。

date = (TextView) rootView.findViewById(R.id.textDate);
if (date == null)
    Log.e("x", "NOT FOUND");

查看此日志行是否出现。如果是,R.id.textDate 不是您的 TextViewrootView 是错误的父母。

如果您可以确定,您的 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 端的任何释放、任何与 Activity 相关的操作。当您调用此方法时,您将获得最新的引用就在此处,您就完成了。

【讨论】:

  • 我尝试了您的日志方法,但是当我单击该片段时,我一直收到错误消息。错误发生在 date.setText(mydate);指向空对象引用。
  • - 问题已解决,已删除 - 抱歉没看到 -
猜你喜欢
  • 2012-10-05
  • 1970-01-01
  • 1970-01-01
  • 2020-01-28
  • 1970-01-01
  • 1970-01-01
  • 2021-03-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多