<small id='hqhnV'></small><noframes id='hqhnV'>

    1. <tfoot id='hqhnV'></tfoot>

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

        由于导航回旧片段,查看 Pager Crash Null Pointer Exception - Android

        时间:2023-10-04

          <tbody id='NatiR'></tbody>

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

                <small id='NatiR'></small><noframes id='NatiR'>

                1. <legend id='NatiR'><style id='NatiR'><dir id='NatiR'><q id='NatiR'></q></dir></style></legend>
                2. <tfoot id='NatiR'></tfoot>

                3. 本文介绍了由于导航回旧片段,查看 Pager Crash Null Pointer Exception - Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在使用视图寻呼机在页面之间导航.我正在使用 FragmentStatePagerAdapter.我可以毫无问题地向一个方向滑动.但如果我刷回旧的.它正在崩溃.我收到空指针异常.

                  I am using view pager for navigating between pages. I am using FragmentStatePagerAdapter. I can swipe in one direction with out any problems. But if i swipe back to the older one. Its getting crash. I am getting null pointer exception.

                  03-12 10:20:34.273: E/AndroidRuntime(29035): Process: com.titlesource.notarymobile, PID: 29035
                  03-12 10:20:34.273: E/AndroidRuntime(29035): java.lang.NullPointerException
                  03-12 10:20:34.273: E/AndroidRuntime(29035):    at android.support.v4.app.Fragment.instantiate(Fragment.java:399)
                  03-12 10:20:34.273: E/AndroidRuntime(29035):    at android.support.v4.app.FragmentState.instantiate(Fragment.java:97)
                  03-12 10:20:34.273: E/AndroidRuntime(29035):    at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1790)
                  03-12 10:20:34.273: E/AndroidRuntime(29035):    at android.support.v4.app.Fragment.performCreate(Fragment.java:1489)
                  03-12 10:20:34.273: E/AndroidRuntime(29035):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:893)
                  03-12 10:20:34.273: E/AndroidRuntime(29035):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
                  03-12 10:20:34.273: E/AndroidRuntime(29035):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
                  03-12 10:20:34.273: E/AndroidRuntime(29035):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
                  03-12 10:20:34.273: E/AndroidRuntime(29035):    at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
                  03-12 10:20:34.273: E/AndroidRuntime(29035):    at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
                  03-12 10:20:34.273: E/AndroidRuntime(29035):    at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
                  03-12 10:20:34.273: E/AndroidRuntime(29035):    at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
                  03-12 10:20:34.273: E/AndroidRuntime(29035):    at android.support.v4.view.ViewPager$3.run(ViewPager.java:244)
                  03-12 10:20:34.273: E/AndroidRuntime(29035):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
                  03-12 10:20:34.273: E/AndroidRuntime(29035):    at android.view.Choreographer.doCallbacks(Choreographer.java:574)
                  03-12 10:20:34.273: E/AndroidRuntime(29035):    at android.view.Choreographer.doFrame(Choreographer.java:543)
                  03-12 10:20:34.273: E/AndroidRuntime(29035):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
                  03-12 10:20:34.273: E/AndroidRuntime(29035):    at android.os.Handler.handleCallback(Handler.java:733)
                  03-12 10:20:34.273: E/AndroidRuntime(29035):    at android.os.Handler.dispatchMessage(Handler.java:95)
                  03-12 10:20:34.273: E/AndroidRuntime(29035):    at android.os.Looper.loop(Looper.java:136)
                  03-12 10:20:34.273: E/AndroidRuntime(29035):    at android.app.ActivityThread.main(ActivityThread.java:5102)
                  03-12 10:20:34.273: E/AndroidRuntime(29035):    at java.lang.reflect.Method.invokeNative(Native Method)
                  03-12 10:20:34.273: E/AndroidRuntime(29035):    at java.lang.reflect.Method.invoke(Method.java:515)
                  03-12 10:20:34.273: E/AndroidRuntime(29035):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
                  03-12 10:20:34.273: E/AndroidRuntime(29035):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                  03-12 10:20:34.273: E/AndroidRuntime(29035):    at dalvik.system.NativeStart.main(Native Method)
                  

                  这是我的代码:

                  @Override
                  protected void onCreate(Bundle savedInstanceState) {
                      super.onCreate(savedInstanceState);
                      setContentView(R.layout.activity_page_view);
                      mProgressDialog = new ProgressDialog(JobDetailsActivity.this);
                      mProgressDialog.setMessage("Loading");
                      Bundle bundle = getIntent().getExtras();
                      position = bundle.getInt("list_posiition_details");
                          List<Fragment> fragments = getFragments();
                          pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);
                  
                          pager = (ViewPager)findViewById(R.id.viewpager);
                          pager.setAdapter(pageAdapter);
                          pager.setCurrentItem(position);
                          pager.setOffscreenPageLimit(3);
                  }
                  
                  
                  public interface YourFragmentInterface {
                      void fragmentBecameVisible();
                  }
                  
                   private List<Fragment> getFragments(){
                          List<Fragment> fList = new ArrayList<Fragment>();
                  
                          final ArrayList<?> temp = DataModel.getScheduleList();
                  
                           for (final Object obj : temp) {
                               if(obj instanceof Job) {
                                   String a = ((Job) obj).getA();
                                   String b = Long.toString(((Job) obj).getB());
                                   String c = ((Job) obj).getC();
                                   fList.add(JobDetailsFragment_.newInstance(a, b, c));
                                   mProgressDialog.dismiss();
                               }
                           }
                          return fList;
                      }
                  
                  
                    public class MyPageAdapter extends FragmentStatePagerAdapter {
                          private List<Fragment> fragments;
                  
                          public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) {
                              super(fm);
                              this.fragments = fragments;
                          }
                          @Override
                          public Fragment getItem(int position) {
                              return this.fragments.get(position);
                          }
                  
                          @Override
                          public int getCount() {
                              return this.fragments.size();
                          }
                  
                          @Override
                          public int getItemPosition(Object object) {
                              return POSITION_NONE;
                          }  
                  
                      }
                  

                  这是我的片段类:

                  public class MyFragment extends Fragment {
                  public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";
                  
                  
                  private static View view;
                  /**
                   * Note that this may be null if the Google Play services APK is not
                   * available.
                   */
                  
                  SupportMapFragment mSupportMapFragment;
                  private GoogleMap googleMap;
                  
                  public static final MyFragment newInstance(String message)
                  {
                      MyFragment f = new MyFragment();
                      Bundle bdl = new Bundle(1);
                      bdl.putString(EXTRA_MESSAGE, message);
                      f.setArguments(bdl);
                      return f;
                  }
                  //  View view;
                  
                  @Override
                  public View onCreateView(LayoutInflater inflater, ViewGroup container, 
                      Bundle savedInstanceState) {
                      String message = getArguments().getString(EXTRA_MESSAGE);
                  
                       if (view != null) {
                              ViewGroup parent = (ViewGroup) view.getParent();
                              if (parent != null)
                                  parent.removeView(view);
                          }
                          try {
                              view = inflater.inflate(R.layout.myfragment_layout, container, false);
                              TextView messageTextView = (TextView)view.findViewById(R.id.textView);
                              messageTextView.setText(message);
                          } catch (InflateException e) {
                              /* map is already there, just return view as it is */
                          }
                  
                  //      View v = inflater.inflate(R.layout.myfragment_layout, container, false);
                  
                  
                      return view;
                  }
                  
                  
                  @Override
                  public void onActivityCreated(Bundle savedInstanceState) {
                      super.onActivityCreated(savedInstanceState);
                      FragmentManager fm = getChildFragmentManager();
                      mSupportMapFragment = (SupportMapFragment) fm.findFragmentById(R.id.location_map);
                      if (mSupportMapFragment == null) {
                          mSupportMapFragment = SupportMapFragment.newInstance();
                          fm.beginTransaction().replace(R.id.location_map, mSupportMapFragment).commit();
                      }
                  }
                  
                  @Override
                  public void onResume() {
                      super.onResume();
                      if (googleMap == null) {
                          googleMap = mSupportMapFragment.getMap();
                          googleMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)));
                      }
                    }
                  
                  }
                  

                  推荐答案

                  我找到了解决方案.

                  它的发生是因为 FragmentStatePagerAdapter.在片段中,我们需要分离 getChildFragmentManager.

                  Its happening because of FragmentStatePagerAdapter. In the fragment we need to detach the getChildFragmentManager.

                  所以我们需要在activityCreated()之后添加下面的代码

                  So we need to add the below code after activityCreated()

                          @Override
                      public void onDetach() {
                          super.onDetach();
                          try {
                              Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
                              childFragmentManager.setAccessible(true);
                              childFragmentManager.set(this, null);
                          } catch (NoSuchFieldException e) {
                              throw new RuntimeException(e);
                          } catch (IllegalAccessException e) {
                              throw new RuntimeException(e);
                          }
                      }
                  

                  这篇关于由于导航回旧片段,查看 Pager Crash Null Pointer Exception - Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:viewPager中的按钮onClick? 下一篇:Toast 类型中的方法 makeText 不适用于参数

                  相关文章

                  <legend id='4mf9f'><style id='4mf9f'><dir id='4mf9f'><q id='4mf9f'></q></dir></style></legend>
                  <tfoot id='4mf9f'></tfoot>

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

                  • <bdo id='4mf9f'></bdo><ul id='4mf9f'></ul>