使用标签活动的Android SharedPreferences

时间:2017-03-04 13:41:06

标签: android

我有两个标签(片段)和一个主要活动。在片段2中,我想使用SharedPerferences来存储一个值并检查前一个值是否更大。如果是,则将其替换为更高的值。片段1仅用于显示值。打开应用程序后,片段1是默认片段。

在片段2中:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();

int score = sharedPref.getInt("marks", 0);

if (newS > score) { //newS is the new score
  editor.putInt("marks", newS);
  editor.commit();
}

在片段1中:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int score = sharedPref.getInt("marks", 0);
x.setText(score); //x is a text view.

但是当我运行它时,应用程序会继续关闭。

E/AndroidRuntime: FATAL EXCEPTION: main
android.content.res.Resources$NotFoundException: String resource ID #0x0
      at android.content.res.Resources.getText(Resources.java:247)
      at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52)
      at android.widget.TextView.setText(TextView.java:3495)
      at com.example.mygames.Profile.onCreateView(Profile.java:31)
      at android.support.v4.app.Fragment.performCreateView(Fragment.java:2189)
      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299)
      at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
      at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:757)
      at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2355)
      at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2146)
      at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2098)
      at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1979)
      at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:626)
      at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143)
      at android.support.v4.view.ViewPager.populate(ViewPager.java:1268)
      at android.support.v4.view.ViewPager.populate(ViewPager.java:1116)
      at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1642)
      at android.view.View.measure(View.java:12881)
      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4802)
      at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:714)
      at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:90)
      at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1375)
      at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:784)
      at android.view.View.measure(View.java:12881)
      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4802)
      at android.widget.FrameLayout.onMeasure(FrameLayout.java:297)
      at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
      at android.view.View.measure(View.java:12881)
      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4802)
      at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1385)
      at android.widget.LinearLayout.measureVertical(LinearLayout.java:676)
      at android.widget.LinearLayout.onMeasure(LinearLayout.java:557)
      at android.view.View.measure(View.java:12881)
      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4802)
      at android.widget.FrameLayout.onMeasure(FrameLayout.java:297)
      at android.view.View.measure(View.java:12881)
      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4802)
      at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1385)
      at android.widget.LinearLayout.measureVertical(LinearLayout.java:676)
      at android.widget.LinearLayout.onMeasure(LinearLayout.java:557)
      at android.view.View.measure(View.java:12881)
      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4802)
      at android.widget.FrameLayout.onMeasure(FrameLayout.java:297)
      at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2111)
      at android.view.View.measure(View.java:12881)
      at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064)
      at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
      at android.os.Handler.dispatchMessage(Handler.java:99)
      at android.os.Looper.loop(Looper.java:137)
      at android.app.ActivityThread.main(ActivityThread.java:4441)
      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:784)
      at com.android.internal.os.ZygoteIni
03-04 22:22:18.782 310-943/? W/ActivityManager:   Force finishing activity com.example.mygames/.MainActivity

1 个答案:

答案 0 :(得分:0)

您尝试使用int:x.setText(score)设置文本但分数与字符串资源的ID不匹配。

如果要向textview显示分数值,可以执行以下操作:x.setText(String.valueOf(score));