X Tutup
The Wayback Machine - https://web.archive.org/web/20220709083205/https://github.com/NativeScript/NativeScript/issues/8441
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android: NullPointerException on navigation (Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()') #8441

Closed
cghislai opened this issue Mar 16, 2020 · 7 comments · Fixed by #9669

Comments

@cghislai
Copy link

@cghislai cghislai commented Mar 16, 2020

Environment

  • tns info:
    Getting NativeScript components versions information...
    Update available for component nativescript. Your current version is 6.4.0 and the latest available version is 6.4.1.
    Update available for component tns-core-modules. Your current version is 6.4.1 and the latest available version is 6.4.2.
    Component tns-android has 6.4.1 version and is up to date.
    Update available for component tns-ios. Your current version is 6.4.0 and the latest available version is 6.4.2.

  • package.json dependencies:
    "@nstudio/nativescript-cardview": "^1.0.0",
    "nativescript-angular": "^8.21.0",
    "nativescript-background-http": "^4.2.1",
    "nativescript-handle-file-cghislai": "^6.4.0",
    "nativescript-imagepicker": "^7.1.0",
    "nativescript-mediafilepicker": "^3.0.0",
    "nativescript-perms": "^2.0.3",
    "nativescript-share-file": "file:nativescript-share-file-1.0.6.tgz",
    "nativescript-theme-core": "~1.0.6",
    "nativescript-toast": "^2.0.0",
    "nativescript-ui-chart": "^7.1.1",
    "nativescript-ui-sidedrawer": "^8.0.0",
    "@nativescript/schematics": "~0.7.3",
    "@nativescript/tslint-rules": "0.0.5",
    "nativescript-dev-webpack": "^1.5.0",

  • android version tested: 6.0

Describe the bug
When triggering a navigation, my app crashes with the following exception:

JS: route: Route(url:'login', path:'login')
System.err: An uncaught Exception occurred on "main" thread.
System.err: Calling js method onPause failed
System.err: Error: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
System.err: StackTrace:
System.err: push.../node_modules/@nativescript/core/ui/frame/frame.js.FragmentCallbacksImplementation.loadBitmapFromView(file:///node_modules/@nativescript/core/ui/frame/frame.js:791:0)
System.err:     at push.../node_modules/@nativescript/core/ui/frame/frame.js.FragmentCallbacksImplementation.onPause(file:///node_modules/@nativescript/core/ui/frame/frame.js:766:0)
System.err:     at push.../node_modules/@nativescript/core/ui/frame/fragment.js.FragmentClass.onPause(file:///node_modules/@nativescript/core/ui/frame/fragment.js:19:0)
System.err:     at com.tns.Runtime.callJSMethodNative(Native Method)
System.err:     at com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1286)
System.err:     at com.tns.Runtime.callJSMethodImpl(Runtime.java:1173)
System.err:     at com.tns.Runtime.callJSMethod(Runtime.java:1160)
System.err:     at com.tns.Runtime.callJSMethod(Runtime.java:1138)
System.err:     at com.tns.Runtime.callJSMethod(Runtime.java:1134)
System.err:     at com.tns.FragmentClass.onPause(FragmentClass.java:64)
System.err:     at androidx.fragment.app.Fragment.performPause(Fragment.java:2645)
System.err:     at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1512)
System.err:     at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
System.err:     at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
System.err:     at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3269)
System.err:     at androidx.fragment.app.FragmentManagerImpl.dispatchPause(FragmentManager.java:3245)
System.err:     at androidx.fragment.app.Fragment.performPause(Fragment.java:2641)
System.err:     at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1512)
System.err:     at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
System.err:     at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:797)
System.err:     at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
System.err:     at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
System.err:     at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
System.err:     at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
System.err:     at androidx.fragment.app.FragmentManagerImpl$1.run(FragmentManager.java:733)
System.err:     at android.os.Handler.handleCallback(Handler.java:743)
System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
System.err:     at android.os.Looper.loop(Looper.java:150)
System.err:     at android.app.ActivityThread.main(ActivityThread.java:5621)
System.err:     at java.lang.reflect.Method.invoke(Native Method)
System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)
System.err: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
System.err:     at android.graphics.Bitmap.createBitmap(Bitmap.java:645)
System.err:     ... 29 more

I couldn't reproduce the issue in a poc on nativescript-playground. I tried plyaing around with the route hierarchy and the <page-router-outlet> uses, but I didn't manage to reproduce there unfortunately.

The application was a web-only angular app imported into a nativescript project, so there are some constraint on the application modules and components layout.

Debugging nativescript doc, and consulting android documentation, i think the issue is that nativescript expect View.getDrawingCache() to always return a bitmap, whereas documentation seems to imply null must be checked.

In nativescript frame.js, there is those lines:

view.setDrawingCacheEnabled(true);
const drawCache = view.getDrawingCache();
const bitmap = android.graphics.Bitmap.createBitmap(drawCache);
view.setDrawingCacheEnabled(false);

With the android documentation stating (for setDrawingCacheEnabled):

Calling draw(android.graphics.Canvas) will not draw from the cache when the cache is enabled. To benefit from the cache, you must request the drawing cache by calling getDrawingCache() and draw it on screen if the returned bitmap is not null.

@cghislai
Copy link
Author

@cghislai cghislai commented Mar 16, 2020

note that other navigation work. This is caused by a specific navigation (back to login route), and I couldn't figure out what is so different about it.

@NickIliev
Copy link
Member

@NickIliev NickIliev commented Mar 17, 2020

@cghislai is there a 3rd-party plugin used in the pages that are involved in the navigation? Try to isolate the issue by removing parts of your code and re-testing the case.

Also, what is the version of @nativescript/core (tns-core-modules) used in your application? Make sure you are using the latest version.

@cghislai
Copy link
Author

@cghislai cghislai commented Mar 17, 2020

@NickIliev indeed this seem related to some plugins. removing registerElement('CardView', () => CardView); prevents the crash. Triggering the navigation from a view without any CardView works as well.

My version of tns-core-modules is "^6.4.1".
That plugin does not seem to be maintained since 8 months, so I guess I should avoid using it.
Still I think nativescript could better handle bad/not up to date plugins, and avoid this crash?

EDIT: I can reproduce without cardview, it does not seems completely deterministic, im trying to narrow down.

@cghislai
Copy link
Author

@cghislai cghislai commented Mar 17, 2020

So I have this in the view Im navigating from:

 <StackLayout>
    <StackLayout class="Card__header">
      <Label text="Résumé"></Label>
    </StackLayout>

    <StackLayout>
      <Label text="test"></Label>
    </StackLayout>

    <GridLayout rows="auto" columns="auto, auto, *, auto, auto" class="Card__footer">
      <Label class="incomes-legend " text="⬤" row="0" col="0"></Label>
      <Label class="legend-label Card__footer" text="Revenus" row="0" col="1"></Label>
      <Label class="outcomes-legend Card__footer" text="⬤" row="0" col="3"></Label>
      <Label class="legend-label Card__footer" text="Dépenses" row="0" col="4"></Label>
    </GridLayout>
  </StackLayout>

with the corresponding css:

.Card__header, .Card__footer {
  background-color: var(--grey-200);
  font-weight: var(--font-weight-semi-bold);
  > * {
    padding: var(--margin-medium);
    vertical-align: center;
  }
}

and that triggers the crash.

To avoid the crash, I can either:

  • remove the padding css rule
  • remove the central stack layout with the test label

Enabling the CardView plugin triggers the crash in all cases.

@himanshukk81
Copy link

@himanshukk81 himanshukk81 commented Sep 10, 2020

No above solution work.

@nicolaric
Copy link

@nicolaric nicolaric commented Oct 4, 2021

any updates on this? I currently experience the same issue.

@mobifly-dev
Copy link

@mobifly-dev mobifly-dev commented Nov 15, 2021

Nothing happen with NS. Issue still occurs, peoples have to do own patches for framework... (note - same problem on latest NS 8)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants
X Tutup