I have encountered a problem in which when ever I use
final MediaPlayer tick = MediaPlayer.create(getApplicationContext(), R.raw.tick);
tick.start();// tick is a short .mp3 file
in my app, no matter where, I get:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.start()' on a null object reference
at com.a.aar.MainActivity.onClicks(MainActivity.java:123)
at com.a.aar.MainActivity$2.onClick(MainActivity.java:97)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21153)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
But, whenever I use a slightly shorter sound effect, the app runs great.(I need a short sound because I want a lot of sounds coming fast and so the sounds will not stop)
What is the reason to this error and how can I solve it?
Related
I am not able to figure out what is causing this error while the app is running. It causes the app to stop. Is it a wrong practice to sleep the main thread to finish execution of the asynchronous thread. If so why and what is a good practice?
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.view.View$DeclaredOnClickListener.onClick(View.java:4707)
at android.view.View.performClick(View.java:5619)
at android.view.View$PerformClick.run(View.java:22295)
at android.os.Handler.handleCallback(Handler.java:754)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:163)
at android.app.ActivityThread.main(ActivityThread.java:6342)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:880)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4702)
at android.view.View.performClick(View.java:5619)
at android.view.View$PerformClick.run(View.java:22295)
at android.os.Handler.handleCallback(Handler.java:754)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:163)
at android.app.ActivityThread.main(ActivityThread.java:6342)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:880)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at com.example.sairahul5223.e_bot.Main4Activity.onButtonClickName43(Main4Activity.java:330)
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4702)
at android.view.View.performClick(View.java:5619)
at android.view.View$PerformClick.run(View.java:22295)
at android.os.Handler.handleCallback(Handler.java:754)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:163)
at android.app.ActivityThread.main(ActivityThread.java:6342)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:880)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)
First thing: we don't make the main thread sleep because this will cause an Application Not Responding (ANR) error.
To get the result of an asynchronous task, we normally use a callback to determine the task's status. Take a look at this answer to give you idea: How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class?
I've been working on a single activity application that receives UDP packets, parses their message, then changes an ImageView, a TextView, and plays an alert song depending on what the message contains. I have three classes: MainActivity, ListeningService, and AndroidUDP. MainActivity contains all of the GUI implentation, AndroidUDP contains the enumeration that contains the 20 possible alert types dependent on what the UDP message contains as well as sets the TextView and ImageViews, and ListeningService contains the AsyncTask private class.
I didn't write this, an electrical engineer did and I was tasked with making it actually work. When I attempt to call the ListeningService via the AndroidUDP.startIt() method I get the error
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.p3_group.v2vapp/com.p3_group.v2vapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.p3_group.v2vapp.ListeningService.continueListening()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.p3_group.v2vapp.ListeningService.continueListening()' on a null object reference
at com.p3_group.v2vapp.MainActivity.onCreate(MainActivity.java:61)
I'm sure that I'm doing something dumb like trying to call the background update from the wrong thread. I've never done this kind of thing before so any pointers would be great!
and yes, i know this code is pretty bad. I only need it functional for what we're doing, not perfect
Hi you didn't start the service at all and you are trying to bind the service in "public void doStuff(MainActivity r, IntentFilter filter)" but it will not bind, so ListeningService listeningService is null in AndroidUDP, but you are trying to call below method, but listeningService is null so it's giving null pointer exception.
public void startIt()
{
listeningService.continueListening();
}
I'm developing some android application .In application i want to disable home button .
So i tried to overide onAttachedToWindow method, but the problem is i getting a error when start application.
Code :
#Override
public void onAttachedToWindow(){
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
super.onAttachedToWindow();
}
Error :
java.lang.IllegalArgumentException: Window type can not be changed after the window is added.
at android.os.Parcel.readException(Parcel.java:1603)
at android.os.Parcel.readException(Parcel.java:1552)
at android.view.IWindowSession$Stub$Proxy.relayout(IWindowSession.java:912)
at android.view.ViewRootImpl.relayoutWindow(ViewRootImpl.java:5415)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1581)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
any one have idea , what is the error of this code ?
I found the answer for my question . in android Lolipop have startLockTask() funciton.this will pin your task. thak you all .
As I mentioned in other question, you are not able to disable or override, or even detect home button.
Set type of window you can only before setContentView().
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
i'm implementing google maps in an android project . But as soon as i moved my project into another PC machine i'm facing app crashs and this error message in my logcat console :
FATAL EXCEPTION: main
Process: com.egsdigital.dolan, PID: 24887
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.egsdigital.dolan/com.egsdigital.dolan.Activities.MainPage_Pasenger}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.GoogleMap.setOnCameraChangeListener(com.google.android.gms.maps.GoogleMap$OnCameraChangeListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.GoogleMap.setOnCameraChangeListener(com.google.android.gms.maps.GoogleMap$OnCameraChangeListener)' on a null object reference
at com.egsdigital.dolan.Activities.MainPage_Pasenger.onCreate(MainPage_Pasenger.java:178)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
and here where the error points on:
map.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
#Override
public void onCameraChange(CameraPosition position) {
LatLngBounds bounds = map.getProjection().getVisibleRegion().latLngBounds;
mAdapter.setBounds(bounds);
}
});
thanks so much.
you must add new SHA1 into google console.
See this link for fetch SHA1 from your OS : How to get the SHA1 fingerprint certificate in android studio for debug mode
After fetching your SHA1 code, insert into console.developers and running Application .
Hope help you
Ok, first of all, I know what is NullPointer but wanted to ask what could cause such a behaviour.
I received crash on Crittercism and stacktrace is pointing to List.add:
java.lang.NullPointerException: Attempt to invoke interface method 'boolean java.util.List.add(java.lang.Object)' on a null object reference
at com.xxx.android.activities.SuperActivity.onCreate(SourceFile:59)
at com.xxx.android.activities.XXXActivity.onCreate(SourceFile:30)
But the fact is that there is no list in onCreate or even the whole SuperActivity.
What could cause such a stacktrace in Android? How can I find this NullPointer or similar source?
EDIT
As per request SuperActivity code around line 59:
super.onCreate(savedInstanceState);
if (activitiesCreated == 0) {
isFirstStart = true;
}
activitiesCreated++;
EDIT 2
I know how to follow stacktrace. What I want to know is what could be the problem and what can I do in cases like this?
EDIT 3
Stacktrace for the crashing thread:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxx.android/com.xxx.android.activities.XXXActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'boolean java.util.List.add(java.lang.Object)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'boolean java.util.List.add(java.lang.Object)' on a null object reference
at com.xxx.android.activities.SuperActivity.onCreate(SourceFile:59)
at com.xxx.android.activities.XXXActivity.onCreate(SourceFile:30)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
... 9 more
java.lang.NullPointerException: Attempt to invoke interface method 'boolean java.util.List.add(java.lang.Object)' on a null object reference
at com.xxx.android.activities.SuperActivity.onCreate(SourceFile:59)
at com.xxx.android.activities.XXXActivity.onCreate(SourceFile:30)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)