タブ間のスワイプを可能にする 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();
}
計算ポイント()
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)