0

Ich erhalte eine NPE, wenn ich versuche, eine LinearLayout Variable während onCreate in einem Fragment zuzuweisen. Dieser Fehler tritt nur in API-Versionen < 23 auf (ich habe nicht alle ausprobiert, aber habe 19 und 16 ausprobiert und sie stürzen beim Start ab). Ab 23+ funktioniert alles perfekt und ich bin mir nicht sicher warum.FindViewById/Butterknife.findById wirft NullPointerException nur auf API <23

-Code in Frage:

public void reset() { 
     layoutHolder = ButterKnife.findById(view, R.id.layoutHolder); 
     //I get the same error using (LinearLayout) view.findViewById(...) 
     if (layoutHolder.getChildCount() > 0) { 
      layoutHolder.removeAllViews(); 
      layouts.clear(); 
      sections.clear(); 
      addSectionToLayout(createSection(1)); 
     } 
} 

view wird im onCreate fragment initialisiert

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     view = inflater.inflate(R.layout.fragment_log, container, false); 
     //Omitted for brevity 
     Butterknife.bind(this, view); 
     return view; 
    } 

Fehler/stracktrace

E/AndroidRuntime: FATAL EXCEPTION: main 
Process: com.omitted, PID: 2236 
java.lang.NullPointerException 
at butterknife.ButterKnife.findById(ButterKnife.java:343) 
at com.omitted.LogFragment.reset(LogFragment.java:156) 
at com.omitted.LogPresenter.reset(LogPresenter.java:35) 
at com.omitted.CalculatorModel.doReset(CalculatorModel.java:251) 
at com.omitted.CalculatorPresenter.onResetClicked(CalculatorPresenter.java:107) 
at com.omitted.CalculatorFragment.reset(CalculatorFragment.java:130) 
at com.omitted.CalculatorFragment.onCreateView(CalculatorFragment.java:97) 
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974) 
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) 
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252) 
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742) 
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617) 
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570) 
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141) 
at android.support.v4.view.ViewPager.populate(ViewPager.java:1177) 
at android.support.v4.view.ViewPager.populate(ViewPager.java:1025) 
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1545) 
at android.view.View.measure(View.java:16497) 
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) 
at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:668) 
at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:735) 
at android.view.View.measure(View.java:16497) 
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) 
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135) 
at android.view.View.measure(View.java:16497) 
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) 
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404) 
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695) 
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588) 
at android.view.View.measure(View.java:16497) 
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) 
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
at android.view.View.measure(View.java:16497) 
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) 
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404) 
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695) 
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588) 
at android.view.View.measure(View.java:16497) 
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) 
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291) 
at android.view.View.measure(View.java:16497) 
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916) 
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113) 
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295) 
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000) 
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670) 
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761) 
at android.view.Choreographer.doCallbacks(Choreographer.java:574) 
at android.view.Choreographer.doFrame(Choreographer.java:544) 
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747) 
at android.os.Handler.handleCallback(Handler.java:733) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:136) 
t android.app.ActivityThE/AndroidRuntime: FATAL EXCEPTION: main 
ess: com.omitted, PID: 2236 
.lang.NullPointerException 
at butterknife.ButterKnife.findById(ButterKnife.java:343) 
at com.omitted.LogFragment.reset(LogFragment.java:156) 
at com.omitted.LogPresenter.reset(LogPresenter.java:35) 
at com.omitted.CalculatorModel.doReset(CalculatorModel.java:251) 
at com.omitted.CalculatorPresenter.onResetClicked(CalculatorPresenter.java:107) 
at com.omitted.CalculatorFragment.reset(CalculatorFragment.java:130) 
at com.omitted.CalculatorFragment.onCreateView(CalculatorFragment.java:97) 
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974) 
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) 
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252) 
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742) 
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617) 
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570) 
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141) 
at android.support.v4.view.ViewPager.populate(ViewPager.java:1177) 
at android.support.v4.view.ViewPager.populate(ViewPager.java:1025) 
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1545) 
at android.view.View.measure(View.java:16497) 
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) 
at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:668) 
at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:735) 
at android.view.View.measure(View.java:16497) 
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) 
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135) 
at android.view.View.measure(View.java:16497) 
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) 
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404) 
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695) 
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588) 
at android.view.View.measure(View.java:16497) 
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) 
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
at android.view.View.measure(View.java:16497) 
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) 
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404) 
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695) 
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588) 
at android.view.View.measure(View.java:16497) 
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) 
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291) 
at android.view.View.measure(View.java:16497) 
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916) 
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113) 
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295) 
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000) 
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670) 
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761) 
at android.view.Choreographer.doCallbacks(Choreographer.java:574) 
at android.view.Choreographer.doFrame(Choreographer.java:544) 
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747) 
at android.os.Handler.handleCallback(Handler.java:733) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:136) 
at android.app.ActivityTh 

ich das nicht scheinen, um herauszufinden kann. Hat jemand schon einmal auf dieses Problem gestoßen? Ist der Fragmentlebenszyklus auf API < 23 oder etwas anderes?

Jeder Eingang ist dankbar, danke.

+0

Umgeben den Code in dieser Methode mit 'if (view! = Null)' löst das Problem, aber das scheint sehr sehr hacky. Warum ist das notwendig? –

Antwort

1

Haben Sie ButterKnife.bind (this, view) verwendet?

+0

Ja, tat ich. Es scheint jedoch nicht, dass "ButterKnife.bind (this, view)" in meinem Fragment funktioniert. Ich weiß nicht, warum, vor allem, weil es in meinem anderen Fragment gut funktioniert (2-Fragment-Layout mit Tabs) –

+0

Könnten Sie Code der Initialisierung Ihrer Fragmente in Aktivität beifügen? –