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). 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:
AudioManager audio = null;
to
AudioManager audio;
But still get the error but now don't show any more the line.