在android中的其他方法被召回后,两个分配的整数字段失去了它们的赋值

时间:2015-08-03 16:00:00

标签: android android-activity android-lifecycle okhttp android-json

嘿伙计我得到了一个奇怪的错误我有两个inttotalPagestotalResults,它们已在方法getFilmographySizeAndPaginationSize()中初始化,因为您可以看到以下情况我检查过Log.v()并且它们正确初始化但是当我尝试在方法totalPages和方法totalResults中重用onStart()getFullFilmography();时,我尝试使用日志进行检查.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);

活动类的相关摘要:

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

//....
}

正如您所看到的那样,使用调试器检查JSON正在方法getFilmographySizeAndPaginationSize()上正确检索: enter image description here

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

感谢任何帮助!

enter image description here

1 个答案:

答案 0 :(得分:1)

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

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