0

我有一个带有 ViewPager 的 Android 应用程序,它允许在选项卡之间滑动。当我在选项卡之间滑动时,我调用新活动片段中的函数来更新该片段上的文本,它有时会抛出一个NullPointerException但我无法在我的设备上复制它。想知道是否有人可以查看下面的代码和堆栈跟踪并可能提供帮助 - 将不胜感激。我不确定我是否遗漏了什么或没有正确执行此操作?

今天早上我的第一个想法是,也许我应该在一个单独的线程上运行它,以免干扰主 UI 线程,或者尝试/捕获这些函数,因为它们似乎只是有时会使应用程序崩溃?提前致谢。

查看寻呼机代码

public void onTabChanged(String tag) {
        //TabInfo newTab = this.mapTabInfo.get(tag);
        int pos = this.mTabHost.getCurrentTab();
        mTabHost.getCurrentTabView().invalidate();
        this.mViewPager.setCurrentItem(pos);



    // calculatePoints on TabChange
    if (pos == 1) {
        TabFragment2 fragTrack = (TabFragment2) fragments.get(1);
        fragTrack.calculatePoints();
        fragTrack.createAndFillDatabase();
    }

    else if (pos == 2) {
        TabFragment3 fragFoodList = (TabFragment3) fragments.get(2);
        fragFoodList.createAndFillDatabase("none");
    }

    this.supportInvalidateOptionsMenu();
}

计算点()

public void calculatePoints() {
        int prefDailyPoints, prefPointsUsed, prefPointsRemain, prefBonusPoints, prefBonusUsed, prefBonusPointsRemain;

        // Get points values and fill in respective fields
        appSettings = PreferenceManager.getDefaultSharedPreferences(currentActivity);
        nonDBData = currentActivity.getSharedPreferences(NONDB_DATA, 0);

        prefDailyPoints = Integer.parseInt(appSettings.getString("pointsAllowed", "0"));
        prefPointsUsed = nonDBData.getInt("pointsUsed", 0);
        //prefPointsRemain = nonDBData.getInt("pointsRemain", prefDailyPoints - prefPointsUsed);
        prefPointsRemain = prefDailyPoints - prefPointsUsed;

        textfieldPointsDaily.setText(Integer.toString(prefDailyPoints));
        textfieldPointsUsed.setText(Integer.toString(prefPointsUsed));
        textfieldPointsRemain.setText(Integer.toString(prefPointsRemain));

        if (prefPointsRemain < 0) {
                textfieldPointsRemain.setTextColor(Color.rgb(255, 0, 0));
                textfieldPointsRemain.setTypeface(null, Typeface.BOLD);
        }
        else {
                textfieldPointsRemain.setTextColor(Color.rgb(0, 0, 0));
                textfieldPointsRemain.setTypeface(null, Typeface.NORMAL);
        }
}

堆栈跟踪

java.lang.NullPointerException 
at android.preference.PreferenceManager.getDefaultSharedPreferencesName(PreferenceManager.java:371)
at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:366)
at com.mattdonders.android.wppcalculator.TabFragment2.calculatePoints(TabFragment2.java:667)
at com.mattdonders.android.wppcalculator.TabsViewPagerFragmentActivity.onTabChanged(TabsViewPagerFragmentActivity.java:483)
at android.widget.TabHost.invokeOnTabChangeListener(TabHost.java:446)
at android.widget.TabHost.setCurrentTab(TabHost.java:431) 
at com.mattdonders.android.wppcalculator.TabsViewPagerFragmentActivity.onPageSelected(TabsViewPagerFragmentActivity.java:511)
at android.support.v4.view.ViewPager.scrollToItem(ViewPager.java:539)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:524)
at android.support.v4.view.ViewPager.onTouchEvent(ViewPager.java:1941)
at android.view.View.dispatchTouchEvent(View.java:7239) 
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2168)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1903) 
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917) 
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917) 
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917) 
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917) 
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917) 
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917) 
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1953)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1405)
at android.app.Activity.dispatchTouchEvent(Activity.java:2410) 
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1901)
at android.view.View.dispatchPointerEvent(View.java:7419) 
at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3220)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3165)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4292)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4271)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4363)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179)
at android.os.MessageQueue.nativePollOnce(Native Method) 
at android.os.MessageQueue.next(MessageQueue.java:125) 
at android.os.Looper.loop(Looper.java:124)
at android.app.ActivityThread.main(ActivityThread.java:5039) 
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:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

0

我更改了代码以检测userVisibleHintAndroid 团队在 2012 年年中修复了哪些代码。这 100% 有效并解决了我的问题 - 谢谢大家!

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);

    if (isVisibleToUser == true) { 
        // TO-DO
    }
    else if (isVisibleToUser == false) {
        // TO-DO
    }

}

Android 支持库(第 11 版)最终修复了用户可见提示问题,现在如果您将支持库用于片段,那么您可以安全地使用 getUserVisibleHint() 或覆盖 setUserVisibleHint() 来捕获gorn 回答中描述的更改。

于 2013-01-15T15:05:26.587 回答