Java android studio 3.0更新或错误代码导致的问题?
我尝试运行以下代码,但android studio一直说保存字符串对象有问题,但据我所知,代码编写正确。有人能看到问题所在并帮我解决吗? 在最新的更新之后,我在android studio上遇到了更多的问题,所以我很难确定这些问题是由于我的代码在某个地方出错还是由于更新 错误:(90,37)错误:找不到适合Intent(CityWeatherData,类)构造函数意图的构造函数。Intent(字符串,Uri)不适用(参数不匹配;CityWeatherData无法转换为字符串)构造函数意图。Intent(上下文,类)不适用(参数不匹配;CityWeatherData无法转换为上下文) 错误:(95,13)错误:找不到符号方法startActivity(Intent) 错误:任务执行失败:应用程序:CompiledBugJavaWithJavaC'>编译失败;有关详细信息,请参阅编译器错误输出 我有一个类,它从OpenWeatherMap获取数据作为JSON对象,并在活动中显示它们 非活动类:Java android studio 3.0更新或错误代码导致的问题?,java,android,string,Java,Android,String,我尝试运行以下代码,但android studio一直说保存字符串对象有问题,但据我所知,代码编写正确。有人能看到问题所在并帮我解决吗? 在最新的更新之后,我在android studio上遇到了更多的问题,所以我很难确定这些问题是由于我的代码在某个地方出错还是由于更新 错误:(90,37)错误:找不到适合Intent(CityWeatherData,类)构造函数意图的构造函数。Intent(字符串,Uri)不适用(参数不匹配;CityWeatherData无法转换为字符串)构造函数意图。Int
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);
}
}
您的
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();
}
}
}
}
私有静态类CityWeatherData扩展异步任务{
私人WeakReference主要活动;
公共城市天气数据(主要活动上下文){
mainActivity=新的WeakReference(上下文);
}
@凌驾
受保护的字符串doInBackground(字符串…URL){
....
}
@凌驾
受保护的void onPostExecute(字符串结果){
super.onPostExecute(结果);
MainActivity cxt=MainActivity.get();
如果(cxt!=null){
//从结果创建JSON对象
试一试{
JSONObject JSONObject=新JSONObject(结果);
JSONObject weatherData=新的JSONObject(JSONObject.getString(“main”);
//我们感兴趣的数据位于“main”之后
//从主服务器获取温度
double temp=double.parseDouble(weatherData.getString(“temp”));
//温度为1开尔文,因此需要转换为Celcius
int tempInt=(int)(temp-273.15);
//获取城市名称
String placeName=jsonObject.getString(“名称”);
//获取描述
String weatherDescription=jsonObject.getString(“说明”);
//潮湿
double humidityValue=double.parseDouble(weatherData.getString(“湿度”));
Intent sendDataIntent=新的Intent(cxt,CityDetailsActivity.class);
sendDataIntent.putExtra(“tempData”,tempInt);
sendDataIntent.putExtra(“cityNameData”,地名);
sendDataIntent.putExtra(“humidityData”,humidityValue);
sendDataIntent.putExtra(“descriptionData”,weatherDescription);
startActivity(sendDataIntent);
}捕获(例外e){
e、 printStackTrace();
}
}
}
}
您的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();
}
}
}
}
私有静态类CityWeatherData扩展异步任务{
私人WeakReference主要活动;
公共城市天气数据(主要活动上下文){
mainActivity=新的WeakReference(上下文);
}
@凌驾
受保护的字符串doInBackground(字符串…URL){
....
}
@凌驾
受保护的void onPostExecute(字符串结果){
super.onPostExecute(结果);
MainActivity cxt=MainActivity.get();
如果(cxt!=null){
//从结果创建JSON对象
试一试{
JSONObject JSONObject=新JSONObject(结果);
JSONObject weatherData=新的JSONObject(JSONObject.getString(“main”);
//我们感兴趣的数据位于“main”之后
//从主服务器获取温度
double temp=double.parseDouble(weatherData.getString(“temp”));
//温度为1开尔文,因此需要转换为Celcius
int tempInt=(int)(temp-273.15);
//获取城市名称
String placeName=jsonObject.getString(“名称”);
//获取描述
String weatherDescription=jsonObject.getString(“说明”);
//潮湿
double humidityValue=double.parseDouble(weatherData.getString(“湿度”));
Intent sendDataIntent=新的Intent(cxt,CityDetailsActivity.class);
sendDataIntent.putExtra(“tempData”,tempInt);
sendDataIntent.putExtra(“cityNameData”,地名);
sendDataIntent.putExtra(“humidityData”,humidityValue);
sendDataIntent.putExtra(“descriptionData”,weatherDescription);
startActivity(sendDataIntent);
}捕获(例外e){
e、 printStackTrace();
}
}
}
}
什么问题?哪里请具体说明。保存字符串对象的问题是什么意思?错误消息是什么?发布准确的错误代码错误:(90,37)错误:找不到适合Intent(CityWeatherData,类)构造函数意图的构造函数。Intent(字符串,Uri)不适用(参数不匹配;CityWeatherData无法转换为字符串)构造函数意图。Intent(上下文,类)不适用(参数不匹配;CityWeatherData无法转换为上下文)错误:(95,13)错误:找不到符号方法startActivity(Intent)什么问题?在哪里?请在描述中具体说明。保存字符串对象的问题是什么意思?错误消息是什么?发布准确的错误代码错误:(90,37)错误:不适合