Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.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 使用其他活动更改imagebutton_Java_Android - Fatal编程技术网

Java 使用其他活动更改imagebutton

Java 使用其他活动更改imagebutton,java,android,Java,Android,我想从另一个事件更改ImageButtons,但这不起作用 这是我的主要活动: boolean defaultValue = false; SharedPreferences prefs = getSharedPreferences("YourAppNamePrefs", MODE_PRIVATE); boolean shouldChangeButton = prefs.getBoolean("shouldChangeButton", defaultValue); if(shouldCha

我想从另一个事件更改
ImageButtons
,但这不起作用 这是我的主要活动:

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);
}
这是另一个事件:

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();
}
当我运行此应用程序时,write:unprotunaly应用程序已停止


请帮助我

我正在查看您的代码,但我没有看到您启动main活动的任何行

把它改成

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();

}

你好,LogCat请问zemk.class或historiak.class在哪里?您启动了此活动,并且希望在MainActivity中进行更改?我想如果您只启动MainActivity,它将起作用。不,这不起作用