unable to create a service - android

I have a music player application and untill now, it was working smoothly.
So, i learnt about Android Focus from android documentation and i thought i would try this on my app for learning purposes.
App: minimum api level :16; Maximum:26;
I basically implemented the AudioManager.OnAudioFocusChangeListener in my service class.
In it i included this code:
if(Build.VERSION.SDK_INT>=26){
mAudioFocusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
.setAudioAttributes(mAudioAttributes)
.setAcceptsDelayedFocusGain(true)
.setOnAudioFocusChangeListener(this)
.build();
int res = mAudioManager.requestAudioFocus(mAudioFocusRequest);
synchronized(mFocusLock) {
if (res == AudioManager.AUDIOFOCUS_REQUEST_FAILED) {
mPlaybackNowAuthorized = false;
} else if (res == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
mPlaybackNowAuthorized = true;
playSong();
} else if (res == AudioManager.AUDIOFOCUS_REQUEST_DELAYED) {
mPlaybackDelayed = true;
mPlaybackNowAuthorized = false;
}
}
} else if(Build.VERSION.SDK_INT<26){
int result = 0;
if (mAudioManager != null) {
result = mAudioManager.requestAudioFocus(this,AudioManager.STREAM_MUSIC,AudioManager.AUDIOFOCUS_GAIN);
}
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
mPlaybackNowAuthorized = true;
playSong();
}
}
My onAudioFocusChange(int focusChange) looks something like this:
if(Build.VERSION.SDK_INT<26){
//<26 code
}else {
//26 and above api level
}
I tried implementing audiofocus in my application and this is how i did it. The issue is when i try to run my app now , it gives me this error
unable to create a service
What's the problem with it? i think i read the documentation clearly.
Am i missing out something?
Update:
Removing the below code does make it work
else if(Build.VERSION.SDK_INT<26){
int result = 0;
if (mAudioManager != null) {
result = mAudioManager.requestAudioFocus(this,AudioManager.STREAM_MUSIC,AudioManager.AUDIOFOCUS_GAIN);
}
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
mPlaybackNowAuthorized = true;
playSong();
}
Stacktrace:
12-18 16:27:47.740 30183-30183/com.example.tilak.imusicplay E/AndroidRuntime:
12-18 16:27:47.740 30183-30183/com.example.tilak.imusicplay E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.tilak.imusicplay, PID: 30183
java.lang.RuntimeException: Unable to create service com.example.tilak.imusicplay.MusicService: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2920)
at android.app.ActivityThread.access$2000(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1458)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:5529)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference
at com.example.tilak.imusicplay.MusicService.playSong(MusicService.java:239)
at com.example.tilak.imusicplay.MusicService.initMusicPlayer(MusicService.java:115)
at com.example.tilak.imusicplay.MusicService.onCreate(MusicService.java:66)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2910)
at android.app.ActivityThread.access$2000(ActivityThread.java:153) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1458) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:5529) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629) 
12-18 16:27:47.742 30183-30183/com.example.tilak.imusicplay E/MQSEventManagerDelegate: failed to get MQSService.
Whole Class Code

look your Error Msg!this is a NullPointerException. List is a null object! check you code
java.lang.RuntimeException: Unable to create service com.example.tilak.imusicplay.MusicService: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object

The problem is that your application does not initialise songs. (Sure, there is a setSongs method, but you don't call it!)
Solution: add code to initialize songs in the appropriate place. That will fix this error ...
I strongly advise you to read the Question and Answers for:
What is a NullPointerException, and how do I fix it?

Related

Attempt to invoke virtual method 'internal.policy.DecorView.getWindowInsetsController()' on a null object reference

My application is giving an Error in the opening file. But this bug is only api 30 channel. This is the fault:
Attempt to invoke virtual method 'android.view.WindowInsetsController com.android.internal.policy.DecorView.getWindowInsetsController()' on a null object reference
When I put the application in the Goggle Robo test, it gives the following error. But this error occurs only in the api 30s. It works correctly in other apes. I would be glad if you could help. Thank you.
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mobilprogramlar.bilmecebahcesi/com.mobilprogramlar.bilmecebahcesi.AcilisEkrani}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.WindowInsetsController com.android.internal.policy.DecorView.getWindowInsetsController()' on a null object reference
FATAL EXCEPTION: main
Process: com.mobilprogramlar.bilmecebahcesi, PID: 13816
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mobilprogramlar.bilmecebahcesi/com.mobilprogramlar.bilmecebahcesi.AcilisEkrani}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.WindowInsetsController com.android.internal.policy.DecorView.getWindowInsetsController()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.WindowInsetsController com.android.internal.policy.DecorView.getWindowInsetsController()' on a null object reference
at com.android.internal.policy.PhoneWindow.getInsetsController(PhoneWindow.java:3880)
at com.mobilprogramlar.bilmecebahcesi.AcilisEkrani.onCreate(AcilisEkrani.java:18)
at android.app.Activity.performCreate(Activity.java:8000)
at android.app.Activity.performCreate(Activity.java:7984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
... 11 more
java.util.ConcurrentModificationException
FATAL EXCEPTION: main
Process: com.google.android.googlequicksearchbox:search, PID: 9903
java.util.ConcurrentModificationException
at java.util.LinkedList$ListItr.checkForComodification(LinkedList.java:966)
at java.util.LinkedList$ListItr.next(LinkedList.java:888)
at com.google.android.apps.gsa.shared.util.debug.a.g.a(SourceFile:30)
at com.google.android.apps.gsa.shared.util.debug.a.g.a(SourceFile:135)
at com.google.android.apps.gsa.contentprovider.initializer.c.dump(SourceFile:6)
at android.app.ActivityThread.handleDumpProvider(ActivityThread.java:4307)
at android.app.ActivityThread.access$2400(ActivityThread.java:237)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2013)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
The opening file is the file with the error.
public class AcilisEkrani extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//requestWindowFeature(Window.FEATURE_NO_TITLE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
**final WindowInsetsController insetsController = getWindow().getInsetsController();**
if (insetsController != null) {
insetsController.hide(WindowInsets.Type.statusBars());
}
} else {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
setContentView(R.layout.acilisekrani);
// Thread
Thread timer = new Thread() {
#Override
public void run() {
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
Log.i("tago","Zamanlayıcı Çalışmadı");
} finally {
Intent i = new Intent(AcilisEkrani.this,UygulamaUrunlerimiz.class);
startActivity(i);
AcilisEkrani.this.finish();
finish();
}
}
};
// Thread
timer.start();
}
#Override
protected void onPause() {
super.onPause();
finish();
}
}
You probably should call setContentView() before you try to access the window. According to official documentation, NullPointerException will only be if the is nothing to render.
put your logic inside => onWindowFocusChanged
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus && Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
WindowInsetsController controller = getWindow().getInsetsController();
if(controller != null) {
controller.setSystemBarsAppearance(WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS, WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS);
// controller.setSystemBarsBehavior(WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS);
}
} else {
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
setWindowFlag(this, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, false);
// make fully Android Transparent Status bar
getWindow().setStatusBarColor(Color.TRANSPARENT);
}
}

Crash on getSystemService(Context.CAMERA_SERVICE)

I have a widget with a button that enables the camera service.
It works quite well but after some time (maybe with the home screen app exit) it loses the status (bein on or off).
I have a crash with a NullPointerException from, I believe, Context.CAMERA_SERVICE. It is really hard to reproduce and so far I haven't found the sequence that leads to the crash.
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
at android.content.ContextWrapper.getSystemService(ContextWrapper.java:714)
at com.widget.tst.Widget.CallbackWidgetService.isCameraInUse(CallbackWidgetService.java:163)
at com.widget.tst.Widget.CallbackWidgetService.startCameraInUse(CallbackWidgetService.java:242)
//** button click-->
at com.widget.tst.Widget.CameraWidgetReceiver.updateWidgetButton(CameraWidgetReceiver.java:55)
at com.widget.tst.Widget.CameraWidgetReceiver.onReceive(CameraWidgetReceiver.java:32)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:3632)
at android.app.ActivityThread.-wrap18(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1975) 
at android.os.Handler.dispatchMessage(Handler.java:109) 
at android.os.Looper.loop(Looper.java:166) 
at android.app.ActivityThread.main(ActivityThread.java:7367) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:469) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:963)
How can Context.CAMERA_SERVICE be null?
private void isCameraInUse(){
if(context == null) return;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
--> line 163 mCameraManager = (CameraManager)getSystemService(Context.CAMERA_SERVICE);
}
else{
Toast.makeText(context, "You need to run Android version "+
Build.VERSION_CODES.M+" or above",
Toast.LENGTH_SHORT).show();
return;
}
 
It looks like somewhere inside your current class (this) it's own context gets nulled, and that's why you get your NPE.
But since you have a 100% non-null context in a variable (if(context == null) return;), why don;t you use it instead of this's own context?:
mCameraManager = (CameraManager)context.getSystemService(Context.CAMERA_SERVICE);

Sinch client wont start?

I am trying to use sinch an i am having erros i dont know why an i seem to have set it well.
and it is pointing me to this part of the code which
public void that(final String name) {
call.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if ((getSinchServiceInterface() == null) || (!getSinchServiceInterface().isStarted())) {
getSinchServiceInterface().startClient(name);
}
}
});
}
the "getSinchServiceInterface().startClient(name);"
this is my error
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.obi.thinker.logins.call.SinchService$SinchServiceInterface.startClient(java.lang.String)' on a null object reference
at com.obi.thinker.logins.tabs.Chatting$caller$1.onClick(Chatting.java:386)
at android.view.View.performClick(View.java:5265)
at android.view.View$PerformClick.run(View.java:21534)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5683)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
I dont understand
if ((getSinchServiceInterface() != null && !getSinchServiceInterface().isStarted())) {
getSinchServiceInterface().startClient(name);
}
the callstack says:
getSinchServiceInterface() is null
And would suggest to read this:
https://stackoverflow.com/a/41477703/1979882
I was facing this issue for weeks. I literally rewrote my SinchService from scratch, removed Sinch altogether and tried starting fresh but all to no avail. It turns out I was simply initialising my call too early.
I recommend you move the code block:
if ((getSinchServiceInterface() != null || !getSinchServiceInterface().isStarted())) {
getSinchServiceInterface().startClient(name);
}
I moved mine to the very end of my onResume method and it seems to all work fine now. The reason this doesn't continually keep starting the client is because we are adding the 'if' statement to make sure it isn't already initialised.
If you find that the issue is still thrown, which was the case for me at one point, try calling it at the very end of the launch of your activity. It simply comes down to being called before your BaseActivity has a chance to initialise mSinchServiceInterface

Attempt to write to field 'android.hardware.camera2.ICameraDeviceUser on a null object reference

I use Camera2 to implementation camera preview for devices with API > 20. I got the error on asus_t00j with android 5.0.0. I am not sure why it appear.
Fatal Exception: java.lang.NullPointerException: Attempt to write to field 'android.hardware.camera2.ICameraDeviceUser android.hardware.camera2.impl.CameraDeviceImpl.mRemoteDevice' on a null object reference
at android.hardware.camera2.impl.CameraDeviceImpl.close(CameraDeviceImpl.java:773)
at com.vyda.stream.screencast.CameraHelper.closeCamera(CameraHelper.java:410)
at com.vyda.stream.screencast.CameraHelper.stopCameraAndRemoveWindow(CameraHelper.java:150)
at com.vyda.stream.screencast.ScreencastService.endScreencasting(ScreencastService.java:307)
at com.vyda.stream.screencast.ScreencastService$8.onClick(ScreencastService.java:275)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19761)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5253)
at java.lang.reflect.Method.invoke(Method.java)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
CameraHelper::closeCamera()
private void closeCamera() {
try {
cameraOpenCloseLock.acquire();
if (null != captureSession) {
captureSession.close();
captureSession = null;
}
if (null != cameraDevice) {
cameraDevice.close();
cameraDevice = null;
}
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted while trying to lock camera closing.", e);
} finally {
cameraOpenCloseLock.release();
}
}
captureSession.close() is asynchronous, which means that when your code arrives to cameraDevice.close(), the underlying camera object (but not the cameraDevice member of you CameraHelper) is destroyed. With both captureSession.close() and cameraDevice.close() you are supposed to wait for the onClosed() callback.
But the description of cameraDevice.close() explains that the active session will be closed for you, so you don't need the captureSession.close() call in your scenario.
PS I believe that what you experience is a buggy implementation of Camera2 API on ASUS device.

Android mediaplayer using uri causes app crash

I have been working to resolve this problem for the last three days and cannot get it working. These are the steps of my app:
Open file browser to select and audio file
Audio file is selected
Selected audio file is added to the database
from a list of items the added file is selected
a play button is pressed to play the sound file
at this point the sound file will play, however if I close the app then open it again and attempt to play the same sound file the app crashes with the following error:
FATAL EXCEPTION: main
Process: com.arcitech.developer.ultimatesoundboard, PID: 22967
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.start()' on a null object reference
at com.arcitech.developer.ultimatesoundboard.activities.ItemDetailFragment$4.onClick(ItemDetailFragment.java:153)
at android.view.View.performClick(View.java:5156)
at android.view.View$PerformClick.run(View.java:20755)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5835)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
the filepath(uri) I am saving to the database is:
Content://com.android.providers.media.documents/document/audio%3A15790
and the code I am using is located in the onCreateView of a fragment:
MediaPlayer m;
playSound.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (m != null) {
m.stop();
m.release();
m = null;
}
m = MediaPlayer.create(getActivity(), Uri.parse(mItem.filePath));
m.start();
m.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer m) {
countdownTimerHandler.removeCallbacks(countdownTimerRunnable);
}
});
}
});
According to the doc for MediaPlayer.create(), it can return null if for some reason MediaPlayer cannot be created. You should be checking the return value for null every time unless you are very confident that it will not return null (but even then, do it for safety).
You may also want to check the log at the time create() is called to see if Android is leaving any clues as to why creation failed.

Categories

Resources