1

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?

DaJunti
  • 11
  • 2
  • Please add layout for widget_telescope.xml – Sarah Khan Dec 02 '20 at 02:27
  • When inflating the `mTelescope` view, try to pass the WidgetScreen as the parent layout like this `mTelescope = getLayoutInflater().inflate(R.layout.widget_telescope,WidgetScreen);` – tom Dec 02 '20 at 03:02
  • Also, load the `mDate`, `mMEZ` and `mGMT` views after adding the `mTelescope` view to `WidgetScreen` view. – tom Dec 02 '20 at 03:03
  • Actually i meant `mTime` instead of `mTelescope`, i have corrected that but it should not effect the result beacuase those id's should be global right? furthermore i added the parent `mTime = getLayoutInflater().inflate(R.layout.widget_time,WidgetScreen);` , changed the Order so the `mTime` Layout gets added to the WidgetScreen first and then it tries to find those Textviews, but it is still not finding them – DaJunti Dec 02 '20 at 06:23

1 Answers1

1

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

ATP
  • 2,939
  • 4
  • 13
  • 34
  • yes i have checked the id's they match, but is it possible that it is not finding it because it is not part mainActivity.XML file ? – DaJunti Dec 01 '20 at 17:36
  • is your TextView added programmatically? – ATP Dec 01 '20 at 17:51
  • actually no, it is a Linearlayout with the Textview in it – DaJunti Dec 01 '20 at 19:56
  • so what do you mean not part of main activity file – ATP Dec 01 '20 at 19:57
  • the textview is part of a 2nd layout, wich i inflate and add programmatically to the mainactivity layoutfile. But the Textview is actually not hardcoded in the mainactivity's layout – DaJunti Dec 01 '20 at 20:02
  • iterate over the layout children and check if id is equal – ATP Dec 01 '20 at 20:41
  • the id's are correct! it seems i dont have access to other Layoutfiles in the mainAcitivity class – DaJunti Dec 02 '20 at 06:38
  • as written here https://stackoverflow.com/questions/36883292/findviewbyid-returns-null-for-an-inflated-layout – ATP Dec 02 '20 at 07:08
  • and here https://stackoverflow.com/questions/9228777/findviewbyid-not-working-in-a-not-mainactivity-class – ATP Dec 02 '20 at 07:13