Android应用不改变音量

时间:2014-12-29 17:55:49

标签: java android

所以我试图创建一个简单的应用程序,静音媒体卷然后关闭自己。当我运行应用程序时,我首先得到两个问题,它没有改变音量,第二个它声称应用程序在我的手机上崩溃,我不想发生。她的代码:

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();
    }

    }

如果有帮助,这里是错误日志的副本:

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)

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

堆栈跟踪告诉你该怎么做......

  

android.util.SuperNotCalledException:Activity {com.example.scielencetester / com.example.scielencetester.MainActivity}没有调用super.onCreate()

onCreate()方法中,调用超类方法(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();
}