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

问题描述 投票:0回答:1
我正在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_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>

Im正在对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)

java android android-studio android-layout android-asynctask
1个回答
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();

© www.soinside.com 2019 - 2024. All rights reserved.