为什么分配的局部变量变为空

Why a assigned local variable become to null

onCreateView 中我调用了 getDataFromServer(String url),其中还有另一个函数叫做 processData(String son)。我设置局部变量:

String json = "" inside the processData(String son);

本来可以做的,但是当程序进入下一步到达initTopicNewsImages()时,它会突然变为空。

    public class NewsCenterFragment extends Fragment implements View.OnClickListener{
    private ViewPager viewPager;
    private ViewPager topicNews;
    private ArrayList<String> topicImages;
    //private Activity mActivity;
    private ArrayList<NewsMenu.NewsTabData> newsTabDatas;
    private ArrayList<View> views;
    private TabPageIndicator mIndicator;
    private ImageButton next_btn;
    private NewsInfo newsInfo;
    private BitmapUtils mBitmapUtil;
    private int counter=0;
    private boolean firstEnter = true;
    private String json = "";
    public NewsCenterFragment(ArrayList<NewsMenu.NewsTabData> datas){
        this.newsTabDatas = datas;

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){

     //   LayoutInflater inflater = (LayoutInflater) mActivity.getSystemService(mActivity.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.fragment_newscenter_layout,container,false);
        viewPager = (ViewPager) view.findViewById(R.id.fragment_newscenter_viewPager);
        topicNews = (ViewPager) view.findViewById(R.id.fragment_newscenter_viewPager_topicNews);

        //mBitmapUtil = new BitmapUtils(getActivity());

        mIndicator = (TabPageIndicator) view.findViewById(R.id.fragment_newscenter_tabPageIndicator);
        next_btn = (ImageButton) view.findViewById(R.id.fragment_newscenter_next);
        next_btn.setOnClickListener(this);
        views = new ArrayList<View>();
        //View tempLayout = inflater.inflate(R.layout.news_title_layout,container,false);


        for(int i=0;i<newsTabDatas.size();i++){
            View tempLayout = inflater.inflate(R.layout.news_title_layout,container,false);
            views.add(tempLayout);
        }

        viewPager.setAdapter(new NewsMenuDetailAdapter());
        mIndicator.setViewPager(viewPager);
        firstEnter = false;
        Log.d("newsTabDatas.get(0).url",newsTabDatas.get(0).url);
        getDataFromServer(newsTabDatas.get(0).url);
        initTopicNewsImages();
       //topicNews.setAdapter(new MyTopicNewsAdapter());
        return view;
   }

    @Override
    public void onClick(View v) {
        int currentPage = viewPager.getCurrentItem();
        viewPager.setCurrentItem(currentPage+1);
    }

    class NewsMenuDetailAdapter extends PagerAdapter{

        @Override
        public int getCount() {
            return views.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==(View) object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
                String tabTitle = newsTabDatas.get(position).title;
                View view = views.get(position);
                TextView textView = (TextView) view.findViewById(R.id.news_title_layout_title);
                textView.setText(tabTitle);
            Log.d("NewsCenterFragment", "Position " + position);
                //getDataFromServer(newsTabDatas.get(position).url);
                //initTopicNewsImages();
                //topicNews.setAdapter(new MyTopicNewsAdapter());
                container.addView(view);
                return view;

        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View)object);
        }

        @Override
        public CharSequence getPageTitle(int position) {

            return newsTabDatas.get(position).title;
        }
    }

    private void getDataFromServer(String url){
        HttpUtils httpUtils = new HttpUtils();

        httpUtils.send(HttpMethod.GET, GlobalConstans.SERVER_URL+url, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String resultJson = responseInfo.result;
                setJson(resultJson);
                Log.d("*************************",json);
                //CacheUtils.setCache(GlobalConstans.CATEGORY_URL,result,getActivity());
                processData(json);
            }

            @Override
            public void onFailure(HttpException e, String s) {
                e.printStackTrace();
                Log.d("Fail","fail");
                Toast.makeText(getActivity(),"Failure", Toast.LENGTH_SHORT).show();
            }
        });


    }
    private void processData(String json){
        Gson gson = new Gson();
        newsInfo = gson.fromJson(json, NewsInfo.class);
        //Log.d("Check empty",newsInfo.toString());
        Log.d("Geson3",newsInfo.data.topnews.toString());
        counter+=1;
        Log.d("counter",String.valueOf(counter));
        //Log.d("JSON in process",getJson());
    }

    private void initTopicNewsImages(){
        Log.d("JSON in process",getJson());
//        for(NewsInfo.TopNews topNews:newsInfo.data.topnews){
//            String topImage = topNews.topimage;
//            topicImages.add(topImage);
//        }
//        Gson gson = new Gson();
//        newsInfo = gson.fromJson(json, NewsInfo.class);



    }

    class MyTopicNewsAdapter extends PagerAdapter{

        @Override
        public int getCount() {
            return topicImages.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            String url = topicImages.get(position);
            ImageView view = new ImageView(getActivity());
            mBitmapUtil.display(view,url);
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }


    public String getJson() {
        return json;
    }

    public void setJson(String json) {
        this.json = json;
    }

    public NewsInfo getNewsInfo() {
        return newsInfo;
    }

    public void setNewsInfo(NewsInfo newsInfo) {
        this.newsInfo = newsInfo;
    }
}

您应该了解 asynchronous call 的工作原理。当您调用 httpUtils.send 时,它在后台线程上运行(关闭 UI 线程)。现在主线程上的 control 达到 initTopicNewsImages()(在调用 getDataFromServer(newsTabDatas.get(0).url) 之后),到那时后台线程还没有返回,因此 json 不是 尚未更新。

你应该做的是,在后台线程 returns 之后调用 initTopicNewsImages() 方法。 像下面这样的东西会起作用:

httpUtils.send(HttpMethod.GET, GlobalConstans.SERVER_URL+url, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String resultJson = responseInfo.result;
                setJson(resultJson);
                initTopicNewsImages(); //CALLING FROM HERE.
                Log.d("*************************",json);
                //CacheUtils.setCache(GlobalConstans.CATEGORY_URL,result,getActivity());
                processData(json);
            }

            @Override
            public void onFailure(HttpException e, String s) {
                e.printStackTrace();
                Log.d("Fail","fail");
                Toast.makeText(getActivity(),"Failure", Toast.LENGTH_SHORT).show();
            }
        });