android 中的两个指定整数字段在使用其他方法调用后丢失了指定值

Two fields of assigned integers losing their assigned value after being recalled on other method in android

大家好,我有一个奇怪的错误,我有两个 inttotalPagestotalResults,它们是在方法 getFilmographySizeAndPaginationSize() 中初始化的,如下所示和我使用 Log.v() 检查,它们已正确初始化,但是当我尝试在方法 onStart() 和方法 getFullFilmography(); 中重用 totalPagestotalResults 时,我尝试检查 Log.v() 他们都给出 = 0

我正在使用 OkHTTP 库,但我检查过 JSON 收到的是好的,然后我使用以下代码片段分配 totalPagestotalResults :

  JSONObject results = new JSONObject(jsonData);
                            int pages = results.getInt("total_pages");
                            totalPages = pages;

                            setTotalPages(pages);

                            int totalResult = results.getInt("total_results");
                            totalResults = totalResult;

                            setTotalResults(totalResult);

activityclass的相关片段:

    public class ActorFilmographyActivity extends AppCompatActivity {

        public static final String TAG = ActorFilmographyActivity.class.getSimpleName();

        @Bind(R.id.actorNameLabel)
        TextView actorLabel;

        @Bind(R.id.actorProfileImage)
        ImageView profileImage;

        @Bind(R.id.recyclerView)
        RecyclerView filmsRecyclerView;

        private Film[] filmography;
        private FilmographyAdapter adapter;

        private Actor actor;

        private static final String API_KEY = "0000000000";
        private Response response;
        public int totalResults;
        public int totalPages;


        public int getTotalPages() {
            return totalPages;
        }

        public void setTotalPages(int totalPages) {
            this.totalPages = totalPages;
        }

        public int getTotalResults() {
            return totalResults;
        }

        public void setTotalResults(int totalResults) {
            this.totalResults = totalResults;
        }


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

            ButterKnife.bind(this);

            Intent intent = getIntent();

            if (intent != null) {
                actor = (Actor) intent.getSerializableExtra("actor");
                actorLabel.setText(actor.getName().toString());
                loadActorProfilePicture();
                getFilmographySizeAndPaginationSize();
            }
        }

        @Override
        protected void onStart() {
            super.onStart();
            Log.v("ON START", totalPages + " " + totalResults);
            getFullFilmography();

        }

        private void getFullFilmography() {
                Toast.makeText(ActorFilmographyActivity.this,"getFullFilmography" + totalPages + " " + totalResults, Toast.LENGTH_SHORT).show();
            Log.v(TAG, "pager ");



        }


        private void getFilmographySizeAndPaginationSize() {
            String filmographyByActorIdUrl = "http://api.themoviedb.org/3/discover/movie?with_cast=" + actor.getId() + "&sort_by=release_date.asc&api_key=" + API_KEY + "&page=" + 1;

            OkHttpClient client = new OkHttpClient();
            final Request request = new Request.Builder()
                    .url(filmographyByActorIdUrl)
                    .build();

            Call call = client.newCall(request);

            call.enqueue(new Callback() {
                @Override
                public void onFailure(Request request, IOException e) {
                    alertUserAboutError();
                }

                @Override
                public void onResponse(Response response) throws IOException {

                    final String jsonData = response.body().string();
                    Log.v(TAG, "REST raw response for page " + 1 + jsonData);

                    try {
                        JSONObject results = new JSONObject(jsonData);
                        int pages = results.getInt("total_pages");
                        totalPages = pages;

                        setTotalPages(pages);

                        int totalResult = results.getInt("total_results");
                        totalResults = totalResult;

                        setTotalResults(totalResult);

                        Log.v(TAG, "total pages=" + totalPages + " and results= " + totalResults); //PRINTS total pages = XX and result= XX but then totalPages and totalResults print both 0 at method getFullFilmography(); on onStart() 

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
        }

//....
}

如您所见,正在使用方法 getFilmographySizeAndPaginationSize() 正确检索调试器 JSON 的检查:

但是当我在方法 getFullFilmography() 中使用调试器时,现在 totalPagestotalResults 都设置为 0。

感谢任何帮助!

如果操作系统调用了 onStart(...),您在 onCreate(...) 方法中开始执行的异步任务尚未完成。

尝试将放置在 onStart(...) 方法中的内容移动到一个方法中,该方法可以在您从异步 http 操作获得结果后调用。