我的应用程序是一个大亨游戏,用户点击按钮以增加他的分数,因此他购买升级以增加他的快速得分。
好吧,出于好奇,我卸载了我的应用程序并通过Android Studio重新安装它,当我运行它时,应用程序无效。我试着点击按钮来增加分数,但仍然没有增加。为了保存用户分数,我在关闭应用后使用 SharedPreferences 。也许这与它有关?
Android Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.navjeevenmann.mytycoon">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity" />
<activity android:name=".ThirdActivity"></activity>
</application>
主要活动:
package com.example.navjeevenmann.mytycoon;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import static com.example.navjeevenmann.mytycoon.R.id.textView;
public class MainActivity extends AppCompatActivity {
private ImageButton myButton;
private int Counter;
private ImageButton autoclick;
private TextView myTextView;
Handler handler = new Handler();
private int add;
private TextView myAddView;
private ImageButton singleclick;
private int Singleadd = 1;
private ImageButton money;
private boolean[] array;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
getSupportActionBar().hide();
super.onCreate(savedInstanceState);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
if (bundle.containsKey("Count")) {
Counter = bundle.getInt("Count");
}
if (bundle.containsKey("Add")) {
add = bundle.getInt("Add");
}
if (bundle.containsKey("Single")) {
Singleadd = bundle.getInt("Single");
}
if (bundle.containsKey("array")) {
array = bundle.getBooleanArray("array");
}
}
add = getaddvalue();
Counter = getvalue();
Singleadd = getSinglevalue();
money = (ImageButton) findViewById(R.id.imageButton2);
myButton = (ImageButton) findViewById(R.id.button);
autoclick = (ImageButton) findViewById(R.id.autoclick);
singleclick = (ImageButton) findViewById(R.id.singleclick);
myTextView = (TextView) findViewById(textView);
myAddView = (TextView) findViewById(R.id.textView2);
myButton.setBackgroundColor(0);
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Counter = ButtonCounter(Counter);
}
});
autoclick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
intent.putExtra("Count", Counter);
intent.putExtra("Add", add);
intent.putExtra("Single", Singleadd);
intent.putExtra("array", array);
startActivity(intent);
}
});
singleclick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), ThirdActivity.class);
intent.putExtra("Count", Counter);
intent.putExtra("Single", Singleadd);
intent.putExtra("Add", add);
intent.putExtra("array", array);
startActivity(intent);
}
});
}
public void onStop() {
super.onStop();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("usermoney", Counter);
editor.putInt("Auto", add);
editor.putInt("Single", Singleadd);
editor.commit();
}
public int getvalue() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
return prefs.getInt("usermoney",0);
}
public int getaddvalue() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
return prefs.getInt("Auto", 0);
}
public int getSinglevalue() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
return prefs.getInt("Single", 0);
}
public void onResume() {
super.onResume();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Counter = AutoCounter(Counter, add);
handler.postDelayed(this, 1000);
}
}, 1000);
handler.postDelayed(new Runnable() {
@Override
public void run() {
Display(Counter, add);
handler.postDelayed(this, 1);
}
}, 50);
}
public int ButtonCounter(int Counter) {
Counter += Singleadd;
return Counter;
}
public int AutoCounter(int Counter, int add) {
Counter = Counter + add;
return Counter;
}
public void Display(int Counter, int add) {
String money = String.valueOf(Counter);
myTextView.setText(":" + money);
myAddView.setText("$" + add + "/Per Second");
}
}
谢谢