2013-11-01 110 views
-1

當我改變方向並刷卡時,它崩潰。 這是我的日誌貓getActivity()在片段中返回null

11-01 11:21:19.715: E/AndroidRuntime(4336): FATAL EXCEPTION: main 
11-01 11:21:19.715: E/AndroidRuntime(4336): java.lang.NullPointerException 
11-01 11:21:19.715: E/AndroidRuntime(4336):  at com.example.saisuke.CalendarAdapter.<init>(CalendarAdapter.java:33) 
11-01 11:21:19.715: E/AndroidRuntime(4336):  at com.example.saisuke.MonthFragment.updateCurrentMonth(MonthFragment.java:161) 
11-01 11:21:19.715: E/AndroidRuntime(4336):  at com.example.saisuke.MonthFragment.onNextMonth(MonthFragment.java:177) 
11-01 11:21:19.715: E/AndroidRuntime(4336):  at com.example.saisuke.Saisuke$1.onPageScrollStateChanged(Saisuke.java:107) 
11-01 11:21:19.715: E/AndroidRuntime(4336):  at android.support.v4.view.ViewPager.setScrollState(ViewPager.java:398) 
11-01 11:21:19.715: E/AndroidRuntime(4336):  at android.support.v4.view.ViewPager.access$000(ViewPager.java:84) 
11-01 11:21:19.715: E/AndroidRuntime(4336):  at android.support.v4.view.ViewPager$3.run(ViewPager.java:243) 
11-01 11:21:19.715: E/AndroidRuntime(4336):  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725) 
11-01 11:21:19.715: E/AndroidRuntime(4336):  at android.view.Choreographer.doCallbacks(Choreographer.java:555) 
11-01 11:21:19.715: E/AndroidRuntime(4336):  at android.view.Choreographer.doFrame(Choreographer.java:524) 
11-01 11:21:19.715: E/AndroidRuntime(4336):  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711) 
11-01 11:21:19.715: E/AndroidRuntime(4336):  at android.os.Handler.handleCallback(Handler.java:615) 
11-01 11:21:19.715: E/AndroidRuntime(4336):  at android.os.Handler.dispatchMessage(Handler.java:92) 11-01 
11:21:19.715: E/AndroidRuntime(4336): at android.os.Looper.loop(Looper.java:137) 
11-01 11:21:19.715: E/AndroidRuntime(4336):  at android.app.ActivityThread.main(ActivityThread.java:4898) 
11-01 11:21:19.715: E/AndroidRuntime(4336):  at java.lang.reflect.Method.invokeNative(Native Method) 
11-01 11:21:19.715: E/AndroidRuntime(4336):  at java.lang.reflect.Method.invoke(Method.java:511) 
11-01 11:21:19.715: E/AndroidRuntime(4336):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006) 
11-01 11:21:19.715: E/AndroidRuntime(4336):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773) 
11-01 11:21:19.715: E/AndroidRuntime(4336):  at dalvik.system.NativeStart.main(Native Method) 

MainActivity.java

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_saisuke); 
     ((Global) this.getApplication()).setStraightMode(false); 
     mOldState = ((Global) this.getApplication()).getStraightMode(); 
     monthList = new ArrayList<MonthFragment>(3); 
     curMonth = Calendar.getInstance(); 
     Calendar prevMonth, nextMonth; 
     prevMonth = (Calendar) curMonth.clone(); 
     nextMonth = (Calendar) curMonth.clone(); 
     prevMonth.set(Calendar.MONTH, prevMonth.get(Calendar.MONTH) - 1); 
     nextMonth.set(Calendar.MONTH, nextMonth.get(Calendar.MONTH) + 1); 
      monthList.add(MonthFragment.newInstance(prevMonth)); 
      monthList.add(MonthFragment.newInstance(curMonth)); 
      monthList.add(MonthFragment.newInstance(nextMonth)); 
     //tmpCurMonth = monthList[1].getCurCalendar(); 
     mViewPager = (ViewPager) findViewById(R.id.pager); 
     mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), monthList); 
     mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { 
      @Override 
      public void onPageScrollStateChanged(int arg0) { 
       // TODO Auto-generated method stub 
       if (arg0 == ViewPager.SCROLL_STATE_IDLE) { 
        if (mSelectedPageIndex < PAGE_MIDDLE) { 
         monthList.get(0).onPreviousMonth(); 
         monthList.get(1).onPreviousMonth(); 
         monthList.get(2).onPreviousMonth(); 
        } else if (mSelectedPageIndex > PAGE_MIDDLE) { 
         monthList.get(0).onNextMonth(); 
         monthList.get(1).onNextMonth(); 
         monthList.get(2).onNextMonth(); 
        } 
        mViewPager.setCurrentItem(1, false); 
        //tmpCurMonth = monthList[1].getCurCalendar(); 
        mSectionsPagerAdapter.notifyDataSetChanged(); 
       } 
      } 

      @Override 
      public void onPageScrolled(int arg0, float arg1, int arg2) { 
       // TODO Auto-generated method stub 
       if (((Global) getApplication()).getStraightMode() != mOldState){ 
        mOldState = ((Global) getApplication()).getStraightMode(); 
        mSectionsPagerAdapter.notifyDataSetChanged(); 
       } 
      } 

      @Override 
      public void onPageSelected(int arg0) { 
       // TODO Auto-generated method stub 
       mSelectedPageIndex = arg0; 
      } 
     }); 
     mViewPager.setAdapter(mSectionsPagerAdapter); 
     mViewPager.setCurrentItem(1, false); 
    } 

MyFragent.java

static MonthFragment newInstance(Calendar a) { 
     MonthFragment f = new MonthFragment(a); 
     return f;  
    } 

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

     if (savedInstanceState != null) { 
      // Restore last state 
      calendar = (Calendar) savedInstanceState.getSerializable("starttime"); 
     } 

     mMainView = inflater.inflate(R.layout.fragment_saisuke, container, false); 
     adap = new CalendarAdapter(getActivity(), calendar); 
     ....   
     return mMainView; 
    } 

protected void updateCurrentMonth() { 
     adap.updateData(); 
    } 

protected final void onNextMonth() { 
     if (calendar.get(Calendar.MONTH) == Calendar.DECEMBER) { 
      calendar.set((calendar.get(Calendar.YEAR) + 1), Calendar.JANUARY, 1); 
     } else { 
      calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + 1); 
     } 
     updateCurrentMonth(); 
    } 
+0

什麼是第33行'CalendarAdapter.java'? 'getActivity()'不爲null。你的'日曆'可能會爲空 – Raghunandan

+0

@Raghunandan:日曆不爲空。 第33行是mInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 因爲getActivity()得到null,所以上下文爲空 – lolyoshi

+0

如果它在片段用戶'getActivity()'而不是'context'並且發佈那部分代碼 – Raghunandan

回答

-4

我終於找到了如何修復我的錯誤 這是相當簡單的。我只需要覆蓋活動中的onSaveInstanceState函數並省略super.onSaveInstanceState(outState); 它看起來像

@Override 
protected void onSaveInstanceState(final Bundle outState) {   
    //super.onSaveInstanceState(outState); 
} 
+3

否否否...這是不好的:)不要。特別糟糕。該活動有一個生命週期,並執行您不必擔心的事情。它在調用方法時執行。如果您重寫某個方法並跳過超級調用,則基本上阻止該活動執行其內部工作。 –

+1

@MartínMarconcini:但是,它適用於我。它解決了我的錯誤 – lolyoshi

+1

你明白爲什麼它解決了你的錯誤? –

5

自己的解決方案是非常不建議,你永遠不應該跳過調用的生命週期方法的超()。

相反,找出你的問題在哪裏。

提示要做到這一點:

  1. 覆蓋(並調用超!)每個活動/片段的生命週期方法。 OnCreate,OnResume,OnPause,OnStop,OnCreateView,OnViewCreated等...

  2. 向每個添加一條LOG行並運行您的應用程序。看看哪些人被調用並按照哪個順序。

  3. 旋轉您的設備。比較結果。

  4. 找到爲什麼你的成員變量未初始化,鑑於上旋轉,活動破壞並重新創建。

  5. 對於不使用非常糟糕的黑客以及您的Android應用程序不會神祕崩潰的事實感到高興。

+0

我的問題可能與此類似。如果你有任何其他想法。你可以告訴我,因爲我找不到其他方法來解決它 http://stackoverflow.com/questions/13910826/viewpager-fragmentstatepageradapter-orientation-change – lolyoshi

相關問題