简体   繁体   中英

Android Studio Can't pass TextView object between Fragment class and Thread

ERROR:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

ON LINE:

olxLoadingFragment.pricesTextView.setText("some text");

I have one Fragment class that's called OLXLoadingFragment and it has one TextView nested in ScrollView. I basically want to update that TextView's text dynamically in my OLXThread, but can't pass that TextView object to the OLXThread itself. Here are code parts that could help you:

OLXLoadingFragment class:

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;
import android.widget.TextView;

import java.io.Serializable;

public class OLXLoadingFragment extends Fragment {
    public static View OLXLoadingFragmentView;
    public static ScrollView scrollViewPrices;
    public static TextView pricesTextView;
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        OLXLoadingFragmentView = inflater.inflate(R.layout.fragment_olx_loading, container, false);
        scrollViewPrices = OLXLoadingFragmentView.findViewById(R.id.olx_scroller);
        pricesTextView = OLXLoadingFragmentView.findViewById(R.id.olx_prices_text);
        return inflater.inflate(R.layout.fragment_olx_loading, container, false);
    }
}

OLXThread class:

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.widget.ScrollView;
import android.widget.TextView;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.w3c.dom.Text;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

import static android.app.PendingIntent.getActivity;

public class OLXThread extends Thread {
    public static double fullSum = 0.0d;
    public static ArrayList<String> pricesPerPage = new ArrayList<String>();
    public static ArrayList<String> pricesPerPage2 = new ArrayList<String>();
    public static ArrayList<String> prices = new ArrayList<String>();
    public static String toSearch;
    public static TextView olxpricesView;
    public static Context context;
    public OLXThread(String toSearch) {
        this.toSearch = toSearch;
    }

    public void run() {
        //SET SOME TEXT TO TEXTVIEW HERE
        OLXLoadingFragment olxLoadingFragment = new OLXLoadingFragment();
        olxLoadingFragment.pricesTextView.setText("some text"); //<- DOESN'T WORK!
    }

OLXLoadingFragment xml layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent" android:layout_height="match_parent">

    <ImageView
            android:id="@+id/imageView2"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@drawable/rectangle"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.82" />

    <TextView
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:gravity="center"
            android:text="@string/olx_loading_engl_text"
            android:textColor="@color/limegreen"
            android:textSize="30sp"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="1.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.82" />

    <ImageView
            android:id="@+id/imageView6"
            android:layout_width="287dp"
            android:layout_height="284dp"
            android:src="@drawable/rectangle"
            app:layout_constraintBottom_toBottomOf="@+id/olx_scroller"
            app:layout_constraintEnd_toEndOf="@+id/olx_scroller"
            app:layout_constraintStart_toStartOf="@+id/olx_scroller"
            app:layout_constraintTop_toTopOf="@+id/olx_scroller" />

    <ScrollView
            android:id="@+id/olx_scroller"
            android:layout_width="287dp"
            android:layout_height="284dp"
            android:layout_marginBottom="36dp"
            android:fillViewport="true"
            android:scrollbars="vertical"
            app:layout_constraintBottom_toTopOf="@+id/imageView2"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent">

        <TextView
                android:id="@+id/olx_prices_text"
                android:text="neki tekst"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1.0"

                />
    </ScrollView>
</android.support.constraint.ConstraintLayout>

Please help!

At the time you call the method pricesTextView.setText(), the OnCreateView method of the Fragment wasn't called yet, so the pricesTextView was still null, as you only set it's value on the onCreateView method.

You should ensure that pricesTextView is not null before you call any method on this.

Note also that the only Thread that can touch views is the Ui thread, so even if your TextView is not null you cannot modify it's properties in other threads

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