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

Posted

技术标签:

【中文标题】异步任务尝试调用虚拟方法\'void android.widget.TextView.setText(java.lang.CharSequence)\'[重复]【英文标题】:Async Task Attemp to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' [duplicate]异步任务尝试调用虚拟方法'void android.widget.TextView.setText(java.lang.CharSequence)'[重复] 【发布时间】:2020-08-25 06:35:45 【问题描述】:

我正在 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());

    

主要活动

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_
    android:layout_
    tools:context=".DailyVerse_Activity">

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

</RelativeLayout>

我在 app.VOTD_Data.onPostExecute.app.VOTD_Data.onPostExecute(VOTD_Data.java: 88) 在 app.VOTD_Data.onPostExecute(VOTD_Data.java:18)

【问题讨论】:

【参考方案1】:

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

public VOTD_Data(Context context, TextView textView)

在 postExecute 中只做:

textView.setText(dailyverse.toString());

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

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

【讨论】:

将绑定到活动生命周期的Texview 传递给AsyncTask 不是一个好的选择。如果 Activity 被破坏,这仍然会导致NullPointerException。最好的选择是使用回调接口。

以上是关于异步任务尝试调用虚拟方法'void android.widget.TextView.setText(java.lang.CharSequence)'[重复]的主要内容,如果未能解决你的问题,请参考以下文章

Android Studio:尝试在空对象引用上调用虚拟方法“void android.view.View.setOnClickListener”

Android:尝试在空对象引用上调用虚拟方法“void android.widget.ListView.setAdapter(android.widget.ListAdapter)”

尝试在空对象引用上调用虚拟方法“void android.hardware.Camera.stopPreview()”

尝试在空对象引用上调用虚拟方法“void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)”

尝试在空对象引用上调用虚拟方法 'void android.view.View.measure(int, int)'

尝试在空对象引用上调用虚拟方法“void android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapt