我有一个带有ViewPager的Android应用程序,允许在标签之间滑动。当我在选项卡之间滑动时,我调用新活动的Fragment中的函数来更新该Fragment上的文本,它有时会抛出NullPointerException
但我无法在我的设备上复制它。想知道是否有人可以看看代码&堆栈跟踪下面,可能会有所帮助 - 非常感谢。我不确定我是否遗漏了某些东西或者没有正确地做到这一点?
我今天早上的第一个想法是,也许我应该在一个单独的线程上运行它,以免打扰主UI线程或者尝试/捕获这些功能,因为它们似乎有时只会崩溃应用程序?提前谢谢。
ViewPagerCode
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();
}
calculatePoints()
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)
答案 0 :(得分:0)
我更改了我的代码,以检测Android团队在2012年年中修复的userVisibleHint
。这100%工作并解决了我的问题 - 谢谢大家!
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser == true) {
// TO-DO
}
else if (isVisibleToUser == false) {
// TO-DO
}
}
Android支持库(第11版)finally fixed the user visible hint issue,现在如果您使用片段支持库,则可以 安全地使用getUserVisibleHint()或覆盖setUserVisibleHint() 按照gorn的回答所描述的那样捕捉变化。