简体   繁体   中英

Android app not changing volume

so I am trying to make a simple app that mutes the media volume then closes itself. When I run the app I get two problems first it does not change the volume, and second it claims the app crashed on my phone witch I don't want to happen. Hers my code:

package com.example.scielencetester;

import android.support.v7.app.ActionBarActivity;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;



public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        AudioManager mgr=null;
        mgr=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
        mgr.setStreamVolume(AudioManager.STREAM_MUSIC, 0, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
        finish();
    }

    }

If it helps here is a copy of the error log:

12-29 09:49:20.698: E/AndroidRuntime(23766): FATAL EXCEPTION: main
12-29 09:49:20.698: E/AndroidRuntime(23766): Process: com.example.scielencetester, PID: 23766
12-29 09:49:20.698: E/AndroidRuntime(23766): android.util.SuperNotCalledException: Activity {com.example.scielencetester/com.example.scielencetester.MainActivity} did not call through to super.onCreate()
12-29 09:49:20.698: E/AndroidRuntime(23766):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2254)
12-29 09:49:20.698: E/AndroidRuntime(23766):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
12-29 09:49:20.698: E/AndroidRuntime(23766):    at android.app.ActivityThread.access$800(ActivityThread.java:144)
12-29 09:49:20.698: E/AndroidRuntime(23766):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
12-29 09:49:20.698: E/AndroidRuntime(23766):    at android.os.Handler.dispatchMessage(Handler.java:102)
12-29 09:49:20.698: E/AndroidRuntime(23766):    at android.os.Looper.loop(Looper.java:135)
12-29 09:49:20.698: E/AndroidRuntime(23766):    at android.app.ActivityThread.main(ActivityThread.java:5221)
12-29 09:49:20.698: E/AndroidRuntime(23766):    at java.lang.reflect.Method.invoke(Native Method)
12-29 09:49:20.698: E/AndroidRuntime(23766):    at java.lang.reflect.Method.invoke(Method.java:372)
12-29 09:49:20.698: E/AndroidRuntime(23766):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
12-29 09:49:20.698: E/AndroidRuntime(23766):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

Thanks in advance for your help.

The stack trace is telling you what to do...

android.util.SuperNotCalledException: Activity {com.example.scielencetester/com.example.scielencetester.MainActivity} did not call through to super.onCreate()

In your onCreate() method, call the superclass method ( super.onCreate(...) )

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    AudioManager mgr=null;
    mgr=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
    mgr.setStreamVolume(AudioManager.STREAM_MUSIC, 0, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
    finish();
}

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