简体   繁体   English

访问 TextView 的 setText 方法时出现 NullPointerException

[英]NullPointerException when access TextView's setText method

I'm trying to call setText() method of a TextView frequently to display time on a second thread.我正在尝试频繁调用TextViewsetText()方法以在第二个线程上显示时间。 The TextView is a part of a layout which added programmatically to the MainActivity's root view. TextView是布局的一部分,它以编程方式添加到 MainActivity 的根视图。 The app crashes when I try to call setText() due to the error.由于错误,当我尝试调用setText()时应用程序崩溃。

NullPointerException: Attempt to invoke virtual method on a null object reference NullPointerException:尝试在 null object 参考上调用虚拟方法

I have tried accessing the TextView with different approaches, but every time it threw the same NullPointerException .我尝试使用不同的方法访问TextView ,但每次它都会抛出相同的NullPointerException The only solution that I know is to add the layout I want to the activity_main.xml, but that's not quite what I want.我知道的唯一解决方案是将我想要的布局添加到activity_main.xml,但这并不是我想要的。

Here's how I inflate and add the views to a Linearlayout(id/WidgetScreen) inside the MainActivity.java and how I'm calling the setText() method.这是我如何膨胀并将视图添加到 MainActivity.java 内的 Linearlayout(id/WidgetScreen) 以及我如何调用setText()方法。

´´´ LinearLayout WidgetScreen = (LinearLayout) findViewById(R.id.WidgetScreen); ´´´ LinearLayout WidgetScreen = (LinearLayout) findViewById(R.id.WidgetScreen);

mTime = getLayoutInflater().inflate(R.layout.widget_time, null);
mDate = (TextView) mTime.findViewById(R.id.TVdate);
mMEZ = (TextView) mTime.findViewById(R.id.TVmez);
mGMT = (TextView) mTime.findViewById(R.id.TVgmt);

WidgetScreen.addView(mTime);

// The code where I set time for TextView
while (!StopDateThread) {
    final String mmTime = getformatedUST();
    final String mmDate = getformatedDate();
    final String mmGMT = getformatedGMT();
    
    Log.d(TAG, "run: ");
    try {
        mMEZ.setText(mmTime);
        mDate.setText(mmDate);
        mGMT.setText(mmGMT);
        Thread.sleep(500);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
widget_time.XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:orientation="vertical"
    android:layout_width="match_parent"
    android:id="@+id/WidgetTabTime"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_clock"
        android:layout_marginTop="@dimen/LabelIconMarginTop"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:layout_marginStart="@dimen/LabelIconMarginStart"
        android:layout_gravity="center"
        android:contentDescription="CLOCK"
        android:id="@+id/LabelIcon"/>

    <View
        android:id="@+id/view"
        android:layout_width="match_parent"
        android:layout_height="@dimen/StrokeWidth"
        android:layout_marginTop="@dimen/LabelDividerMarginTop"
        android:background="@color/colorAccent"
        app:layout_constraintTop_toBottomOf="@id/LabelIcon" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/colorAccent"
        android:fontFamily="sans-serif-light"
        app:layout_constraintStart_toEndOf="@id/LabelIcon"
        android:layout_marginStart="@dimen/LabelTextMarginStart"
        app:layout_constraintBottom_toBottomOf="@id/LabelIcon"
        android:text="@string/time"
        android:textSize="@dimen/LabelText"/>

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Date:"
        app:layout_constraintStart_toStartOf="parent"
        android:layout_marginStart="@dimen/StatsMarginStart"
        android:layout_marginTop="@dimen/StatsMarginTop"
        android:textColor="@color/colorAccent"
        android:fontFamily="sans-serif-light"
        android:textSize="@dimen/StatsText"
        app:layout_constraintTop_toBottomOf="@+id/view" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/StatsMarginTop"
        android:fontFamily="sans-serif-light"
        android:text="MEZ:"
        android:textColor="@color/colorAccent"
        android:textSize="@dimen/StatsText"
        app:layout_constraintStart_toStartOf="@+id/textView"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="GMT:"
        android:textColor="@color/colorAccent"
        android:fontFamily="sans-serif-light"
        android:textSize="@dimen/StatsText"
        android:layout_marginTop="@dimen/StatsMarginTop"
        app:layout_constraintStart_toStartOf="@+id/textView2"
        app:layout_constraintTop_toBottomOf="@+id/textView2" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="LST:"
        android:layout_marginTop="@dimen/StatsMarginTop"
        android:textColor="@color/colorAccent"
        android:fontFamily="sans-serif-light"
        android:textSize="@dimen/StatsText"
        app:layout_constraintStart_toStartOf="@+id/textView3"
        app:layout_constraintTop_toBottomOf="@+id/textView3" />

    <TextView
        android:id="@+id/TVdate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/CurrentDate"
        android:layout_marginEnd="@dimen/StatsMarginEnd"
        android:textColor="@color/colorAccent"
        android:fontFamily="sans-serif-light"
        android:textSize="@dimen/StatsText"
        app:layout_constraintBottom_toBottomOf="@+id/textView"
        app:layout_constraintEnd_toEndOf="parent" />

    <TextView
        android:id="@+id/TVmez"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/CurrentLocalTime"
        android:layout_marginEnd="@dimen/StatsMarginEnd"
        android:textColor="@color/colorAccent"
        android:fontFamily="sans-serif-light"
        android:textSize="@dimen/StatsText"
        app:layout_constraintBottom_toBottomOf="@+id/textView2"
        app:layout_constraintEnd_toEndOf="parent" />

    <TextView
        android:id="@+id/TVgmt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/CurrentGMT"
        android:layout_marginEnd="@dimen/StatsMarginEnd"
        android:textColor="@color/colorAccent"
        android:fontFamily="sans-serif-light"
        android:textSize="@dimen/StatsText"
        app:layout_constraintBottom_toBottomOf="@+id/textView3"
        app:layout_constraintEnd_toEndOf="parent" />

    <TextView
        android:id="@+id/textView8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="02:57:15"
        android:layout_marginEnd="@dimen/StatsMarginEnd"
        android:textColor="@color/colorAccent"
        android:fontFamily="sans-serif-light"
        android:textSize="@dimen/StatsText"
        app:layout_constraintBottom_toBottomOf="@+id/textView4"
        app:layout_constraintEnd_toEndOf="parent" />
    <View
        android:layout_width="match_parent"
        android:layout_height="5dp"
        app:layout_constraintTop_toBottomOf="@id/textView4"/>
    <View
        android:layout_width="match_parent"
        android:layout_height="5dp"
        app:layout_constraintTop_toBottomOf="@id/textView4"/>

</androidx.constraintlayout.widget.ConstraintLayout>

What would be the simplest way to access the setText() method?访问setText()方法的最简单方法是什么?

you can't use findViewById() on inflated layout you must add the inflated layout to your main layout via您不能在充气布局上使用findViewById()您必须通过以下方式将充气布局添加到主布局中

mainLayout.addView(mTime);

as written here findViewById() returns null for an inflated layout如此处所写findViewById() 返回 null 用于膨胀布局

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM