<i id='qlNr8'><tr id='qlNr8'><dt id='qlNr8'><q id='qlNr8'><span id='qlNr8'><b id='qlNr8'><form id='qlNr8'><ins id='qlNr8'></ins><ul id='qlNr8'></ul><sub id='qlNr8'></sub></form><legend id='qlNr8'></legend><bdo id='qlNr8'><pre id='qlNr8'><center id='qlNr8'></center></pre></bdo></b><th id='qlNr8'></th></span></q></dt></tr></i><div id='qlNr8'><tfoot id='qlNr8'></tfoot><dl id='qlNr8'><fieldset id='qlNr8'></fieldset></dl></div>
<tfoot id='qlNr8'></tfoot>
    1. <small id='qlNr8'></small><noframes id='qlNr8'>

      <legend id='qlNr8'><style id='qlNr8'><dir id='qlNr8'><q id='qlNr8'></q></dir></style></legend>

          <bdo id='qlNr8'></bdo><ul id='qlNr8'></ul>

        Android getSupportFragmentManager Fragment中的NULL指针异常

        时间:2023-10-03
          <bdo id='ZRoeQ'></bdo><ul id='ZRoeQ'></ul>

            <i id='ZRoeQ'><tr id='ZRoeQ'><dt id='ZRoeQ'><q id='ZRoeQ'><span id='ZRoeQ'><b id='ZRoeQ'><form id='ZRoeQ'><ins id='ZRoeQ'></ins><ul id='ZRoeQ'></ul><sub id='ZRoeQ'></sub></form><legend id='ZRoeQ'></legend><bdo id='ZRoeQ'><pre id='ZRoeQ'><center id='ZRoeQ'></center></pre></bdo></b><th id='ZRoeQ'></th></span></q></dt></tr></i><div id='ZRoeQ'><tfoot id='ZRoeQ'></tfoot><dl id='ZRoeQ'><fieldset id='ZRoeQ'></fieldset></dl></div>

              <tfoot id='ZRoeQ'></tfoot>
            1. <small id='ZRoeQ'></small><noframes id='ZRoeQ'>

                  <legend id='ZRoeQ'><style id='ZRoeQ'><dir id='ZRoeQ'><q id='ZRoeQ'></q></dir></style></legend>
                    <tbody id='ZRoeQ'></tbody>
                  本文介绍了Android getSupportFragmentManager Fragment中的NULL指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  单击导航抽屉项目后,我正在尝试使用 TabBar 和 View Pager 显示新片段.

                  i'm trying to display new fragment with TabBar and View Pager after click on the Navigation drawer item.

                  问题是新创建的 Fragment 导致 NULL 指针异常

                  Problem is that newly created Fragment is causing NULL pointer exception on

                  viewPager.setAdapter(mAdapter);
                  

                  我被困在 Fragment 中带有标签的可滑动视图几个小时但没有运气,有人可以帮我解决这个问题吗?

                  I'm stucked on swipeable-views with tabs in Fragment a several hours but without luck, can somebody helps me with this problem please?

                  对于可滑动视图,我遵循了本教程:

                  For swipable views i followed this tutorial:

                  http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/

                  片段代码在这里:

                  import android.app.ActionBar;
                  import android.support.v4.app.Fragment;
                  import android.app.FragmentTransaction;
                  import android.os.Bundle;
                  import android.support.v4.view.ViewPager;
                  import android.view.LayoutInflater;
                  import android.view.View;
                  import android.view.ViewGroup;
                  import eu.com.mycompany.adapter.TabsPagerAdapter;
                  
                  public class Fragment_AboutApplication extends Fragment implements ActionBar.TabListener {
                  
                      public Fragment_AboutApplication(){}
                      private ViewPager viewPager;
                      private TabsPagerAdapter mAdapter;
                      private ActionBar actionBar;
                      // Tab titles
                      private String[] tabs = { "Top Rated", "Games", "Movies" };
                  
                      @Override
                      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                              Bundle savedInstanceState) {
                  
                          View rootView = inflater.inflate(R.layout.fragment_about_app, container, false);
                  
                  
                          // Initilization
                          viewPager = (ViewPager) getActivity().findViewById(R.id.pager);
                          actionBar = getActivity().getActionBar();
                          mAdapter = new TabsPagerAdapter(getActivity().getSupportFragmentManager());
                  
                          /*HERE IS THROWED NULL POINTER EXCEPTION*/ 
                          viewPager.setAdapter(mAdapter);
                          actionBar.setHomeButtonEnabled(false);
                          actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
                  
                  
                          // Adding Tabs
                          for (String tab_name : tabs) {
                              actionBar.addTab(actionBar.newTab().setText(tab_name)
                                      .setTabListener(this));
                          }
                          return rootView;
                      }
                  
                      @Override
                      public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
                  
                      }
                  
                      @Override
                      public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
                  
                      }
                  
                      @Override
                      public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
                  
                      }
                  }
                  

                  类 TabPagerAdapter:

                  public class TabsPagerAdapter extends FragmentPagerAdapter {
                  
                      public TabsPagerAdapter(FragmentManager fm) {
                          super(fm);
                      }
                  
                      @Override
                      public Fragment getItem(int index) {
                  
                          switch (index) {
                              case 0:
                                  // Top Rated fragment activity
                                  return new Fragment_TopRated();
                              case 1:
                                  // Games fragment activity
                                  return new Fragment_TopRated();
                              case 2:
                                  // Movies fragment activity
                                  return new Fragment_TopRated();
                          }
                  
                          return null;
                      }
                  
                      @Override
                      public int getCount() {
                          // get item count - equal to number of tabs
                          return 3;
                      }
                  
                  }
                  

                  使用 View Pager 的主要片段布局

                  <?xml version="1.0" encoding="utf-8"?>
                  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                      android:layout_width="match_parent"
                      android:layout_height="match_parent">
                  
                      <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
                          android:id="@+id/pager"
                          android:layout_width="match_parent"
                          android:layout_height="match_parent">
                      </android.support.v4.view.ViewPager>
                  
                      <TextView
                          android:id="@+id/txtLabel"
                           android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:layout_centerInParent="true"
                          android:textSize="16dp"
                          android:text="Settings"/>
                  
                      <ImageView android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:layout_below="@id/txtLabel"
                          android:src="@drawable/ic_communities"
                          android:layout_centerHorizontal="true"
                          android:layout_marginTop="10dp"/>    
                  
                  </RelativeLayout>
                  

                  例外情况是:

                  E/AndroidRuntime﹕ FATAL EXCEPTION: main
                      java.lang.NullPointerException
                      at android.support.v4.app.Fragment.performCreateView(Fragment.java:1504)
                                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:942)
                                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
                                  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
                                  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
                                  at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:450)
                                  at android.os.Handler.handleCallback(Handler.java:725)
                                  at android.os.Handler.dispatchMessage(Handler.java:92)
                                  at android.os.Looper.loop(Looper.java:153)
                                  at android.app.ActivityThread.main(ActivityThread.java:5330)
                                  at java.lang.reflect.Method.invokeNative(Native Method)
                                  at java.lang.reflect.Method.invoke(Method.java:511)
                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
                                  at dalvik.system.NativeStart.main(Native Method)
                  

                  推荐答案

                  更改:

                  mAdapter = new TabsPagerAdapter(getActivity().getSupportFragmentManager())
                  

                  到:

                   mAdapter = new TabsPagerAdapter(getChildFragmentManager())
                  

                  当您在另一个片段中使用片段时,您必须使用 ChildFragmentManager 而不是 FragmentManager.

                  when you are using fragment inside another fragment you must use ChildFragmentManager not FragmentManager.

                  这篇关于Android getSupportFragmentManager Fragment中的NULL指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在 NestedScrollView 中使用 RecyclerView 制作 ViewPager 下一篇:想使用ViewPager,无法识别android.support.*?

                  相关文章

                1. <legend id='GprWU'><style id='GprWU'><dir id='GprWU'><q id='GprWU'></q></dir></style></legend>
                  <tfoot id='GprWU'></tfoot>
                2. <i id='GprWU'><tr id='GprWU'><dt id='GprWU'><q id='GprWU'><span id='GprWU'><b id='GprWU'><form id='GprWU'><ins id='GprWU'></ins><ul id='GprWU'></ul><sub id='GprWU'></sub></form><legend id='GprWU'></legend><bdo id='GprWU'><pre id='GprWU'><center id='GprWU'></center></pre></bdo></b><th id='GprWU'></th></span></q></dt></tr></i><div id='GprWU'><tfoot id='GprWU'></tfoot><dl id='GprWU'><fieldset id='GprWU'></fieldset></dl></div>

                    <bdo id='GprWU'></bdo><ul id='GprWU'></ul>

                  1. <small id='GprWU'></small><noframes id='GprWU'>