简体   繁体   English

Android应用未更改音量

[英]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() android.util.SuperNotCalledException:活动{com.example.scielencetester / com.example.scielencetester.MainActivity}没有调用super.onCreate()

In your onCreate() method, call the superclass method ( 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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM