Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 共享首选项在重新打开活动时未检索值_Java_Android_Performance_Sharedpreferences - Fatal编程技术网

Java 共享首选项在重新打开活动时未检索值

Java 共享首选项在重新打开活动时未检索值,java,android,performance,sharedpreferences,Java,Android,Performance,Sharedpreferences,我正在创建一个应用程序,在这个应用程序中,观看奖励视频广告会增加硬币/点数,这些硬币/点数将被保存 例如:每次点击按钮,硬币价值增加到10点。现在,当我完全销毁应用程序并再次打开它时,分值应该显示相同,而不是零。 我试图实现共享首选项来保存硬币,但在退出应用程序后,当我重新打开它时,硬币像往常一样变为零 这是我的密码 public class BooksActivity extends AppCompatActivity implements RewardedVideoAdListener {

我正在创建一个应用程序,在这个应用程序中,观看奖励视频广告会增加硬币/点数,这些硬币/点数将被保存

例如:每次点击按钮,硬币价值增加到10点。现在,当我完全销毁应用程序并再次打开它时,分值应该显示相同,而不是零。 我试图实现共享首选项来保存硬币,但在退出应用程序后,当我重新打开它时,硬币像往常一样变为零

这是我的密码

public class BooksActivity extends AppCompatActivity implements RewardedVideoAdListener {
private RewardedVideoAd mRewardedVideoAd;
private TextView mText;
private int coinCount;
private CardView book;


public static final String PREFS_NAME = "MyPrefsFile";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_books);
    MobileAds.initialize(this, R.string.mypublisherid);

    SharedPreferences sharedPreferences = getSharedPreferences(PREFS_NAME,0);
    coinCount = sharedPreferences.getInt("coins", coinCount);

    mText = (TextView) findViewById(R.id.mText);
    coinCount = 0;
    mText.setText(" " + coinCount);
    SharedPreferences sharedpreferences = getSharedPreferences(PREFS_NAME,0);
    SharedPreferences.Editor editor = sharedpreferences.edit();
    editor.putInt("coins", coinCount);
    editor.commit();


    mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
    mRewardedVideoAd.setRewardedVideoAdListener(this);



    loadRewardedVideoAd();


    book = (CardView) findViewById(R.id.book_one);
    book.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (coinCount <= 29) {
                //if(coinCount <30) {
                new MaterialStyledDialog.Builder(BooksActivity.this)
                        .setTitle("Not Enough Coins")
                        .setDescription("Watch Ads To earn coins")
                        .setIcon(R.drawable.money)
                        .withIconAnimation(true)
                        .withDialogAnimation(true)
                        .setHeaderColor(R.color.colorPrimaryDark)
                        .setPositiveText("Of Course!")
                        .onPositive(new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                mRewardedVideoAd.show();
                            }
                        })
                        .setNegativeText("Not Now")
                        .onNeutral(new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                dialog.dismiss();
                            }
                        })
                        .show();

            } else {
                coinCount = coinCount - 30;
                mText.setText(String.valueOf(coinCount));

            }
        }
    });
}


private void loadRewardedVideoAd() {
    mRewardedVideoAd.loadAd(R.string.mypublisherid,
            new AdRequest.Builder().build());
}

@Override
public void onRewardedVideoAdLoaded() {

}

@Override
public void onRewardedVideoAdOpened() {

}

@Override
public void onRewardedVideoStarted() {

}

@Override
public void onRewardedVideoAdClosed() {
    loadRewardedVideoAd();

}

@Override
public void onRewarded(RewardItem rewardItem) {
    addCoins(rewardItem.getAmount());

}

private void addCoins(int amount) {
    coinCount += amount;
    mText.setText(" " + coinCount);

}

@Override
public void onRewardedVideoAdLeftApplication() {

}

@Override
public void onRewardedVideoAdFailedToLoad(int i) {

    Toast.makeText(BooksActivity.this, "Check your network connection", Toast.LENGTH_SHORT).show();

}
@Override
public void onResume() {
    mRewardedVideoAd.resume(this);
    super.onResume();
}

@Override
public void onPause() {
    mRewardedVideoAd.pause(this);
    super.onPause();
}

@Override
public void onDestroy() {
    mRewardedVideoAd.destroy(this);
    super.onDestroy();
}

public void startVideoAd(View view) {
    mRewardedVideoAd.show();
}
公共类BooksActivity扩展AppCompative实现RewardedVideoAdListener{
私人奖励视频;私人奖励视频;
私有文本查看多行文本;
私人整数硬币计数;
私人卡德维尤图书;
公共静态最终字符串PREFS\u NAME=“MyPrefsFile”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_books);
初始化(this,R.string.mypublisherid);
SharedReferences SharedReferences=GetSharedReferences(首选项名称,0);
coinCount=SharedReferences.getInt(“coins”,coinCount);
mText=(TextView)findViewById(R.id.mText);
硬币计数=0;
mText.setText(“+coinCount”);
SharedReferences SharedReferences=GetSharedReferences(首选项名称,0);
SharedReferences.Editor=SharedReferences.edit();
编辑:putInt(“硬币”,硬币计数);
commit();
mrewardvideoad=MobileAds.getRewardedVideoAdInstance(此);
mrewardvideoad.setRewardedVideoAdListener(此);
loadRewardedVideoAd();
book=(cardwiew)findviewbyd(R.id.book\u one);
book.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){

如果(coinCount您仅在onCreate中使用SharedReference编辑器保存coinCount,请在将其设置为0后立即保存。它按预期工作


在coinCount实际更改后尝试保存它。

每次创建
活动时,您都会将首选项值重新写入0。因此每次创建
活动时,您根据
共享引用的计数都是0


在函数
addCoins()中
您增加了值,但从不更新
SharedReferences
中的值。如果目标是在启动
活动的多个实例中保留您的值,则每次创建
活动时都需要停止将值重新写入0。然后,如果您希望更新增加的值,请n
SharedReferences
您需要在每次递增时更新
SharedReferences
中的值,或者每当您的
活动在
OnPause()
步骤1中暂停时,在onCreate中使用它之前初始化cointCount

private int cointCount = 0
第2步,不要覆盖
onCreate
中存储的值,因此从
onCreate
方法中删除这些行

coinCount = 0;
SharedPreferences sharedpreferences = getSharedPreferences(PREFS_NAME,0);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putInt("coins", coinCount);
editor.commit();
步骤3,在addCoins中更新它

private void addCoins(int amount) {
    coinCount += amount;
    mText.setText(" " + coinCount);

    SharedPreferences sharedpreferences = getSharedPreferences(PREFS_NAME,0);
    SharedPreferences.Editor editor = sharedpreferences.edit();
    editor.putInt("coins", coinCount);
    editor.apply();
}

或者,在暂停时将更新代码放入
onPause
,这样您就不会经常运行它了。

您的代码似乎没有问题,可能是因为您将coinCount设置为零,然后打印了?您是否使用断点对其进行了测试?尝试删除coinCount=0I之前在私有void addCoins中使用的共享首选项。它不起作用。关闭活动时在onCreate中它变成了0。您再次将它设置为0,并将其另存为0到首选项。在onCreate中删除putInt并提交,它没有任何用途。非常感谢。我之前忘记将coinCount删除为零