‘I am Making an Earning App
I Added Spin wheel “spinner Activity” but the score counter is on “Main Activity” so i want to Add Coins On Textview which is present in Main Activity
MAIN ACTIVITY
And This Is my spinner Activity code
but when i run this score app crashes only when i click on spin the wheel button in xml’
CardView redeem;
CardView watchad;
CardView shareit;
CardView spinner;
private static final String PREFS_NAME = "SavedValue";
private static final String COIN_COUNT_KEY = "coinCount";
private TextView coinTextView;
private int coinCount;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
redeem = findViewById(R.id.reddemm);
spinner = findViewById(R.id.spinner);
coinTextView = findViewById(R.id.txt45);
shareit = findViewById(R.id.shareit);
watchad = findViewById(R.id.watchad);
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
coinCount = prefs.getInt(COIN_COUNT_KEY, 0);
coinTextView.setText(String.valueOf(coinCount));
redeem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this,redeem.class));
}
});
spinner.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this,spinner.class));
}
});
shareit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
coinCount += 500;
coinTextView.setText(String.valueOf(coinCount));
SharedPreferences.Editor editor = getSharedPreferences(PREFS_NAME, MODE_PRIVATE).edit();
editor.putInt(COIN_COUNT_KEY, coinCount);
editor.apply();
}
});
watchad.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
}
private static final String PREFS_NAME = "SavedValue";
private static final String COIN_COUNT_KEY = "coinCount";
private TextView coinTextView;
private int coinCount;
List<LuckyItem> data = new ArrayList<>();
private int coin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner);
coinTextView = findViewById(R.id.txt45);
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
coinCount = prefs.getInt(COIN_COUNT_KEY, 0);
coinTextView.setText(String.valueOf(coinCount));
ImageView imageView = findViewById(R.id.imageView11);
imageView.setOnClickListener(v -> onBackPressed());
final LuckyWheelView luckyWheelView = findViewById(R.id.luckyWheel);
findViewById(R.id.play).setEnabled(true);
findViewById(R.id.play).setAlpha(1f);
LuckyItem luckyItem1 = new LuckyItem();
luckyItem1.text = "5";
luckyItem1.color = Color.parseColor("#8574F1");
data.add(luckyItem1);
LuckyItem luckyItem2 = new LuckyItem();
luckyItem2.text = "20";
luckyItem2.color = Color.parseColor("#8E84FF");
data.add(luckyItem2);
LuckyItem luckyItem3 = new LuckyItem();
luckyItem3.text = "30";
luckyItem3.color = Color.parseColor("#752BEF");
data.add(luckyItem3);
LuckyItem luckyItem4 = new LuckyItem();
luckyItem4.text = "0";
luckyItem4.color = ContextCompat.getColor(getApplicationContext(), R.color.Spinwell140);
data.add(luckyItem4);
LuckyItem luckyItem5 = new LuckyItem();
luckyItem5.text = "50";
luckyItem5.color = Color.parseColor("#8574F1");
data.add(luckyItem5);
LuckyItem luckyItem6 = new LuckyItem();
luckyItem6.text = "100";
luckyItem6.color = Color.parseColor("#8E84FF");
data.add(luckyItem6);
LuckyItem luckyItem7 = new LuckyItem();
luckyItem7.text = "200";
luckyItem7.color = Color.parseColor("#752BEF");
data.add(luckyItem7);
LuckyItem luckyItem8 = new LuckyItem();
luckyItem8.text = "0";
luckyItem8.color = ContextCompat.getColor(getApplicationContext(), R.color.Spinwell140);
data.add(luckyItem8);
luckyWheelView.setData(data);
luckyWheelView.setRound(getRandomRound());
findViewById(R.id.play).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int index = getRandomIndex();
luckyWheelView.startLuckyWheelWithTargetIndex(index);
findViewById(R.id.play).setEnabled(false);
findViewById(R.id.play).setAlpha(.5f);
}
});
luckyWheelView.setLuckyRoundItemSelectedListener(index -> {
if (index ==1 ){
coin = 5;
coinCount += 5;
coinTextView.setText(String.valueOf(coinCount));
SharedPreferences.Editor editor = getSharedPreferences(PREFS_NAME, MODE_PRIVATE).edit();
editor.putInt(COIN_COUNT_KEY, coinCount);
editor.apply();
} if (index ==2 ){
coin = 20;
coinCount += 20;
coinTextView.setText(String.valueOf(coinCount));
SharedPreferences.Editor editor = getSharedPreferences(PREFS_NAME, MODE_PRIVATE).edit();
editor.putInt(COIN_COUNT_KEY, coinCount);
editor.apply();
} if (index ==3 ){
coin = 30;
coinCount += 30;
coinTextView.setText(String.valueOf(coinCount));
SharedPreferences.Editor editor = getSharedPreferences(PREFS_NAME, MODE_PRIVATE).edit();
editor.putInt(COIN_COUNT_KEY, coinCount);
editor.apply();
} if (index ==4 ){
coin = 0;
coinCount += 0;
coinTextView.setText(String.valueOf(coinCount));
SharedPreferences.Editor editor = getSharedPreferences(PREFS_NAME, MODE_PRIVATE).edit();
editor.putInt(COIN_COUNT_KEY, coinCount);
editor.apply();
} if (index ==5){
coin = 50;
coinCount += 50;
coinTextView.setText(String.valueOf(coinCount));
SharedPreferences.Editor editor = getSharedPreferences(PREFS_NAME, MODE_PRIVATE).edit();
editor.putInt(COIN_COUNT_KEY, coinCount);
editor.apply();
} if (index ==6 ){
coin = 100;
coinCount += 100;
coinTextView.setText(String.valueOf(coinCount));
SharedPreferences.Editor editor = getSharedPreferences(PREFS_NAME, MODE_PRIVATE).edit();
editor.putInt(COIN_COUNT_KEY, coinCount);
editor.apply();
} if (index ==7 ){
coin = 200;
coinCount += 200;
coinTextView.setText(String.valueOf(coinCount));
SharedPreferences.Editor editor = getSharedPreferences(PREFS_NAME, MODE_PRIVATE).edit();
editor.putInt(COIN_COUNT_KEY, coinCount);
editor.apply();
} if (index ==8 ){
coin = 0;
coinCount += 0;
coinTextView.setText(String.valueOf(coinCount));
SharedPreferences.Editor editor = getSharedPreferences(PREFS_NAME, MODE_PRIVATE).edit();
editor.putInt(COIN_COUNT_KEY, coinCount);
editor.apply();
}
Toast.makeText(getApplicationContext(), String.valueOf("+ " + coin +" Coins"), Toast.LENGTH_SHORT).show();
findViewById(R.id.play).setEnabled(true);
findViewById(R.id.play).setAlpha(1f);
});
}
private int getRandomIndex() {
int[] ind = new int[] {1,2,3,4,5,6,7,8};
int rand = new Random().nextInt(ind.length);
return ind[rand];
}
private int getRandomRound() {
Random rand = new Random();
return rand.nextInt(10) + 15;
}
}
New contributor
Laksh Diyora is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.