null pointer exception at getRingerMode()

前端 未结 2 484
野性不改
野性不改 2021-01-28 17:37

I wrote this app where users can toggle silent mode by clicking on an image button: package p.a;

import android.media.AudioManager;
import android.support.v7         


        
相关标签:
2条回答
  • 2021-01-28 17:59

    You can do it as Vamshi Krishna says, it's totally a good answer for this problem, but if you don't want to instantiate it for some reason you could change "Mode class" to a "Mode static class" as follows:

    package p.a;
    
    import android.media.AudioManager;
    
    /**
     * Created by root on 9/19/17.
     */
    
    public static class mode {
        public static boolean phonesilent(AudioManager audioManager){
            return audioManager.getRingerMode()==AudioManager.RINGER_MODE_SILENT;
        }
        public static void toggle(AudioManager audioManager){
            int mode = phonesilent(audioManager)?
                    AudioManager.RINGER_MODE_NORMAL:
                    AudioManager.RINGER_MODE_SILENT;
        }
    }
    

    Hope it helps!

    0 讨论(0)
  • 2021-01-28 18:01

    Firstly create a Mode Object like this :

    Mode mode = new Mode();
    
    0 讨论(0)
提交回复
热议问题