Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SwipeRefreshLayout+AsyncTask:刷新数据失败_Java_Android_Android Asynctask_Swiperefreshlayout - Fatal编程技术网

Java SwipeRefreshLayout+AsyncTask:刷新数据失败

Java SwipeRefreshLayout+AsyncTask:刷新数据失败,java,android,android-asynctask,swiperefreshlayout,Java,Android,Android Asynctask,Swiperefreshlayout,我在应用程序中同时使用SwipeRefreshLayout和AsyncTask。我知道,如果要使用AsyncTask两次,我必须创建一个新实例来运行所有内容。但是,当我按照下面的方法执行此操作时,进程将被卡住,数据将不会被刷新。滑动按钮将永远持续 下面是Function.java: 我不熟悉SwipeRefreshLayout、java和Android,并且发现理解文档有困难。有人能帮我吗?提前感谢。内部处理完成。。执行refreshLayout.setRefreshingfalse 您也可以在

我在应用程序中同时使用SwipeRefreshLayout和AsyncTask。我知道,如果要使用AsyncTask两次,我必须创建一个新实例来运行所有内容。但是,当我按照下面的方法执行此操作时,进程将被卡住,数据将不会被刷新。滑动按钮将永远持续

下面是Function.java:


我不熟悉SwipeRefreshLayout、java和Android,并且发现理解文档有困难。有人能帮我吗?提前感谢。

内部处理完成。。执行refreshLayout.setRefreshingfalse

您也可以在一个函数中创建asynctask,这样就不会编写代码来实例化asnyctask两次


还可以使用日志或调试器调试代码,并告诉我

谢谢你的建议!运行应用程序时,我注意到在Logcats中有一些错误,但应用程序没有崩溃…例如TaskPersister:File error Access recents directory和BatteryExternalStatsWorker:modem信息无效与应用程序无关。
public interface AsyncResponse {

    void processFinish(String output1, String output2, String output3, String output4, String output5, String output6,
                       String output7, String output8, String output9, String output10, String output11, String output12,
                       String output13, String output14, String output15, String output16, String output17, String output18,
                       String output19, String output20, String output21, String output22, String output23, String output24);
}

public static class placeIdTask extends AsyncTask<String, Void, JSONObject> {

    public AsyncResponse delegate = null; //Call back interface

    public placeIdTask(AsyncResponse asyncResponse) {
        delegate = asyncResponse; //Assigning call back interface through constructor
    }

    @Override
    protected JSONObject doInBackground(String... params) {

        JSONObject jsonWeather = null;
        try {
            jsonWeather = getWeatherJSON();
        } catch (Exception e) {
            Log.d("Error", "Cannot process JSON results", e);
        }
        return jsonWeather;
    }

    @Override
    protected void onPostExecute(JSONObject json) {
        try {
            if (json != null) {
                ...... // more code
                delegate.processFinish(description, temperature, humidity, windspeed, updatedOn, iconText,
                        first_hour_icon, first_hour_text, first_hour_temperature,
                        third_hour_icon, third_hour_text, third_hour_temperature,
                        ninth_hour_icon, ninth_hour_text, ninth_hour_temperature,
                        first_day_icon, first_day_text, first_day_temperature,
                        second_day_icon, second_day_text, second_day_temperature,
                        third_day_icon, third_day_text, third_day_temperature);

            }
        } catch (JSONException e) {
            Log.e("Json Procession failed", e.toString());
        }
    }
}
public static JSONObject getWeatherJSON() { ... } // to retrieve data from the Internet
... get everything imported

public class MainActivity extends AppCompatActivity {

... Variables declared

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_main);

        weatherFont = Typeface.createFromAsset(getApplicationContext().getAssets(), "fonts/weathericons-regular-webfont.ttf");

        ...use findviewbyid to get things working

        final Function.placeIdTask asyncTask =new Function.placeIdTask(new Function.AsyncResponse() {
            public void processFinish(String weather_description, String weather_temperature, String weather_humidity, String weather_windspeed, String weather_updatedOn, String weather_iconText,
                                      String weather_firstHourIcon, String weather_firstHourText, String weather_firstHourTemperature,
                                      String weather_thirdHourIcon, String weather_thirdHourText, String weather_thirdHourTemperature,
                                      String weather_ninthHourIcon, String weather_ninthHourText, String weather_ninthHourTemperature,
                                      String weather_firstDayIcon, String weather_firstDayText, String weather_firstDayTemperature,
                                      String weather_secondDayIcon, String weather_secondDayText, String weather_secondDayTemperature,
                                      String weather_thirdDayIcon, String weather_thirdDayText, String weather_thirdDayTemperature) {

                updatedField.setText(weather_updatedOn);
                detailsField.setText(weather_description);
                currentTemperatureField.setText(weather_temperature);
                humidity_field.setText("Humidity: " + weather_humidity);
                windspeed_field.setText("Wind Speed: " + weather_windspeed);
                firstHourText.setText(weather_firstHourText);
                thirdHourText.setText(weather_thirdHourText);
                ninthHourText.setText(weather_ninthHourText);
                firstHourTemperature.setText(weather_firstHourTemperature);
                thirdHourTemperature.setText(weather_thirdHourTemperature);
                ninthHourTemperature.setText(weather_ninthHourTemperature);
                firstDayText.setText(weather_firstDayText);
                secondDayText.setText(weather_secondDayText);
                thirdDayText.setText(weather_thirdDayText);
                firstDayTemperature.setText(weather_firstDayTemperature);
                secondDayTemperature.setText(weather_secondDayTemperature);
                thirdDayTemperature.setText(weather_thirdDayTemperature);
                weatherIcon.setText(Html.fromHtml(weather_iconText));
                firstHourIcon.setText(Html.fromHtml(weather_firstHourIcon));
                thirdHourIcon.setText(Html.fromHtml(weather_thirdHourIcon));
                ninthHourIcon.setText(Html.fromHtml(weather_ninthHourIcon));
                firstDayIcon.setText(Html.fromHtml(weather_firstDayIcon));
                secondDayIcon.setText(Html.fromHtml(weather_secondDayIcon));
                thirdDayIcon.setText(Html.fromHtml(weather_thirdDayIcon));
            }
        });

        asyncTask.execute();

        refreshLayout = (SwipeRefreshLayout)findViewById(R.id.refresh_layout);
        scrollView = (NestedScrollView)findViewById(R.id.nested_scroll_view);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                Function.placeIdTask asyncTask2 =new Function.placeIdTask(new Function.AsyncResponse() {
                    public void processFinish(String weather_description, String weather_temperature, String weather_humidity, String weather_windspeed, String weather_updatedOn, String weather_iconText,
                                              String weather_firstHourIcon, String weather_firstHourText, String weather_firstHourTemperature,
                                              String weather_thirdHourIcon, String weather_thirdHourText, String weather_thirdHourTemperature,
                                              String weather_ninthHourIcon, String weather_ninthHourText, String weather_ninthHourTemperature,
                                              String weather_firstDayIcon, String weather_firstDayText, String weather_firstDayTemperature,
                                              String weather_secondDayIcon, String weather_secondDayText, String weather_secondDayTemperature,
                                              String weather_thirdDayIcon, String weather_thirdDayText, String weather_thirdDayTemperature) {

                        updatedField.setText(weather_updatedOn);
                        detailsField.setText(weather_description);
                        currentTemperatureField.setText(weather_temperature);
                        humidity_field.setText("Humidity: " + weather_humidity);
                        windspeed_field.setText("Wind Speed: " + weather_windspeed);
                        firstHourText.setText(weather_firstHourText);
                        thirdHourText.setText(weather_thirdHourText);
                        ninthHourText.setText(weather_ninthHourText);
                        firstHourTemperature.setText(weather_firstHourTemperature);
                        thirdHourTemperature.setText(weather_thirdHourTemperature);
                        ninthHourTemperature.setText(weather_ninthHourTemperature);
                        firstDayText.setText(weather_firstDayText);
                        secondDayText.setText(weather_secondDayText);
                        thirdDayText.setText(weather_thirdDayText);
                        firstDayTemperature.setText(weather_firstDayTemperature);
                        secondDayTemperature.setText(weather_secondDayTemperature);
                        thirdDayTemperature.setText(weather_thirdDayTemperature);
                        weatherIcon.setText(Html.fromHtml(weather_iconText));
                        firstHourIcon.setText(Html.fromHtml(weather_firstHourIcon));
                        thirdHourIcon.setText(Html.fromHtml(weather_thirdHourIcon));
                        ninthHourIcon.setText(Html.fromHtml(weather_ninthHourIcon));
                        firstDayIcon.setText(Html.fromHtml(weather_firstDayIcon));
                        secondDayIcon.setText(Html.fromHtml(weather_secondDayIcon));
                        thirdDayIcon.setText(Html.fromHtml(weather_thirdDayIcon));
                    }
                });

                asyncTask2.execute();
                refreshLayout.setRefreshing(true);

            }
        });

    }
}