【问题标题】:Problems caused by android studio 3.0 update or bad code?android studio 3.0 更新或错误代码导致的问题?
【发布时间】:2017-11-01 09:11:38
【问题描述】:

我尝试运行以下代码,但 android studio 一直说保存字符串对象存在问题,但据我所知代码编写正确。谁能看到问题出在哪里并帮我解决? 在最新更新之后,我的 android studio 出现了更多问题,所以我很难确定问题是由于我的代码在某处错误还是由于更新造成的。

Error:(90, 37) 错误:没有为 Intent(CityWeatherData,Class) 找到合适的构造函数构造函数 Intent.Intent(String,Uri) 不适用(参数不匹配;CityWeatherData 无法转换为字符串)构造函数 Intent.Intent (Context,Class) 不适用(参数不匹配;CityWeatherData 无法转换为 Context)

Error:(95, 13) error: 找不到符号方法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);
    }
}

【问题讨论】:

  • 什么问题?在哪里?请在描述中具体说明。
  • problems with saving string objects 是什么意思?错误信息是什么?
  • 发布确切的错误代码
  • Error:(90, 37) 错误:没有找到适合 Intent(CityWeatherData,Class) 的构造函数 Intent.Intent(String,Uri) 不适用(参数不匹配;CityWeatherData 不能转换为字符串)构造函数 Intent.Intent(Context,Class>) 不适用(参数不匹配;CityWeatherData 无法转换为 Context)
  • 错误:(95, 13) 错误:找不到符号方法 startActivity(Intent)

标签: java android string


【解决方案1】:

您的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();
        }
      }
    }
}

【讨论】:

  • 啊,是的!谢谢!那个我错过了:)
  • 如果它解决了所提出的问题,请接受答案。
  • 您的错误是您在 AsyncTask 中使用了您的活动的context。始终使用对活动的弱引用以始终确保它被垃圾收集。我会更新我的答案。
  • 这回答了我的问题....我找不到我接受答案的地方虽然 xD
猜你喜欢
  • 2013-11-09
  • 2018-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-05
相关资源
最近更新 更多