我正在创建新闻应用程序,我在内部片段中有Gridview,并且在配置更改时我实现了onSaveInstanceState和onRestoreInstanceState。
低于我得到的错误
致命的例外:主要 过程:com.edgar.yodgorbekkomilo.newsapp,PID:5676 java.lang.NullPointerException:尝试调用虚方法' android.os.Parcelable android.widget.GridView.onSaveInstanceState()'在null对象引用上 在com.edgar.yodgorbekkomilo.newsapp.AllNewsFragmentTab.onCreateView(AllNewsFragmentTab.java:53) 在android.support.v4.app.Fragment.performCreateView(Fragment.java:2354) 在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419) 在android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740) 在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809) 在android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:799) 在android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2580) 在android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2367) 在android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322) 在android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:2199) 在android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:651) 在android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:167) 在android.support.v4.view.ViewPager.populate(ViewPager.java:1236) 在android.support.v4.view.ViewPager.populate(ViewPager.java:1084) 在android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1614) 在android.view.View.measure(View.java:22002) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580) 在android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:714) 在android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:786) 在android.view.View.measure(View.java:22002) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:185) 在android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139) 在android.view.View.measure(View.java:22002) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580) 在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514) 在android.widget.LinearLayout.measureVertical(LinearLayout.java:806) 在android.widget.LinearLayout.onMeasure(LinearLayout.java:685) 在android.view.View.measure(View.java:22002) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:185) 在android.view.View.measure(View.java:22002) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580) 在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514) 在android.widget.LinearLayout.measureVertical(LinearLayout.java:806) 在android.widget.LinearLayout.onMeasure(LinearLayout.java:685) 在android.view.View.measure(View.java:22002) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:185) 在com.android.internal.policy.DecorView.onMeasure(DecorView.java:721) 在android.view.View.measure(View.java:22002) 在android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2410) 在android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1498) 在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1751) 在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1386) 在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:6733) 在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:911) 在android.view.Choreographer.doCallbacks(Choreographer.java:723) 在android.view.Choreographer.doFrame(Choreographer.java:658) 在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:897) 在android.os.Handler.handleCallback(Handler.java:789) 03-17 20:12:26.358 5676-5676 / com.edgar.yodgorbekkomilo.newsapp E / AndroidRuntime:at android.os.Handler.dispatchMessage(Handler.java:98) 在android.os.Looper.loop(Looper.java:164) 在android.app.ActivityThread.main(ActivityThread.java:6541) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 在我的Fragment下面,我实现了配置更改
公共类AllNewsFragmentTab扩展了Fragment {
public ArrayList<News> articleList;
ArrayList<Article> articleArrayList = new ArrayList<>();
public GridView gridView;
private View parentView;
private ArticleAdapter adapter;
Parcelable state;
// ...
//恢复索引和位置
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.all_news_fragment_tab, container, false);
gridView = view.findViewById(R.id.listView);
articleList = new ArrayList<>();
parentView = view.findViewById(R.id.parentLayout);
state = gridView.onSaveInstanceState();
/**
* Getting List and Setting List Adapter
*
*
*/
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Snackbar.make(parentView,articleArrayList.get(position).getStatus()+&#34; =&gt;&#34; + articleArrayList.get(position).getTotalResults(),Snackbar.LENGTH_LONG)。节目(); Intent i = new Intent(getContext(),NewsDetailActivity.class);
i.putExtra("myDataKey", articleArrayList.get(position));
startActivity(i);
}
});
Toast toast = Toast.makeText(getActivity().getApplicationContext(), R.string.string_click_to_load, Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
/**
* Checking Internet Connection
*/
if (InternetConnection.checkConnection(getActivity().getApplicationContext())) {
final ProgressDialog dialog;
/**
* Progress Dialog for User Interaction
*/
dialog = new ProgressDialog(getActivity());
dialog.setTitle(getString(R.string.string_getting_json_title));
dialog.setMessage(getString(R.string.string_getting_json_message));
dialog.dismiss();
//Creating an object of our api interface
ApiService api = RetroClient.getApiService();
/**
* Calling JSON
*/
Call<News> call = api.getAllNews();
/**
* Enqueue Callback will be call when get response...
*/
call.enqueue(new Callback<News>() {
@Override
public void onResponse(Call<News> call, Response<News> response) {
//Dismiss Dialog
dialog.dismiss();
if (response.isSuccessful()) {
/**
* Got Successfully
*/
// String articleList = String.valueOf(response.body());
News news = response.body();
articleArrayList.addAll(news.getArticles());
/**
* Binding that List to Adapter
*/adapter = new ArticleAdapter(getActivity(), articleArrayList);
gridView.setAdapter(adapter);
if(state != null) {
gridView.onRestoreInstanceState(state);
}
} else {
Snackbar.make(parentView, R.string.string_some_thing_wrong, Snackbar.LENGTH_LONG).show();
}
}
@Override
public void onFailure(Call<News> call, Throwable t) {
dialog.dismiss();
}
});
} else {
Snackbar.make(parentView, R.string.string_internet_connection_not_available, Snackbar.LENGTH_LONG).show();
}
return view;
}
}
below my all_news_fragment_tab.xml where I have declared my fragment
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textAppearance="?android:attr/textAppearanceLarge" />
<GridView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</RelativeLayout>