我尝试运行以下代码,但android studio一直说保存字符串对象存在问题,但据我所知,代码编写正确。任何人都可以看到问题所在并帮助我解决问题吗? 在最新的更新之后我对android studio有更多的问题,所以我很难弄清楚问题是由于我的代码在某处错误还是更新。
错误:(90,37)错误:没有为Intent(CityWeatherData,Class)构造函数找到合适的构造函数Intent.Intent(String,Uri)不适用(参数不匹配; CityWeatherData无法转换为String)构造函数Intent.Intent (Context,Class)不适用(参数不匹配; CityWeatherData无法转换为Context)
错误:(95,13)错误:找不到符号方法startActivity(Intent)
错误:任务':app:compileDebugJavaWithJavac'的执行失败。 >编译失败;有关详细信息,请参阅编译器错误输出。
我有一个类,它将OpenWeatherMap中的数据作为JSON对象获取,并将它们显示在Activity中。
非活动类:
public class CityWeatherData extends AsyncTask<String,Void,String> {
@Override
protected String doInBackground(String... urls) {
String result = ""; //JSON data will be kept here when first downloaded
URL url;
HttpURLConnection urlConnection = null;
//API set up in OpenWeatherMap
//Try and catch used in case user does not have internet connection etc.
try {
url = new URL(urls[0]);
urlConnection = (HttpURLConnection) url.openConnection();
InputStream input = urlConnection.getInputStream();
//Reader to read inputStream for URL
InputStreamReader inputReader = new InputStreamReader(input);
int data = inputReader.read(); //Data from stream is put into an int called data
//When data finishes reading = -1 ; There for while loop need for data not equal to -1
while (data != -1){
char current = (char) data;
result += current;
data = inputReader.read();
}
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
//Create JSON object from result
try {
JSONObject jsonObject = new JSONObject(result);
JSONObject weatherData = new JSONObject(jsonObject.getString("main"));
//The data we are interesed in is located after "main"
//Get temp from main
double temp = Double.parseDouble(weatherData.getString("temp"));
//Temp is given i Kelvin so it needs to be converted to Celcius
int tempInt = (int) (temp -273.15);
//Get city name
String placeName = jsonObject.getString("name");
//Get description
String weatherDescription = jsonObject.getString("description");
//Get humidity
double humidityValue = Double.parseDouble(weatherData.getString("humidity"));
Intent sendDataIntent = new Intent(CityWeatherData.this, CityDetailsActivity.class);
sendDataIntent.putExtra("tempData", tempInt);
sendDataIntent.putExtra("cityNameData", placeName);
sendDataIntent.putExtra("humidityData", humidityValue);
sendDataIntent.putExtra("descriptionData", weatherDescription);
} catch (Exception e) {
e.printStackTrace();
}
}
}
活动类:
public class CityDetailsActivity extends AppCompatActivity {
String placeName;
int tempInt;
double humidityValue;
String weatherDescription;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_city_details);
Bundle getData = getIntent().getExtras();
if (getData !=null){
int tempInt = getData.getInt("tempData");
String placeName = getData.getString("cityNameData");
double humidityValue = getData.getDouble("humidityData");
String weatherDescription = getData.getString("descriptionData");
}
}
@Override
protected void onResume() {
super.onResume();
// Update with info from ShowData
updateFields();
}
private void updateFields(){
// Used to show data
TextView city_name = findViewById(R.id.city_name);
city_name.setText(placeName);
TextView temp = findViewById(R.id.temp);
temp.setText(String.valueOf(tempInt));
TextView humidity = findViewById(R.id.humidity);
humidity.setText(String.valueOf(humidityValue));
TextView show_description = findViewById(R.id.show_description);
show_description.setText(weatherDescription);
}
//Save current data in activity upon rotation
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
}
答案 0 :(得分:0)
您的AsyncTask
在没有实际启动活动的情况下接收参数。
编辑:对活动上下文使用弱引用。
private static class CityWeatherData extends AsyncTask<String,Void,String> {
private WeakReference<MainActivity> mainActivity;
public CityWeatherData(MainActivity context) {
mainActivity = new WeakReference<>(context);
}
@Override
protected String doInBackground(String... urls) {
....
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
MainActivity cxt = mainActivity.get();
if (cxt != null) {
//Create JSON object from result
try {
JSONObject jsonObject = new JSONObject(result);
JSONObject weatherData = new JSONObject(jsonObject.getString("main"));
//The data we are interesed in is located after "main"
//Get temp from main
double temp = Double.parseDouble(weatherData.getString("temp"));
//Temp is given i Kelvin so it needs to be converted to Celcius
int tempInt = (int) (temp -273.15);
//Get city name
String placeName = jsonObject.getString("name");
//Get description
String weatherDescription = jsonObject.getString("description");
//Get humidity
double humidityValue = Double.parseDouble(weatherData.getString("humidity"));
Intent sendDataIntent = new Intent(cxt, CityDetailsActivity.class);
sendDataIntent.putExtra("tempData", tempInt);
sendDataIntent.putExtra("cityNameData", placeName);
sendDataIntent.putExtra("humidityData", humidityValue);
sendDataIntent.putExtra("descriptionData", weatherDescription);
startActivity(sendDataIntent);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}