OnePlus Nord devices with Android 10 are painful! 😔 - android

We have an enterprise application being developed, and since the project inception, we're facing a constant crash only in OnePlus Nord devices.
We tried to reproduce in our devices, and it's not getting reproduced, but Firebase Crashlytics always reports it in live application. Description as:
cpe.java line 11
com.oneplus.theme.cpe.onViewAttachedToWindow
Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.removeOnAttachStateChangeListener(android.view.View$OnAttachStateChangeListener)' on a null object reference
at com.oneplus.theme.cpe.onViewAttachedToWindow(cpe.java:11)
at android.view.View.dispatchAttachedToWindow(View.java:20272)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3455)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2273)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1960)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8228)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1239)
at android.view.Choreographer.doCallbacks(Choreographer.java:1041)
at android.view.Choreographer.doFrame(Choreographer.java:953)
at android.view.Choreographer$FrameHandler.handleMessage(Choreographer.java:1163)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7711)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
At this stage, as it is working fine on all other devices, we couldn't pinpoint any code which could be causing this issue.
Being reported with many OnePlus Nord users, this issue brings our crash free % down significantly! How to fix this?

It's some view related problem I think.
It's saying that you're using listener on null view object. So check if you are getting the view first & then applying listener.
You can use GlobalLayoutListener to check if the view has finished Drawing.
yourView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
yourView.getViewTreeObserver()
.removeOnGlobalLayoutListener(this);
//Your logic here (You can Put the OnAttachStateChangeListener here)
}
});

Related

Strange NPE deep inside MediaSessionCompat

I have seen similar questions posted about MediaSessionCompat but they are all pretty old, in fact most predate AndroidX and they point at it being fixed in newer versions.
This crash is happening with androidx.media:media:1.2.1 on various phones from Android 8 through 11. It is always MediaSessionImplApi21. Works fine for me on every phone I've tried.
This issue is new for me as of my latest release. The two big changes I made are that I added a progress bar to my MediaStyle notification and that I upgraded to 1.2.1 from 1.0.0.
This is the exception:
Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v4.media.session.MediaSessionCompat$Callback android.support.v4.media.session.MediaSessionCompat$MediaSessionImplApi21.getCallback()' on a null object reference
at android.support.v4.media.session.MediaSessionCompat$Callback$MediaSessionCallbackApi21.getSessionImplIfCallbackIsSet(MediaSessionCompat.java:1907)
at android.support.v4.media.session.MediaSessionCompat$Callback$MediaSessionCallbackApi21.onMediaButtonEvent(MediaSessionCompat.java:1597)
at android.media.session.MediaSession$CallbackMessageHandler.handleMessage(MediaSession.java:1471)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7094)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
If my Callback wasn't set them a lot of things wouldn't work.
Also my code looks like this:
MediaSessionCompat mediaSessionCompat = new MediaSessionCompat(context, TAG);
mediaSessionCompat.setCallback(mediaSessionCallback);
So there's really no room to not set the Callback.
Any idea what it could be?
From looking at the code and where it crashed:
private MediaSessionImplApi21 getSessionImplIfCallbackIsSet() {
MediaSessionImplApi21 sessionImpl;
synchronized (mLock) {
sessionImpl = (MediaSessionImplApi21) mSessionImpl.get();
}
return MediaSessionCompat.Callback.this == sessionImpl.getCallback()
? sessionImpl : null;
}
It is crashing on sessionImpl.getCallback(). Which probably means it either got recycled because mSessionImpl is a WeakReference or it was set to null to begin with.
Turns out it was a bug and has now been patched and will hopefully be out on 1.2.2 https://issuetracker.google.com/issues/178694750

onBackPressed sometimes or always called automaticaly when starting activity

i have problem with my app in some devices (HUAWEI P10 Lite, rockchip A95XR3). Maybe more in future.
According to users, when opening the Video Player Activity, the app immediately crashes.
I think, when they open the Video Player Activity, they quickly press the back button when Activity is not ready. So I tried to use my Oppo Realme 5Pro device and then tried to hit the back button with lightning speed, but it didn't crash (I tried several times). And I also tried running it on the emulator according to the Android version with problems 8.0.0 and 9.
Stack Trace:
Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'void c.d.b.a.t0.b()' on a null object reference
at com.**cencored**.C_Video_Player.ReleasePlayer(Act_Video_Player.java:163)
at com.**cencored**.C_Video_Player.onBackPressed(Act_Video_Player.java:405)
at android.app.Activity.onKeyUp(Activity.java:3161)
at android.view.KeyEvent.dispatch(KeyEvent.java:2779)
at androidx.core.view.KeyEventDispatcher.activitySuperDispatchKeyEventPre28(KeyEventDispatcher.java:137)
at androidx.core.view.KeyEventDispatcher.dispatchKeyEvent(KeyEventDispatcher.java:87)
at androidx.core.app.ComponentActivity.dispatchKeyEvent(ComponentActivity.java:133)
at androidx.appcompat.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:558)
at androidx.appcompat.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:59)
at androidx.appcompat.app.AppCompatDelegateImpl$AppCompatWindowCallback.dispatchKeyEvent(AppCompatDelegateImpl.java:2814)
at com.android.internal.policy.DecorView.dispatchKeyEvent(DecorView.java:415)
at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:5191)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5063)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4581)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4634)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4600)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4727)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4608)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4784)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4581)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4634)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4600)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4608)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4581)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4634)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4600)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4760)
at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:4921)
at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2599)
at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:2160)
at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:2151)
at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:2574)
at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
at android.os.MessageQueue.nativePollOnce(MessageQueue.java)
at android.os.MessageQueue.next(MessageQueue.java:379)
at android.os.Looper.loop(Looper.java:144)
at android.app.ActivityThread.main(ActivityThread.java:7523)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
If I see Stack Trace through Firebase Crashlytics, it looks like onBackPressed has been called, but according to the user they don't press the back button.
Below this is part of the code that is related to the problem.
private void ReleasePlayer() {
exoPlayer.release(); //Line 163
isVideoPrepared ="no";
TextError.setVisibility(GONE);
TextReconnecting.setVisibility(GONE);
TextTryCount.setVisibility(GONE);
}
#Override
public void onBackPressed() {
super.onBackPressed();
exoPlayerView.setVisibility(GONE);
forInfo.setVisibility(GONE);
forAds.setVisibility(GONE);
forControl.setVisibility(GONE);
ReleasePlayer(); //Line 405
finish();
}
On the stack trace above also not mentioned onCreate, so of course the exoplayer has not been initialized yet.
The real question is, what are the problems with the above devices? or maybe in my code?

Error after updating targetSDK version - ListView not working: interface method 'int java.util.List.size()' on a null object reference

I have a NameDay app in the App Store that was using an Android SDK <26. Since Google wants to make every app change its TargetSDK at least to 26 in order to continue being able to update, I wanted to change my app in order to satisfy Google's needs.
Changed the proper fields regarding the SDK, but now I get a crash when I try to run the app in the emulator.
App MainActivity:
The main activity of the app searches the database and gets all the names that celebrate today and shows them. Below that textview, i have a listview that compares the names that celebrate today to your contacts, and shows the contacts celebrating their name day in order to be able to call or send an sms.
The error I get is:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.duk3r.eortologio2, PID: 9577
java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at com.duk3r.eortologio2.LazyAdapter.getCount(LazyAdapter.java:32)
at android.widget.AdapterView$AdapterDataSetObserver.onChanged(AdapterView.java:836)
at android.widget.AbsListView$AdapterDataSetObserver.onChanged(AbsListView.java:6414)
at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37)
at android.widget.BaseAdapter.notifyDataSetChanged(BaseAdapter.java:50)
at com.duk3r.eortologio2.LazyAdapter.setItems(LazyAdapter.java:45)
at com.duk3r.eortologio2.MainActivity$LoadItemsTask.onPostExecute(MainActivity.java:498)
at com.duk3r.eortologio2.MainActivity$LoadItemsTask.onPostExecute(MainActivity.java:480)
at android.os.AsyncTask.finish(AsyncTask.java:695)
at android.os.AsyncTask.-wrap1(Unknown Source:0)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:712)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
The main files that appear in the error messages are:
MainActivity.java
and LazyAdapter.java
I read similar posts here in StackOverflow and they say that getCount() returns null and crashes the app. The thing is that I didn't change and code regarding the adapters or/and the main activity, and the app was working before. Did Google change something in the latest SDKs?
Thanks in advance.
Update: The app works fine with targetSDK 27 in an emulator with API 23, just tried it again. When I switch to an emulator with SDK >26, it crashes on startup when it tries to check the listview code.
It seems that the problem was that from API 23 and forth, the declared permissions in the Android Manifest don't really matter. You have to ask for the permissions on the fly, if you want it to work on api >23.
Made it work with this:
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
if (checkSelfPermission(Manifest.permission.READ_CONTACTS) ==
PackageManager.PERMISSION_GRANTED) {
System.out.println("Permission Granted");
} else {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
}
}
You have to ask for each permission you need separately, so that the app continues to work with no problem.

Exception in the Android's code while starting the app

In the "Crashes and ANRs" of the Google Play Developer console I've got such a report:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.AssetManager android.content.res.Resources.getAssets()' on a null object reference
at android.app.LoadedApk.getAssets(LoadedApk.java:590)
at android.app.LoadedApk.makeApplication(LoadedApk.java:646)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5088)
at android.app.ActivityThread.access$1600(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1509)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5944)
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:1389)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)
The device that has this problem is Galaxy S4 and runs Android 5.0
What it can be - there is not a single line from my code, why does it fail?
Thanks a lot!
I've got this in my console too.
It seems this occurs when users start the app when it's currently being updated or just after that.
A possible workaround would be to check if getResources returns null when the application start, and kill it if it does:
public class DevToolsApplication extends Application {
private static final String TAG = "DevToolsApplication";
#Override
public void onCreate() {
super.onCreate();
AppLogger.i(TAG, "app start...");
checkAppReplacingState();
}
private void checkAppReplacingState() {
if (getResources() == null) {
AppLogger.w(TAG, "app is replacing...kill");
Process.killProcess(Process.myPid());
}
}
}
See this for more information
https://issuetracker.google.com/issues/36972466
Make sure that anywhere you call getAssets(), you call it as:
getApplicationContext().getAssets()
It would appear as if you are calling getAssets() in a class that does not have the application context available, hence the fact that it is null.
I've got the same exception while added the below overlay in the AndroidManifest.xml file. Even after removing the Overlay code, landed into the above exception.
<overlay android:targetPackage="com.android.systemui"
android:priority="1"/>
Just close the Android Studio completely. Open the project again. Clean and Build the exception got cleared.

Android Wear Watchface and the WakeLock

I'm developing a watchface for Android Wear using the WatchFace API (extending CanvasWatchFaceService).
I've used the code from here to build a ticker that run code every second.
I'm experiencing the following problem. Every now and then the service crashes with this exception. I can't understand where it comes from, if you have any lead I'll post additional code.
01-06 11:22:00.247 12965-12965/com.my.package E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.my.package, PID: 12965
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.os.PowerManager$WakeLock.acquire()' on a null object reference
at android.support.wearable.watchface.WatchFaceService$Engine.onCommand(WatchFaceService.java:201)
at android.service.wallpaper.WallpaperService$Engine.doCommand(WallpaperService.java:977)
at android.service.wallpaper.WallpaperService$IWallpaperEngineWrapper.executeMessage(WallpaperService.java:1191)
at com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:37)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
After this crash the watchface stops working and to make it start again I have to choose another watchface, then reselect mine, so it's a total show stopper!
Props to David for the lead on super. calls - it turns out that was the problem. Specifically, the CanvasWatchFaceService.Engine.onCreate method needs to call through to its ancestor, as such:
private class Engine extends CanvasWatchFaceService.Engine {
#Override
public void onCreate(SurfaceHolder holder) {
super.onCreate(holder);
// your engine initialization code here
}
// other watch face engine code
}
Without the super.onCreate(holder); call, my watch face would crash within minutes; with it, it happily ran overnight.
As an aside, this is something missing from the Android developer documentation; specifically, the Training page for Building a Watch Face Service doesn't include this ancestor call in its code sample.
The code you linked lacks some important parts, that are dotted. I.e. have you defined the update rate?
private static final long INTERACTIVE_UPDATE_RATE_MS = TimeUnit.SECONDS.toMillis(1);
Fully implemented it works well. See AnalogWatchFaceService in code samples: Wearable/Watchface face.

Categories

Resources