Home Second button in Activity doesn´t react after started AsyncTask
Reply: 0

Second button in Activity doesn´t react after started AsyncTask

Adam Lasák
1#
Adam Lasák Published in 2017-12-06 16:03:39Z

The problem is simple. In my activity when I press the confirm button I take the data from input boxes, then I check internet connection a then I call AsyncTask which downloads data from weathers API. All works fine, data are showed in output textbox.

But when I press another button after download (show next activity with graphs) it doesn't works. I'am sending downloaded data to next activity with Bundle. Nothing reactions from buttons, It looks like freeze.

Here's my buttons listener:

@Override
public void onClick(View v) {

    if (v.getId() == R.id.buttonGraph){
        Intent intent = new Intent(MainActivity.this, GraphActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("result_", (Serializable) this.result_);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    if (v.getId() == R.id.buttonConfirm){
        EditText city_ = (EditText)findViewById(R.id.editTextCity);
        EditText countryCode_ = (EditText)findViewById(R.id.editTextCountry);
        city = city_.getText().toString();
        countryCode = countryCode_.getText().toString();
        if (refreshDisplay) {
            loadPage();
        }
    }

}

Load page method:

private void loadPage() {
    if (((sPref.equals(ANY)) && (wifiConnected || mobileConnected))
            || ((sPref.equals(WIFI)) && (wifiConnected))) {
        // AsyncTask subclass
        new DownloadJsonTask(this).execute(URL + city + "," + countryCode + "&units=metric" + API_KEY);
    } else {
        showErrorPage();
        updateConnectedFlags();
    }
}

And finally Async class:

private class DownloadJsonTask extends AsyncTask<String, Void, WeatherEntry.WeatherObject> {

    Context context;

    public DownloadJsonTask(Context context) {
        this.context = context;
    }

    @Override
    protected WeatherEntry.WeatherObject doInBackground(String... urls) {
        try {
            if (isCancelled()) return null;
            return loadJsonFromNetwork(urls[0]);
        } catch (IOException e) {
            return null;
        }
    }

    @Override
    protected void onPostExecute(WeatherEntry.WeatherObject result) {
        setContentView(R.layout.activity_main);

        TextView text = (TextView)findViewById(R.id.textView);
        TextView textOutput = (TextView)findViewById(R.id.textView2);

        if (result == null){
            text.setText("Unable to load data. Check your internet connection");
        } else {
            text.setText("Data was successfully loaded.");

            result_ = result;
            weather_ = result.listMain.get(0);
            String outputText = "Actual temperature: " + weather_.listMain.temp + " deg. C\n" +
                    "Today min. temperature: " + weather_.listMain.temp_min + " deg. C\n" +
                    "Today max. temperature: " + weather_.listMain.temp_max + " deg. C\n" +
                    "Pressure: " + weather_.listMain.pressure + " hPa\n" +
                    "Humidity: " + weather_.listMain.humidity + " %\n" +
                    "Weather: " + weather_.weather.main + "\n" +
                    "Description: " + weather_.weather.description + "\n" +
                    "Cloudiness: " + weather_.clouds + " %\n" +
                    "Wind speed: " + weather_.wind.speed + " m/s\n" +
                    "Last update: " + weather_.dt_txt;
            textOutput.setText(outputText);
        }

    }
}

I am not so Java skilled so if you can help me, I'll be happy.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.305611 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO