简体   繁体   中英

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. The TextView is a part of a layout which added programmatically to the MainActivity's root view. The app crashes when I try to call setText() due to the error.

NullPointerException: Attempt to invoke virtual method on a null object reference

I have tried accessing the TextView with different approaches, but every time it threw the same 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.

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.

´´´ 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?

you can't use findViewById() on inflated layout you must add the inflated layout to your main layout via

mainLayout.addView(mTime);

as written here findViewById() returns null for an inflated layout

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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