TextView在更新后消失,它不为空

时间:2018-03-22 04:53:03

标签: android textview

我有一个在Try Catch块内更新的TextView。我可以看到更新的文本显示一秒钟然后它从模拟器中消失。我不知道造成这种不良行为的原因。我的第一个猜测是该值为null所以我写了一个快速行到日志并调用TextView的getText()方法并确认我的数据值良好并且不为空。我的第二次尝试是进入XML文件并检查我的视图是否没有任何冲突。当我刚开始开发这个应用程序时,我没有更新UI,所以只是基本上拖动屏幕中的组件来初始化它们。我有另一个具有相同颜色属性和相同背景图像的TextView,它显示正常。删除了布局组件,只是省略了我的两个TextView。一个显示,另一个显示一秒,然后消失。

更新TextViews的功能

private void setDateTime() {
        Locale locale = Locale.getDefault();
        TimeZone timeZone = TimeZone.getDefault();
        Calendar calendar = Calendar.getInstance(timeZone, locale);
        Date currentDateTime = calendar.getTime();
        String dateTime = currentDateTime.toString();
        Log.d(LOG_TAG, "The current date and time is: " + dateTime);

        SimpleDateFormat inputDate = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
        SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, MMM dd");
        SimpleDateFormat timeFormat = new SimpleDateFormat("hh:mm a");

        try {
            Date parsedDate = inputDate.parse(dateTime);
            mCurrentDateTV.setText(dateFormat.format(parsedDate));
            mCurrentTimeTV.setText(timeFormat.format(parsedDate));
            Log.d(LOG_TAG, "The current date is: " + mCurrentDateTV.getText() + " and the time is " + mCurrentTimeTV.getText());
        } catch (ParseException e) {
            Log.d(LOG_TAG, "Error parsing date and time: " + e.toString());
        }
    }

XML布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/fragment_current_weather_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="91dp"
        android:text="TextView"
        android:textColor="@android:color/black"
        android:textSize="15sp" />

    <com.nvanbenschoten.motion.ParallaxImageView
        android:id="@+id/fragment_current_weather_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <ImageView
        android:id="@+id/fragment_current_weather_icon"
        android:layout_width="90dp"
        android:layout_height="90dp" />


    <ImageView
        android:id="@+id/fragment_current_weather_settings_icon"
        android:layout_width="wrap_content"
        android:layout_height="3dp" />

    <TextView
        android:id="@+id/fragment_current_weather_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginStart="56dp"
        android:layout_toEndOf="@+id/fragment_current_weather_icon"
        android:text="TextView"
        android:textColor="@android:color/black"/>

</RelativeLayout>

日志文件

03-22 00:46:09.346 7818-7818/com.xxcanizeuxx.clima_tact/CurrentWeatherFragment: The current date and time is: Thu Mar 22 00:46:09 EDT 2018

03-22 00:46:10.009 7818-7818/com.xxcanizeuxx.clima_tact/CurrentWeatherFragment: The current date is: Thu, Mar 22 and the time is 12:46 AM

正如您所看到的,我的TextView中有数据。

0 个答案:

没有答案