异步任务尝试调用虚拟方法 'void android.widget.TextView.setText(java.lang.CharSequence)'

Async Task Attemp to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)'

我正在对 app.VOTD_Data.onPostExecute 处的空对象引用调用虚拟方法 'void android.widget.TextView.setText(java.lang.CharSequence)'。

异步任务

public class VOTD_Data extends AsyncTask<Void, Void, Void> {

    private String verseData = "";
    private String dailyverse = "";
    private String verseauthor = "";
    private String dailVersePref = "";
    private String verseAuthorPref = "";
    private SharedPreferences sharedPreferences;

    private Context context;

    public VOTD_Data(Context context){
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        dailVersePref = sharedPreferences.getString("dailyverse", "");
        verseAuthorPref = sharedPreferences.getString("verseauthor", "");
    }

    public VOTD_Data() {

    }


    @Override
    protected Void doInBackground(Void... voids) {

        try {
            URL url = new URL("https://beta.ourmanna.com/api/v1/get/?format=json");

            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

            String line = "";

            while (line != null){

                line = bufferedReader.readLine();
                verseData  = verseData + line;
            }

            JSONObject mainObject = new JSONObject(verseData).getJSONObject("verse");
            JSONObject verseObject = mainObject.getJSONObject("details");

            dailyverse = verseObject.getString("text");
            verseauthor = verseObject.getString("reference");

            sharedPreferences
                    .edit()
                    .putString("dailyverse", dailyverse)
                    .putString("verseauthor", verseauthor)
                    .apply();


        } catch (IOException | JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);

        //Daily Verse Activity
        DailyVerse_Activity.data.setText(dailyverse.toString());

    }
}

主要Activity

public class DailyVerse_Activity extends AppCompatActivity {


    public static TextView data;

    private ImageView banner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_daily_verse);

        data = (TextView) findViewById(R.id.dataText);

        VOTD_Data process = new VOTD_Data(DailyVerse_Activity.this);
        process.execute();


    }

    //On Back Pressed
    @Override
    public boolean onSupportNavigateUp() {
        onBackPressed();
        finish();
        return true;
    }

}

XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".DailyVerse_Activity">

    <TextView
        android:id="@+id/dataText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#000"
        android:gravity="center"
        android:hint="Verse Data"
        />

</RelativeLayout>

我在 app.VOTD_Data.onPostExecute.app.VOTD_Data.onPostExecute(VOTD_Data.java:88) 的空对象引用上调用虚拟方法 'void android.widget.TextView.setText(java.lang.CharSequence)' 在 app.VOTD_Data.onPostExecute(VOTD_Data.java:18)

将您的 rootView 传递给 AsyncTask 并从那里获取它的引用。像这样在 VOTD_Data class 中再添加一个参数。

public VOTD_Data(Context context, TextView textView)

在 postExecute 中执行:

textView.setText(dailyverse.toString());

并且在你的 Activity class 中调用 AsyncTask 时将 textView 传递给 VOTD_Data class 构造函数,如下所示:

VOTD_Data process = new VOTD_Data(DailyVerse_Activity.this, data);
process.execute();