简体   繁体   English

AudioManager NullPointerException对于发行版本

[英]AudioManager NullPointerException for Release version

When I run my code in Debug from Android Studio my app work good, but when I create the APK and install it on device and start my app, the app get NullPointerException error and get killed( using Android Debug Monitor). 当我在Android Studio的Debug中运行代码时,我的应用程序运行良好,但是当我创建APK并将其安装在设备上并启动我的应用程序时,该应用程序会收到NullPointerException错误并被杀死(使用Android Debug Monitor)。 Here is the code: 这是代码:

public class MusicService extends IntentService {

    boolean INTERNET_CONNECTION = false;

    boolean COMMAND_DOWNALOAD_SDCARD = false;

    String TABLE_NAME = "";
    String COMMAND_ARGUMENT = "";

    // Here throw sometimes the error - Line 42
    AudioManager audio;

    MediaPlayer mp = new MediaPlayer();
    long TIME_ELAPSED = 0;
    long TIME_STARTED = 0;

    boolean ALIVE = true;
    long SLEEP_TIME = 5000;

    String SERVER_URL = "http://xxx.xxx";

    public MusicService() {
    super("Service");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
    Log.d("SERVICE", "STARTED");

    initialize();
    initializeTimer();

    while (ALIVE) {
        ...
    }
    }

    private void initialize() {
    // Get phone number set the table name
    TABLE_NAME = "t" + getPhoneNumber();

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

    // Wait for internet connection
    INTERNET_CONNECTION = waitInternetConnection();

    Log.d("INITIALIZE", "COMPLETED");
    }

    ...
}

I start my app when boot is completed. 启动完成后,我启动了我的应用程序。 After some times of trying to run it I got error at line 42 and I changed the line from: 经过几次尝试运行后,我在第42行出现错误,并将行从以下位置更改:

    AudioManager audio = null;

to

    AudioManager audio;

But still get the error but now don't show any more the line. 但是仍然会收到错误,但现在不再显示该行。

There is no difference between these: AudioManager audio = null; 它们之间没有区别: AudioManager audio = null; and AudioManager audio; AudioManager audio; .

Changing the line from explicitly assigning null to the object doesn't change any functionality at all; 将行从显式分配null更改为对象根本不会更改任何功能; all it means is that audio now implicitly defaults to null . 这意味着audio现在隐式默认为null

Clearly there is a difference in what getSystemService() does, depending on whether you're in your debugging environment versus your production environment. 显然, getSystemService()功能有所不同,具体取决于您是在调试环境中还是在生产环境中。 To find out what's wrong, go into that service. 要找出问题所在,请进入该服务。 We don't have enough information here to debug it for you. 我们这里没有足够的信息来为您调试。

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

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

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