我有一個Android應用程序與ViewPager,允許在標籤之間滑動。當我在標籤頁之間滑動時,我會調用新激活的Fragment中的函數來更新該片段上的文本,並且它有時會拋出NullPointerException
,但我無法將其複製到我的設備上。想知道是否有人可以看看下面的代碼&堆棧跟蹤和可能的協助 - 這將不勝感激。我不確定我是否錯過了某些東西,或者沒有正確地做到這一點?NullPointerException getDefaultSharedPreferences/getDefaultSharedPreferencesName
我今天早上第一個想到的是,也許我應該在單獨的線程上運行此操作,以免打擾主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)
您確定currentActivity不爲null嗎?您可能在活動附加到您的新片段之前調用您的calculatePoints()方法 – dymmeh
'calculatePoints()'必須在調用'onActivityCreated()'後調用分段。 –
我的膝蓋反應是懷疑你正在通過它的'currentActivity'。 –