0
package p.a;

import android.media.AudioManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    ImageView imageView;
    AudioManager audioManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final ImageView imageView = (ImageView) findViewById(R.id.imageView);
        final AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int mode = audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT? AudioManager.RINGER_MODE_NORMAL:
                        AudioManager.RINGER_MODE_SILENT;
                audioManager.setRingerMode(mode);
                int imageid = audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT?
                        R.drawable.ringer_off:R.drawable.ringer_on;
                imageView.setImageResource(imageid);
            }


        });
    }
    public void onResume() {
        super.onResume();
        int imageid = audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT?
                R.drawable.ringer_off:R.drawable.ringer_on;
        imageView.setImageResource(imageid);
    }
}

See the title for the error message I received. I can't see any flaws in my code, other than hard coding, but that's irrelevant. Anyone help me please?See the title for the error message I received. I can't see any flaws in my code, other than hard coding, but that's irrelevant. Anyone help me please?See the title for the error message I received. I can't see any flaws in my code, other than hard coding, but that's irrelevant. Anyone help me please?See the title for the error message I received. I can't see any flaws in my code, other than hard coding, but that's irrelevant. Anyone help me please?

dave
  • 11,641
  • 5
  • 47
  • 65
pxc3110
  • 243
  • 3
  • 14

1 Answers1

0

You are creating two AudioManagers. Change the line:

final AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);

to:

audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);

This will ensure you use the AudioManager that has been declared and initialised.

Kyle Higginson
  • 922
  • 6
  • 11
  • I've fixed that problem. But around the onResume method it says Error:(38, 5) error: class, interface, or enum expected – pxc3110 Sep 19 '17 at 23:21