当我旋转设备(Android)时,适配器变量接收null?

时间:2013-02-05 13:49:34

标签: android rotation save instance adapter

在旋转设备时,在下面的代码中(为了清楚起见,缩写),适配器变量接收null,并且gridView变量接收null。

任何人都可以帮助我在旋转设备屏幕后保留适配器和gridView吗?

感谢。

(抱歉英语不好)

    public class FlickrXmlFragment extends Fragment {

    private GridView gridView;
    private FlickrGridViewAdapter adapter;

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

        //...
        View view = darkInflater.inflate(R.layout.fragment_flickr_xml,
                container, false);
        //...
        gridView = (GridView) view.findViewById(R.id.flickr_gridView);

        return view;
    }

    class FlickrPhotoSearchThread extends
            AsyncTask<String, Void, List<FlickrImagem>> {

        @Override
        protected void onPreExecute() { //... process...    }

        @Override
        protected List<FlickrImagem> doInBackground(String... params) { //process... }

        @Override
        protected void onPostExecute(List<FlickrImagem> result) {
            //...
            adapter = new FlickrGridViewAdapter(getActivity(), 0, result);
            gridView.setAdapter(adapter);
            adapter.notifyDataSetChanged();
            //...
        }
    }
}

1 个答案:

答案 0 :(得分:-1)

处理您的活动的方向更改。

请参阅:http://developer.android.com/guide/topics/manifest/activity-element.html#config

声明你的活动:

<activity android:name=".MyActivity"
          android:configChanges="orientation|keyboardHidden"
          android:label="@string/app_name">