public class HomePage extends AppCompatActivity {
private TextView mWeather;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_page);
mWeather = findViewById(R.id.weather);
String coordination = "";
Location location = MyLocationManager.getLocation(this);
if (location != null) {
coordination = location.getLatitude() + "," + location.getLongitude();
}
getWeather(Constants.WEATHER_URL + coordination + Constants.WEATHER_API_PARAM);
}
private void getWeather(String requestUrl) {
@SuppressLint("StaticFieldLeak")
AsyncTask<String, Void, String> getWeatherTask = new AsyncTask<String, Void, String>() {
@Override
protected void onPostExecute(String json) {
try {
JSONObject jsonObject = new JSONObject(json);
JSONObject current = jsonObject.optJSONObject("currently");
if (current != null) {
double fdegree = current.optDouble("temperature");
double cdegree = (fdegree - 32) / 1.8f;
DecimalFormat decimalFormat = new DecimalFormat("0.0");
mWeather.setText("Temperature:" + decimalFormat.format(cdegree) + "c");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
protected String doInBackground(String... strings) {
String jsonData = HttpUtils.httpGet(strings[0]);
return jsonData;
}
};
getWeatherTask.execute(requestUrl);
}
fragment:
public class fragmenthome extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.home_page, null);
return view;
}
}
home_page 中就有一个名为weather的textview,但是运行项目的时候,home_pag 全是空白,请问各位大神这是怎么回事?