访问片段视图时偶尔会出现NPE

时间:2018-01-03 20:33:29

标签: java android android-fragments android-view android-lifecycle

我在输入片段时偶尔会得到@if ($notActivated) Your account is blocked @endif 。当应用程序在后台运行很长时间然后我打开它并滑动到这个片段时就会发生这种情况。

NullPointerException

MainActivity viewPager:

public class SummaryFragment extends Fragment implements FragmentLifecycle {

    private static final String TAG = "DTAG";
    private DateFormat dateFormatName;
    private Preference prefs;
    private List<String> monthList;
    private TextView totalTimeFullTv;
    private TextView totalTimeNetTv;
    private TextView averageTimeTv;
    private TextView overUnderTv;
    private TextView minTimeTv;
    private TextView maxTimeTv;
    private TextView vacationsTv;
    private TextView sickTv;
    private TextView headlineTv;
    private TextView overUnderTvH;
    private OnFragmentInteractionListener mListener;

    public SummaryFragment() {
        // Required empty public constructor
    }


    public static SummaryFragment newInstance(String param1, String param2) {
        SummaryFragment fragment = new SummaryFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

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

        View RootView = inflater.inflate(R.layout.fragment_summary, container, false);

        dateFormatName = new SimpleDateFormat(getResources().getString(R.string.month_text));
        monthList = Arrays.asList(new DateFormatSymbols().getMonths());
        prefs = new Preference(GeneralAdapter.getContext());


        totalTimeFullTv = RootView.findViewById(R.id.textView_sum_ttf);
        totalTimeNetTv = RootView.findViewById(R.id.textView_sum_ttn);
        averageTimeTv = RootView.findViewById(R.id.textView_sum_av);
        overUnderTv = RootView.findViewById(R.id.textView_sum_ou);
        overUnderTvH = RootView.findViewById(R.id.textView_sum_ou_h);


        minTimeTv = RootView.findViewById(R.id.textView_sum_min);
        maxTimeTv = RootView.findViewById(R.id.textView_sum_max);
        vacationsTv = RootView.findViewById(R.id.textView_sum_vac);
        sickTv = RootView.findViewById(R.id.textView_sum_sick);
        headlineTv= RootView.findViewById(R.id.textView_sum_headline);

        return RootView;
    }

    private void refreshData() {

        if (prefs == null)
        {
            prefs = new Preference(GeneralAdapter.getContext());
        }

        String month = prefs.getString(Preference.CURRENT_MONTH);

        MonthData monthData = Calculators.CalculateLocalData(MainActivity.db.getAllDays(month));

        totalTimeFullTv.setText(monthData.getTotalTimeFull()); //Crash here
        totalTimeNetTv.setText(monthData.getTotalTimeNet());
        averageTimeTv.setText(monthData.getAverageTime());
        overUnderTv.setText(monthData.getOverUnder());
        if (monthData.getOverUnderFloat()<0)
        {
            overUnderTvH.setText(R.string.sum_over_time_neg);
            overUnderTv.setTextColor(ContextCompat.getColor(GeneralAdapter.getContext(),R.color.negative_color));
        }
        else
        {
            overUnderTvH.setText(R.string.sum_over_time_pos);
            overUnderTv.setTextColor(ContextCompat.getColor(GeneralAdapter.getContext(),R.color.positive_color));
        }

        minTimeTv.setText(monthData.getMinTime());
        maxTimeTv.setText(monthData.getMaxTime());
        vacationsTv.setText(""+monthData.getVacations());
        sickTv.setText(""+monthData.getSick());
        headlineTv.setText(month);
    }

    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttachFragment(Fragment childFragment) {
        super.onAttachFragment(childFragment);

    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public void onPauseFragment() {

    }

    @Override
    public void onResumeFragment()
    {
        refreshData();
    }


    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}

日志:

viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            int currentPosition = 0;

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {

                FragmentLifecycle fragmentToHide = (FragmentLifecycle) adapter.getItem(currentPosition);
                fragmentToHide.onPauseFragment();

                FragmentLifecycle fragmentToShow = (FragmentLifecycle) adapter.getItem(position);
                fragmentToShow.onResumeFragment(); //Crash start

                currentPosition = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

更新

我最终使用了:

                                            E/AndroidRuntime: FATAL EXCEPTION: main
                                               Process: michlind.com.workcalendar, PID: 25038
                                               java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                                                   at michlind.com.workcalendar.mainfragments.SummaryFragment.refreshData(SummaryFragment.java:99)
                                                   at michlind.com.workcalendar.mainfragments.SummaryFragment.onResumeFragment(SummaryFragment.java:147)
                                                   at michlind.com.workcalendar.activities.MainActivity.onPageSelected(MainActivity.java:84)
                                                   at android.support.v4.view.ViewPager.dispatchOnPageSelected(ViewPager.java:1941)
                                                   at android.support.v4.view.ViewPager.scrollToItem(ViewPager.java:680)
                                                   at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:664)
                                                   at android.support.v4.view.ViewPager.onTouchEvent(ViewPager.java:2257)
                                                   at android.view.View.dispatchTouchEvent(View.java:11776)
                                                   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2962)
                                                   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2643)
                                                   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2968)
                                                   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2657)
                                                   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2968)
                                                   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2657)
                                                   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2968)
                                                   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2657)
                                                   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2968)
                                                   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2657)
                                                   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2968)
                                                   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2657)
                                                   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2968)
                                                   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2657)
                                                   at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:448)
                                                   at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1829)
                                                   at android.app.Activity.dispatchTouchEvent(Activity.java:3307)
                                                   at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:68)
                                                   at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:410)
                                                   at android.view.View.dispatchPointerEvent(View.java:12015)
                                                   at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4795)
                                                   at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4609)
                                                   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4147)
                                                   at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4200)
                                                   at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4166)
                                                   at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4293)
                                                   at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4174)
                                                   at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4350)
                                                   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4147)
                                                   at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4200)
                                                   at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4166)
                                                   at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4174)
                                                   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4147)
                                                   at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6661)
                                                   at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6635)
                                                   at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6596)
                                                   at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6764)
                                                   at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:186)
                                                   at android.os.MessageQueue.nativePollOnce(Native Method)
                                                   at android.os.MessageQueue.next(MessageQueue.java:325)
                                                   at android.os.Looper.loop(Looper.java:142)
                                                   at android.app.ActivityThread.main(ActivityThread.java:6494)

在我的MainActivity中,并在每个片段上使用onResume()。这个适配器的解决方案: http://thedeveloperworldisyours.com/android/update-fragment-viewpager/

10 个答案:

答案 0 :(得分:3)

问题是,您过早地尝试访问视图:此时尚未创建视图层次结构。

如果您发布的事件将在下一帧发生,您可以保证,该视图层次结构已经设置:

@Override
public void onResumeFragment() {
    new Handler().post(new Runnable() {
        @Override
        public void run() {
            refreshData();
        }
    });
}

答案 1 :(得分:1)

当我为ViewPager实现自定义生命周期时,我遇到了同样的问题。我认为您正在使用FragmentStatePagerAdapter使用ViewPager填充片段。我们知道FragmentStatePagerAdapter在失去焦点时会破坏所有碎片。我们需要使用单例模式为每个页面提供相同的对象。

您的代码中,对于单例模式,片段创建可以如下所示。

private SummaryFragment mInstance;

private SummaryFragment() {
        // Required empty public constructor
    }

public static SummaryFragment newInstance(String param1, String param2) {
        if(mInstance == null)
        mInstance = new SummaryFragment();
        return mInstance;
    }

这样做解决了我的问题。如果这不适合你?你能分享你的PagerAdapter课吗?

答案 2 :(得分:0)

在创建此片段的所有视图之前,将调用

onResumeFragment()。 尝试首先重新创建newInstance,然后在Activity中调用onResumeFragment接口FragmentLifeCycle

答案 3 :(得分:0)

ViewPager会在任意一方附加几个项目(即片段已恢复),但FragmentPagerAdapter使用Fragment.setUserVisibleHint来表示哪个项目是最新的。而是利用它。

以下是利用用户可见提示的方法:

  1. 删除OnPageChangeListener
  2. 抛弃FragmentLifecycle界面。
  3. 像这样设置你的片段:
  4. (在Kotlin,但你会得到要点)

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
    
        if (isVisibleToUser && isResumed) {
            // Do stuff.
        }
    }
    
    override fun onResume() {
        super.onResume()
    
        if (userVisibleHint) {
            // Do the same stuff.
        }
    }
    

    更多信息

    FragmentPagerAdapter.getItem是一种工厂方法。 总是应该返回一个片段的新实例。如果您尝试缓存它们,请删除缓存(1),不要自己使用getItem(2)。

    1. 有时崩溃但有时不崩溃的代码是b ****来调试。这可能是因为你不应该重复使用碎片。
    2. 未附加新的片段实例,没有理由创建视图,并在您离开onPageSelected后进行垃圾回收。

答案 4 :(得分:0)

您正在错误地使用OnPageChangeListener。这不是控制视图生命周期事件的安全方法。您需要将PagerAdapter与ViewPager结合使用并覆盖其instantiateItem / destroyItem回调。

请参阅此示例:http://android-er.blogspot.com/2014/04/example-of-viewpager-with-custom.html

PagerAdapter是ViewPager ListAdapter对ListView的作用,你需要两者才能让你的系统正常工作。

答案 5 :(得分:0)

使用片段的onViewCreated()回调方法来更新您的数据,确保您的所有观看次数都完美无缺。

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
   refreshData();

}

使用Handler仍然存在风险,因为您无法确定视图是否会膨胀。

答案 6 :(得分:0)

<强>问题

Fragment的生命周期是独立的。当onPageSelected()被注册时,您无法确定该片段是否已经布局。它是一个异步事件。所以你不能依赖这个回调。

但另一方面,您不能仅依赖onResume(),因为在ViewPager中,预先加载了与当前可见页面相邻的页面。

<强>解

当片段对用户可见并且正在运行时,您需要refreshData()onResume()的定义说的相同:

  

当片段对用户可见并且正在运行时调用。 (...)

只需在您的片段的refreshData()中拨打onResume()即可,如果您在ViewPager未真正显示此页面时发现此调用,请不要担心。

答案 7 :(得分:0)

与大多数人一样,您需要确保您的片段处于活动状态且对用户可见。我有类似的问题。我使用onHiddenChanged决定何时重新加载数据。

@Override
public void onHiddenChanged(boolean hidden) {
    super.onHiddenChanged(hidden);
    if (!hidden) {
        refreshData();
    }
}

答案 8 :(得分:-1)

  

您应该在onCreateView中夸大您的布局,但不应使用findViewById中的onCreateView初始化其他观看次数。

这是FragmentManager的代码

  // This calls onCreateView()
f.mView = f.performCreateView(f.getLayoutInflater(f.mSavedFragmentState), null, f.mSavedFragmentState);

// Null check avoids possible NPEs in onViewCreated
// It's also safe to call getView() during or after onViewCreated()
if (f.mView != null) {
    f.mView.setSaveFromParentEnabled(false);
    if (f.mHidden) f.mView.setVisibility(View.GONE);
    f.onViewCreated(f.mView, f.mSavedFragmentState);
}

最好将子视图分配给onViewCreated中的字段。这是因为框架会对您进行自动空检查,以确保您的Fragment的视图层次结构已被正确创建和膨胀(如果使用XML布局文件)。

创建视图后,然后初始化视图。

答案 9 :(得分:-1)

在refreshData()方法中添加此检查:

if (isAdded() && getActivity() != null)