简体   繁体   中英

Change imagebutton with another activity

I want change ImageButtons from another event but that does not work This is my MainActivity:

boolean defaultValue = false; 

SharedPreferences prefs = getSharedPreferences("YourAppNamePrefs", MODE_PRIVATE);
boolean shouldChangeButton = prefs.getBoolean("shouldChangeButton", defaultValue);

if(shouldChangeButton) {
    ImageButton ib = (ImageButton) findViewById(R.id.imageButton10);
    ib.setVisibility(View.VISIBLE);
}

boolean defaultValue1 = false;

SharedPreferences prefs1 = getSharedPreferences("YourAppNamePrefs1",MODE_PRIVATE);
boolean shouldChangeButton1 = prefs1.getBoolean("shouldChangeButton1",defaultValue1);

if (shouldChangeButton1) {
    ImageButton ib1 = (ImageButton) findViewById(R.id.imageButton11);
    ib1.setVisibility(View.VISIBLE);
}

This is another event:

public void onClick7 (View view) {
    Intent myIntent = new Intent(view.getContext(), historiak.class);

    SharedPreferences prefs1 = getSharedPreferences("YourAppNamePrefs1", MODE_PRIVATE);
    prefs1.edit().putBoolean("shouldChangeButton1", true).apply();

    startActivity(myIntent);
    finish();
    MediaPlayer mediaPlayer = MediaPlayer.create(historia20.this, R.raw.dobre);
    mediaPlayer.start();
}

public void onClick8 (View view) {
    Intent myIntent = new Intent(view.getContext(), zemk.class);

    SharedPreferences prefs = getSharedPreferences("YourAppNamePrefs", MODE_PRIVATE);
    prefs.edit().putBoolean("shouldChangeButton", true).apply();

    startActivity(myIntent);
    finish();
    MediaPlayer mediaPlayer = MediaPlayer.create(zem20.this, R.raw.dobre);
    mediaPlayer.start();
}

When i run this app that write: Unfrotunately app has stopped

Please help me

I am looking your code and I don't see any line that you start your MainActivity.

just change it to

public void onClick7 (View view) {
    Intent myIntent = new Intent(view.getContext(), MainActivity.class);

    SharedPreferences prefs1 = getSharedPreferences("YourAppNamePrefs1", MODE_PRIVATE);
    prefs1.edit().putBoolean("shouldChangeButton1", true).apply();

    startActivity(myIntent);
    finish();

}

public void onClick8 (View view) {
    Intent myIntent = new Intent(view.getContext(), MainActivity.class);

    SharedPreferences prefs = getSharedPreferences("YourAppNamePrefs", MODE_PRIVATE);
    prefs.edit().putBoolean("shouldChangeButton", true).apply();

    startActivity(myIntent);
    finish();

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM