Skip to content

Commit 9b5d125

Browse files
triniwizNathanWalker
authored andcommitted
fix(android): NullPointerException on navigation (#9669)
closes #8441
1 parent dbaf203 commit 9b5d125

File tree

4 files changed

+49
-8
lines changed

4 files changed

+49
-8
lines changed
-133 Bytes
Binary file not shown.

packages/core/ui/frame/index.android.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,12 +1060,11 @@ class FragmentCallbacksImplementation implements AndroidFragmentCallbacks {
10601060
// view.layout(0, 0, width, height);
10611061
// view.draw(canvas);
10621062

1063-
view.setDrawingCacheEnabled(true);
1064-
const drawCache = view.getDrawingCache();
1065-
const bitmap = android.graphics.Bitmap.createBitmap(drawCache);
1066-
view.setDrawingCacheEnabled(false);
1067-
1068-
return bitmap;
1063+
// view.setDrawingCacheEnabled(true);
1064+
// const drawCache = view.getDrawingCache();
1065+
// const bitmap = android.graphics.Bitmap.createBitmap(drawCache);
1066+
// view.setDrawingCacheEnabled(false);
1067+
return org.nativescript.widgets.Utils.getBitmapFromView(view);
10691068
}
10701069
}
10711070

packages/types-android/src/lib/android/org.nativescript.widgets.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,7 @@ declare module org {
633633
export module widgets {
634634
export class Utils {
635635
public static class: java.lang.Class<org.nativescript.widgets.Utils>;
636+
public static getBitmapFromView(param0: globalAndroid.view.View): globalAndroid.graphics.Bitmap;
636637
public static loadImageAsync(param0: globalAndroid.content.Context, param1: string, param2: string, param3: number, param4: number, param5: org.nativescript.widgets.Utils.AsyncImageCallback): void;
637638
public static drawBoxShadow(param0: globalAndroid.view.View, param1: string): void;
638639
public static saveToFileAsync(param0: globalAndroid.graphics.Bitmap, param1: string, param2: string, param3: number, param4: org.nativescript.widgets.Utils.AsyncImageCallback): void;

packages/ui-mobile-base/android/widgets/src/main/java/org/nativescript/widgets/Utils.java

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,65 @@
3333
import java.util.concurrent.Executor;
3434
import java.util.concurrent.Executors;
3535

36+
import android.graphics.Canvas;
37+
38+
import androidx.core.view.ViewCompat;
39+
40+
import android.os.Build;
3641

3742
public class Utils {
38-
public static Drawable getDrawable(String uri, Context context){
43+
public static Drawable getDrawable(String uri, Context context) {
3944
int resId = 0;
4045
int resPrefixLength = "res://".length();
4146

4247
if (uri.length() > resPrefixLength) {
4348
String resPath = uri.substring(resPrefixLength);
4449
resId = context.getResources().getIdentifier(resPath, "drawable", context.getPackageName());
4550
}
46-
51+
4752
if (resId > 0) {
4853
return AppCompatResources.getDrawable(context, resId);
4954
} else {
5055
Log.v("JS", "Missing Image with resourceID: " + uri);
5156
return null;
5257
}
5358
}
59+
60+
private static Bitmap drawBitmap(View view) {
61+
int width = view.getWidth();
62+
int height = view.getHeight();
63+
Bitmap bitmap;
64+
if (view.getAlpha() < 1F) {
65+
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
66+
} else {
67+
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
68+
}
69+
Canvas canvas = new Canvas(bitmap);
70+
if (!ViewCompat.isLaidOut(view)) {
71+
view.layout(0, 0, width, height);
72+
}
73+
view.draw(canvas);
74+
return bitmap;
75+
}
76+
77+
public static Bitmap getBitmapFromView(View view) {
78+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
79+
return drawBitmap(view);
80+
} else {
81+
82+
view.setDrawingCacheEnabled(true);
83+
Bitmap drawCache = view.getDrawingCache();
84+
Bitmap bitmap = Bitmap.createBitmap(drawCache);
85+
view.setDrawingCacheEnabled(false);
86+
87+
if (bitmap == null) {
88+
bitmap = drawBitmap(view);
89+
}
90+
91+
return bitmap;
92+
}
93+
}
94+
5495
public static void drawBoxShadow(View view, String value) {
5596
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.M) {
5697
return;

0 commit comments

Comments
 (0)