2017-04-15 27 views
0

我聲明一個全局變量BigCLat和BigCLng這樣的值:如何從onPostExecute()來的onCreate

公共靜態字符串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()); 
     } 

    } 

回答

0

請嘗試以下步驟。

1。把下面一行放在onCreate()

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

2。使BigCLa成爲全球。

3。從onPostExecute()刪除以下行

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

4。保持原樣。

現在建立代碼,並檢查你應該得到屏幕上的更新值。

+0

哦!我得到更新的值。但是我想在onCreate上更新BigCLat,BigCLng和setText。 – user3712289

+0

@ user3712289但是我想在onCreate上更新BigCLat,BigCLng和setText?請解釋 。 –

+0

我需要爲我的全局變量(BigCLat,BigCLng)設置一個值,但是當我在onCreate中調用時它不會更新。 – user3712289

0

設置Textview BigCLa;全球和使用 BigCLa = (TextView) findViewById(R.id.textView4);OnCreate方法。 更新BigClaonPostExecute方法爲BigCLa.setText(BigCLat);