异步任务
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)
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();