Android Studio无法在Fragment类和Thread之间传递TextView对象

时间:2019-11-23 23:56:17

标签: java android android-studio android-fragments

错误:

  

java.lang.NullPointerException:尝试调用虚拟方法'void   android.widget.TextView.setText(java.lang.CharSequence)'上的null   对象引用

在线:

  

olxLoadingFragment.pricesTextView.setText(“某些文本”);

我有一个名为OLXLoadingFragment的Fragment类,它在ScrollView中嵌套了一个TextView。我基本上想在我的OLXThread中动态更新该TextView的文本,但是不能将该TextView对象传递给OLXThread本身。以下是可以帮助您的代码部分:

OLXLoadingFragment类:

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类:

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布局:

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

请帮助!

1 个答案:

答案 0 :(得分:0)

当您调用方法pricesTextView.setText()时,尚未调用该片段的OnCreateView方法,因此pricesTextView仍为空,因为您仅在onCreateView方法上设置了它的值。

在调用任何方法之前,应确保pricesTextView不为null。

还请注意,唯一可以触摸视图的线程是Ui线程,因此,即使您的TextView不为null,您也无法在其他线程中修改其属性