UI に表示する JSON 応答を受け取るこのアクティビティがあります。私のリクエストは次のようになります。
public void onClick(View click) {
if (click == profBtn) {
//this line creates the post request on my RequestHandler class.
requestHandler.setURL(url);
Intent it = new Intent(MainActivity.this, ProfActivity.class);
startActivity(it);
} else if....
これは私の OnCreate メソッドで、ProfActivity のように見えます
TextView tv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.prof_activity);
//this command gets the data from the JSON Response from my mapper class.The JSON Response was converted to a HashMap.
mapData=MapperClass.map;
String value=mapData.get("key");
tv = (TextView) findViewById(R.id.textView1);
tv.setText(value);
}
setText コマンドは NullPointerException を引き起こします。リクエストの作成、レスポンスの受信、ハッシュマップへのレスポンスのマッピング、およびマップの値を ProfActivity に渡すことは正常に機能します。ここでの問題は、JSON 応答が少し遅いことです。Oncreate メソッドは、表示するデータが到着する前に既に実行されています。
アクティビティにデータを待つように指示する方法はありますか?