Explain how getVendorId is called Exception java.lang.NullPointerException: Attempt to…
- User
Explain how getVendorId is called
Exception java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.InputDevice.getVendorId()' on a null object reference at android.view.View.onResolvePointerIcon (View.java:29569) at android.view.ViewGroup.dispatchResolvePointerIcon (ViewGroup.java:2092) at android.view.ViewGroup.onResolvePointerIcon (ViewGroup.java:2067) at android.view.ViewGroup.dispatchResolvePointerIcon (ViewGroup.java:2092) at android.view.ViewGroup.onResolvePointerIcon (ViewGroup.java:2067) at android.view.ViewGroup.dispatchResolvePointerIcon (ViewGroup.java:2092) at android.view.ViewGroup.onResolvePointerIcon (ViewGroup.java:2067) at android.view.ViewGroup.dispatchResolvePointerIcon (ViewGroup.java:2092) at android.view.ViewGroup.onResolvePointerIcon (ViewGroup.java:2067) at android.view.ViewRootImpl.updatePointerIcon (ViewRootImpl.java:7116) at android.view.ViewRootImpl.resetPointerIcon (ViewRootImpl.java:7092) at android.view.ViewRootImpl.-$$NestViewRootHandler.handleMessageImpl (ViewRootImpl.java:6046) at android.view.ViewRootImpl$ViewRootHandler.handleMessage (ViewRootImpl.java:5862) at android.os.Handler.dispatchMessage (Handler.java:106)
Like where and roughly how it looks
- Assistant
Here’s roughly how the framework ends up calling
InputDevice.getVendorId()in the pointer-icon path. I’ll show you the high-level call chain + a simplified pseudo-implementation so you can see where the NPE can happen.
1) ViewRootImpl kicks it off
In
ViewRootImpl.updatePointerIcon(...)(called every time you move your pointer) it does something like:// ViewRootImpl.java private void updatePointerIcon(MotionEvent event) { // pick a pointer index (usually the one that just moved) int pointerIndex = event.findPointerIndex(mLastHandledHoverAction); if (pointerIndex < 0) return; // dispatch down into your view hierarchy PointerIcon icon = mView.dispatchResolvePointerIcon(event, pointerIndex); // tell the system to use that icon... mPointerIconController.setCurrentIcon(icon); }
2) ViewGroup dispatches / bubbles it
Each ViewGroup tries its children first, then if none of them handled it, falls back to its own
onResolvePointerIcon:// ViewGroup.java @Override public PointerIcon dispatchResolvePointerIcon(MotionEvent event, int pointerIndex) { // (1) ask children for (View child : mChildren) { PointerIcon childIcon = child.dispatchResolvePointerIcon(event, pointerIndex); if (childIcon != null) { return childIcon; } } // (2) fallback to self return onResolvePointerIcon(event, pointerIndex); }
3) View.onResolvePointerIcon calls getVendorId()
Finally, in
View.javayou get something like this (simplified):// View.java public PointerIcon onResolvePointerIcon(MotionEvent event, int pointerIndex) { // maybe you have a custom icon first… if (mPointerIcon != null) { return mPointerIcon; } // otherwise, pick a system icon based on the device that generated this event: // (a) grab the InputDevice object InputDevice device = event.getDevice(); // --- NPE happens here if device == null --- int vendorId = device.getVendorId(); // (b) pick a system icon (pseudo) return PointerIcon.getSystemIcon(getContext(), vendorId, /* other params */); }Why the NPE?
event.getDevice()(or its equivalentInputDevice.getDevice(event.getDeviceId())) can return null if Android’s internal device registry no longer knows about that device ID. As soon as you doInputDevice device = event.getDevice(); // ← null int vendor = device.getVendorId(); // ← NPE hereyou blow up.
How to protect yourself
Before calling
getVendorId(), always null-check:InputDevice device = event.getDevice(); if (device == null) { // fallback to safe default return PointerIcon.getSystemIcon(getContext(), PointerIcon.TYPE_DEFAULT); } int vendorId = device.getVendorId(); // …continueThat way you avoid the NullPointerException altogether.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.