如何从onPostExecute()获取值到onCreate

时间:2017-04-15 12:11:44

标签: java android android-asynctask

我声明了一个全局变量BigCLat和BigCLng,如下所示:

public static String BigCLat; public static String BigCLng;

并更新onPostExecute中的值。

当我尝试在此方法中使用setText时,它获得正确的值。但是,它在onCreate中没有更新和null。

解释::::
我需要为我的全局变量(BigCLat,BigCLng)设置一个值,但是当我调用onCreate时它不会更新。 -

感谢回答。

......................

这是我的onPostExecute。

public void onPostExecute(String result1) {

        BPlace_data = new ArrayList<>();

        try {
            JSONObject jsonRoot = new JSONObject(result1);
            JSONArray jArray = jsonRoot.getJSONArray("results");

            for (int i = 0; i < jArray.length(); i++) {
                JSONObject json_data = jArray.getJSONObject(i);
                NearbyPlace nearbyPlace = new NearbyPlace();
                nearbyPlace.place_name = json_data.getString("name");
                nearbyPlace.vicinity = json_data.getString("vicinity");
                nearbyPlace.lat = json_data.getJSONObject("geometry").getJSONObject("location").getString("lat");
                nearbyPlace.lng = json_data.getJSONObject("geometry").getJSONObject("location").getString("lng");
                nearbyPlace.reference = json_data.getString("reference");

                BPlace_data.add(nearbyPlace);
            }

            BigCLat = BPlace_data.get(0).lat;
            BigCLng = BPlace_data.get(0).lng;

            TextView BigCLa = (TextView) findViewById(R.id.textView4);
            BigCLa.setText(BigCLat);

        } catch (JSONException e) {
            Log.d("Exception", e.toString());
        }

    }

2 个答案:

答案 0 :(得分:0)

尝试以下步骤。

1。将以下行放在onCreate()

BigCLa = (TextView) findViewById(R.id.textView4);

2。让BigCLa全球化。

3。从onPostExecute()

中删除以下行
TextView BigCLa = (TextView) findViewById(R.id.textView4);

4。保留所有其他的东西。

现在构建代码并检查您是否应该在屏幕上获取更新的值。

答案 1 :(得分:0)

Textview BigCLa;设置为全局并使用 BigCLa = (TextView) findViewById(R.id.textView4);OnCreate方法内。 将BigCla方法中的onPostExecute值更新为BigCLa.setText(BigCLat);

相关问题