title: Android Studio 3.2.1: Silent Mode Toggle app crashes when clicked even though no error in code
body: <p>I'm just starting to learn coding for Android App by following the book Android App Development for Dummies to create the Silent Mode Toggle App.</p>
  
  <p>Everything seems fine in the code (no error except for warning that:</p>
  
  <hr>
  
  <p>"Casting 'findViewById(R.id.phone_icon)' to 'ImageView' is redundant.
  This inspection reports unnecessary cast expressions."</p>
  
  <hr>
  
  <p>I have read through a similar problem here (<a href="https://stackoverflow.com/questions/22918342/application-crashes-silent-mode-toggle-android-for-dummies">Application Crashes - Silent Mode Toggle - Android for Dummies</a>) and it says to try:</p>
  
  <p>1) Change "extends ActionBarActivity" to just "extends Activity" and import - <strong>mine is already as such.</strong></p>
  
  <p>2) delete or comment the 'if' in the onCreate method out - <strong>mine don't have this section.</strong></p>
  
  <p>3) change the parameter of the setContentView to: R.layout.fragment_main - <strong>not very sure what this means but don't seem to be relevant to my code?</strong> (his codes and mine are slightly different)</p>
  
  <p><strong>MainActivity.java Code</strong></p>
  
  <pre><code>package com.dummies.silentmodetoggle;
  
  import android.app.Activity;
  import android.media.AudioManager;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.FrameLayout;
  import android.widget.ImageView;
  
  import com.dummies.silentmodetoggle.util.RingerHelper;
  
  public class MainActivity extends Activity {
  
  AudioManager audioManager;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
  
      audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
  
      setContentView(R.layout.activity_main);
  
      FrameLayout contentView =
              (FrameLayout) findViewById(R.id.content);
  
  
      contentView.setOnClickListener(new View.OnClickListener() {
          public void onClick(View v) {
  
              RingerHelper.performToggle(audioManager);
  
  
              updateUi();
          }
      });
  }
  
  
  private void updateUi() {
  
      ImageView imageView = (ImageView) findViewById(R.id.phone_icon);
  
      int phoneImage = RingerHelper.isPhoneSilent(audioManager)
              ? R.mipmap.ringer_off
              : R.mipmap.ringer_on;
  
  
      imageView.setImageResource(phoneImage);
      }
  
  @Override
  protected void onResume(){
      super.onResume();
  
      // Update our UI in case anything has changed.
      updateUi();
      }
  }
  </code></pre>
  
  <p><strong>RingerHelper.java</strong></p>
  
  <p>The book says to create a java file at: "src/main/java/com/dummies/silentmodetoggle/util/RingerHelper.java" but did not state how. Since I do not have a util folder so I'd created a package (New>Package) at "src/main/java/com.dummies.silentmodetoggle" and added the RingerHelper java file in the util folder. Note sure if this is the problem? The code is as below:</p>
  
  <pre><code>package com.dummies.silentmodetoggle.util;
  
  import android.media.AudioManager;
  
  public class RingerHelper {
  // private to prevent users from creating a RingerHelper object
  private RingerHelper(){}
  
  /* Toggles the phone's silent mode */
  public static void performToggle(AudioManager audioManager) {
      // If the phone is currently silent, then unsilence it. If
      //  it's currently normal, then silence it.
      audioManager.setRingerMode(
              isPhoneSilent(audioManager)
                      ? AudioManager.RINGER_MODE_NORMAL
                      : AudioManager.RINGER_MODE_SILENT);
  }
  
  /* Returns whether the phone is currently in silent mode. */
  public static boolean isPhoneSilent(AudioManager audioManager){
      return audioManager.getRingerMode()
              == AudioManager.RINGER_MODE_SILENT;
  }
  }
  </code></pre>
  
  <p><strong>Error from LogCat when I clicked the button on app</strong></p>
  
  <pre><code>2018-12-01 22:11:44.029 30122-30122/com.dummies.silentmodetoggle E/AndroidRuntime: FATAL EXCEPTION: main
  Process: com.dummies.silentmodetoggle, PID: 30122
  java.lang.SecurityException: Not allowed to change Do Not Disturb state
      at android.os.Parcel.readException(Parcel.java:1683)
      at android.os.Parcel.readException(Parcel.java:1636)
      at android.media.IAudioService$Stub$Proxy.setRingerModeExternal(IAudioService.java:962)
      at android.media.AudioManager.setRingerMode(AudioManager.java:1022)
      at com.dummies.silentmodetoggle.util.RingerHelper.performToggle(RingerHelper.java:13)
      at com.dummies.silentmodetoggle.MainActivity$1.onClick(MainActivity.java:60)
      at android.view.View.performClick(View.java:5610)
      at android.view.View$PerformClick.run(View.java:22265)
      at android.os.Handler.handleCallback(Handler.java:751)
      at android.os.Handler.dispatchMessage(Handler.java:95)
      at android.os.Looper.loop(Looper.java:154)
      at android.app.ActivityThread.main(ActivityThread.java:6077)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
  2018-12-01 22:11:44.672 1315-1315/? E/EGL_emulation: tid 1315: eglCreateSyncKHR(1901): error 0x3004 (EGL_BAD_ATTRIBUTE)
  </code></pre>
  
  <p>Otherwise the app seems to work fine, ie, when I click on the volume button of the device itself to silent, the app image will change to silent and vice versa. It just crashes when I try to click on the image of the app itself.</p>
  
  <p>I really have no idea what's going on. Please help. Thanks very much!</p>