在哪里调用getFirstVisiblePosition和setPosition?

时间:2017-03-08 16:19:55

标签: java android

目前在我的Android应用中旋转手机屏幕时试图保存位置。目前我仍然得到一个NullPointerException,因为我不知道在哪里调用getFirstVisiblePosition和setPosition。它可能应该在onSavedInstance和onCreate中的某处,但如果我在oncreate中调用setPosition,我会得到一个NullpointerException。 这是我的代码:

public class MovieGridFragment extends Fragment {

public clickInterfaceHelper clickListener;
private int index;
private GridView movieGridView;
public List<movieData> movieDataList = new ArrayList<>();

public MovieGridFragment() {} //empty constructor

@Override
public void onAttach(Context context) {
    this.clickListener = (clickInterfaceHelper) context;
    super.onAttach(context);
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    if(savedInstanceState != null) {
        if (!movieDataList.isEmpty()) {
            movieDataList = Arrays.asList((movieData[]) savedInstanceState.getSerializable("OLDMOVIEDATA"));
        }
    }
    super.onCreate(savedInstanceState);
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    setHasOptionsMenu(true);


    View rootView = inflater.inflate(R.layout.movie_display_fragment, container, false);

    movieGridView = (GridView) rootView.findViewById(R.id.gv_movie_display);
    movieAdapter adapter = new movieAdapter(getActivity(),movieDataList);
    adapter.notifyDataSetChanged();
    movieGridView.setAdapter(adapter);
    movieGridView.setSelection(index);
    movieGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if(clickListener != null)
                    clickListener.clickOnItem(position);

        }
    });
    return rootView;
}

@Override
public void onSaveInstanceState(Bundle outState) {
    index = movieGridView.getFirstVisiblePosition();
    outState.putSerializable("OLDMOVIEDATA",movieData.movieDataArray);
    super.onSaveInstanceState(outState);
}}

logcat的:

                                                                                                                                                                    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.popularmoviesapp/com.example.android.popularmoviesapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.GridView.setSelection(int)' on a null object reference
                                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                                                                                    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4483)
                                                                                    at android.app.ActivityThread.-wrap19(ActivityThread.java)
                                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1466)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                    at android.os.Looper.loop(Looper.java:154)
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                                                                                 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.GridView.setSelection(int)' on a null object reference
                                                                                    at com.example.android.popularmoviesapp.MovieGridFragment.onCreate(MovieGridFragment.java:46)
                                                                                    at android.support.v4.app.Fragment.performCreate(Fragment.java:2172)
                                                                                    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1243)
                                                                                    at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1523)
                                                                                    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1585)
                                                                                    at android.support.v4.app.FragmentManagerImpl.dispatchCreate(FragmentManager.java:2827)
                                                                                    at android.support.v4.app.FragmentController.dispatchCreate(FragmentController.java:190)
                                                                                    at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:353)
                                                                                    at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:88)
                                                                                    at com.example.android.popularmoviesapp.MainActivity.onCreate(MainActivity.java:46)
                                                                                    at android.app.Activity.performCreate(Activity.java:6662)
                                                                                    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
                                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
                                                                                    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4483) 
                                                                                    at android.app.ActivityThread.-wrap19(ActivityThread.java) 
                                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1466) 
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                    at android.os.Looper.loop(Looper.java:154) 
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:6077) 
                                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

1 个答案:

答案 0 :(得分:0)

您可能需要将超级调用移动到每个函数内的第一行。然后尝试在onCreate:

的末尾添加它
movieGridView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                      movieGridView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    //call setPosition here
                }
            });