简体   繁体   中英

App get Crash when press Save Button

i have an issue regarding saving the volume changed when i press a button, and do not understand why. Could you please help pls? My code Below:

    private AudioManager aManager;
    private SeekBar seekVolume;
    private Button save;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        aManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        Button save = (Button) findViewById(R.id.save);

}
    public void saveOnClickListener(View view){

            int progress = 1;
            int keyCode = 0;
            KeyEvent keyEvent;
                aManager.setStreamVolume(AudioManager.STREAM_RING, progress  , AudioManager.FLAG_SHOW_UI + AudioManager.FLAG_PLAY_SOUND);
                if(keyCode==KeyEvent.KEYCODE_VOLUME_DOWN || keyCode==KeyEvent.KEYCODE_VOLUME_UP){
    seekVolume.setProgress(aManager.getStreamVolume(AudioManager.STREAM_RING));

            }else if (keyCode==KeyEvent.KEYCODE_VOLUME_DOWN || keyCode==KeyEvent.KEYCODE_VOLUME_UP){
                seekVolume.setProgress(aManager.getStreamVolume(AudioManager.STREAM_NOTIFICATION));

                }
 }

}

First i want to make changes for Ringer Volume, and when press Save Button , to commit the changes. Thanks guys

i think in xml u have not add this as u have used method saveOnClickListener add in xml in Button property as

 android:onClick="saveOnClickListener" 

or either u can use in activity as shown below if u have not added in xml

 Button save = (Button) findViewById(R.id.save);    
 save.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
          // do here what u want onclick of save button 
               }
        }


    });

you have made the aManager local in oncrete so it's global variable with same name would still null in saveOnClickListener

instead of this

 AudioManager aManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

use

aManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

in Oncreate...........

You better make aManager and seekVolume global and remove

    SeekBar seekVolume = (SeekBar) findViewById(R.id.seekVolume);

in the onClickListener

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