react-native-navigation icon indicating copy to clipboard operation
react-native-navigation copied to clipboard

[V2] Installed app with bottom tabs crashes on startup

Open holparb opened this issue 7 years ago • 38 comments

Issue Description

The app runs without problems when run with the react-native cli using react-native run-android. However when starting the already installed app on the phone (not with the react-native cli) the app crashes with the following NullPointerException:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.drawable.Drawable android.graphics.drawable.Drawable.mutate()' on a null object reference
        at com.aurelhubert.ahbottomnavigation.AHHelper.getTintDrawable(AHHelper.java:36)
        at com.aurelhubert.ahbottomnavigation.AHBottomNavigation.createSmallItems(AHBottomNavigation.java:584)
        at com.aurelhubert.ahbottomnavigation.AHBottomNavigation.createItems(AHBottomNavigation.java:292)
        at com.reactnativenavigation.views.BottomTabs.superCreateItems(BottomTabs.java:48)
        at com.reactnativenavigation.views.BottomTabs.createItems(BottomTabs.java:36)
        at com.reactnativenavigation.views.BottomTabs.enableItemsCreation(BottomTabs.java:24)
        at com.reactnativenavigation.viewcontrollers.bottomtabs.BottomTabsController.applyOptions(BottomTabsController.java:91)
        at com.reactnativenavigation.viewcontrollers.ViewController.onViewAppeared(ViewController.java:204)
        at com.reactnativenavigation.viewcontrollers.ChildController.onViewAppeared(ChildController.java:34)
        at com.reactnativenavigation.viewcontrollers.ViewController.onGlobalLayout(ViewController.java:257)
        at android.view.ViewTreeObserver.dispatchOnGlobalLayout(ViewTreeObserver.java:945)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2273)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1421)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6842)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1026)
        at android.view.Choreographer.doCallbacks(Choreographer.java:838)
        at android.view.Choreographer.doFrame(Choreographer.java:769)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1012)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:171)
        at android.app.ActivityThread.main(ActivityThread.java:6699)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:246)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:783)

Steps to Reproduce / Code Snippets / Screenshots

The bottom tab navigation is set up this way:

root: {
        bottomTabs: {
            children: [{
                stack: {
                    children: [{
                        component: {
                            name: "Tab1",
                            passProps: {
                                text: "Tab1"
                            }
                        } 
                    }],
                    options: {
                        bottomTab: {
                            text: "Tab1",
                            icon: require("../../assets/icons/dummy_icon.png"),
                            testID: "TAB_1"
                        }
                    }
                }
            },
            {
                stack: {
                    children: [{
                        component: {
                            name: "Tab2",
                            passProps: {
                                text: "Tab2"
                            }
                        } 
                    }],
                    options: {
                        bottomTab: {
                            text: "Tab2",
                            icon: require("../../assets/icons/dummy_icon.png"),
                            testID: "TAB_2"
                        }
                    }
                }
            },
            {
                stack: {
                    children: [{
                        component: {
                            name: "Tab3",
                            passProps: {
                                text: "Tab3"
                            }
                        } 
                    }],
                    options: {
                        bottomTab: {
                            text: "Tab3",
                            icon: require("../../assets/icons/dummy_icon.png"),
                            testID: "TAB_3"
                        }
                    }
                }
            }]
        }
    }

I tried clearing the npm cache and also cleaning and rebuilding the project but the issue still persists.


Environment

  • React Native Navigation version: 2.11.0
  • React Native version: 0.57.8
  • Platform(s) (iOS, Android, or both?): Android
  • Device info (Simulator/Device? OS version? Debug/Release?): Device, Xiaomi Mi6 Android 8.0

holparb avatar Feb 12 '19 10:02 holparb

Hey @holparb, can you try reproducing this in the playground app? Does it reproduce on all devices are only that specific type?

guyca avatar Feb 12 '19 15:02 guyca

Hi @guyca , I'll try reproducing in the playground app. I only tested this on my own phone, haven't run it on any other Android devices yet.

Edit: Sorry for the noob question: how can I run the playground app on Android? I cloned the playground repo, did an npm install (got some errors though) and tried running the install-android script but it did not work:

git clone -b v2 [email protected]:wix/react-native-navigation.git
cd react-native-navigation
npm install
npm run install-android

holparb avatar Feb 12 '19 15:02 holparb

same here! It worked before but now crashes with rn 0.58

JohnnyDevX avatar Feb 12 '19 21:02 JohnnyDevX

I got the same issue, the bottom tabs are crashes with rn 0.58

jengener avatar Feb 13 '19 01:02 jengener

@holparb Don't clone v2 branch, use master: git clone [email protected]:wix/react-native-navigation.git

guyca avatar Feb 13 '19 17:02 guyca

imageLoader.loadIcon() is called with url to localhost:8081. But when you install apk on a device there is no Bundler running which produces crash

opiruyan avatar Feb 13 '19 18:02 opiruyan

@holparb Don't clone v2 branch, use master: git clone [email protected]:wix/react-native-navigation.git

@guyca I tried with master as well, install-android still fails:

cd playground/android && ./gradlew installDebug
'.' is not recognized as an internal or external command,
operable program or batch file.
child_process.js:640
    throw err;
    ^

Error: Command failed: cd playground/android && ./gradlew installDebug
    at checkExecSyncError (child_process.js:600:11)
    at Object.execSync (child_process.js:637:13)
    at Object.execSync (D:\Development\ReactNative\rnn-test\react-native-navigat
ion\node_modules\shell-utils\src\exec.js:8:6)
    at run (D:\Development\ReactNative\rnn-test\react-native-navigation\scripts\
install-android.js:9:8)
    at Object.<anonymous> (D:\Development\ReactNative\rnn-test\react-native-navi
gation\scripts\install-android.js:6:1)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)

Pretty sure it's me doing something wrong, should I be running a different script for Android perhaps...?

holparb avatar Feb 14 '19 08:02 holparb

Well, I would suggest opening the project (playground/android) with Android Studio.

cd playground/android
./graldew app:assembleDebug
install the apk

guyca avatar Feb 14 '19 08:02 guyca

Well, I would suggest opening the project (playground/android) with Android Studio.

cd playground/android
./graldew app:assembleDebug
install the apk

Thanks for the suggestion @guyca , I managed to install the Playground app.

I checked the "Tab based app", and it crashes with the same exception as in my project, it seems to be a general issue.

holparb avatar Feb 14 '19 11:02 holparb

@holparb could you check if you have preceding java.io.FileNotFoundException?

opiruyan avatar Feb 14 '19 11:02 opiruyan

@holparb could you check if you have preceding java.io.FileNotFoundException?

@opiruyan There's no FileNotFoundException prior to the NullPointerException, however I have this warning, maybe it's related?

com.reactnativenavigation.playground W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:244)
        at java.net.URL.openStream(URL.java:1059)
        at com.reactnativenavigation.utils.ImageLoader.remoteUrl(ImageLoader.java:124)
        at com.reactnativenavigation.utils.ImageLoader.openStream(ImageLoader.java:120)
        at com.reactnativenavigation.utils.ImageLoader.readJsDevImage(ImageLoader.java:85)
        at com.reactnativenavigation.utils.ImageLoader.getDrawable(ImageLoader.java:75)
        at com.reactnativenavigation.utils.ImageLoader.loadIcon(ImageLoader.java:38)
        at com.reactnativenavigation.viewcontrollers.bottomtabs.BottomTabsController.lambda$createTabs$3(BottomTabsController.java:155)
        at com.reactnativenavigation.viewcontrollers.bottomtabs.-$$Lambda$BottomTabsController$hVDDSxSvqNX-4DhQHls8ynB4x2w.map(Unknown Source:4)
        at com.reactnativenavigation.utils.CollectionUtils.map(CollectionUtils.java:42)
        at com.reactnativenavigation.viewcontrollers.bottomtabs.BottomTabsController.createTabs(BottomTabsController.java:151)
        at com.reactnativenavigation.viewcontrollers.bottomtabs.BottomTabsController.createView(BottomTabsController.java:75)
        at com.reactnativenavigation.viewcontrollers.ViewController.getView(ViewController.java:163)
        at com.reactnativenavigation.viewcontrollers.ParentController.getView(ParentController.java:60)
        at com.reactnativenavigation.viewcontrollers.navigator.RootPresenter.setRoot(RootPresenter.java:29)
        at com.reactnativenavigation.viewcontrollers.navigator.Navigator.setRoot(Navigator.java:133)
        at com.reactnativenavigation.react.NavigationModule.lambda$setRoot$1(NavigationModule.java:70)
        at com.reactnativenavigation.react.-$$Lambda$NavigationModule$IfiDeOnYfgJADKZUxIVN9Zforsw.run(Unknown Source:8)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:171)
        at android.app.ActivityThread.main(ActivityThread.java:6699)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:246)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:783)

holparb avatar Feb 14 '19 12:02 holparb

@holparb It is absolutely related. Cos its still ImageLoader.loadIcon() method which leads to the exception. Basically you are setting TintColor on null - the image which wasn't found.

opiruyan avatar Feb 14 '19 13:02 opiruyan

@opiruyan So practically the image needs to be loaded by Uri, this cannot be done due to some connection issue and it will be null?

By the way, here are the connection warnings from before:

com.reactnativenavigation.playground W/System.err:     at com.android.okhttp.okio.Okio$2.read(Okio.java:136)
        at com.android.okhttp.okio.AsyncTimeout$2.read(AsyncTimeout.java:211)
        at com.android.okhttp.okio.RealBufferedSource.indexOf(RealBufferedSource.java:306)
        at com.android.okhttp.okio.RealBufferedSource.indexOf(RealBufferedSource.java:300)
        at com.android.okhttp.okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:196)
com.reactnativenavigation.playground W/System.err:     at com.android.okhttp.internal.http.Http1xStream.readResponse(Http1xStream.java:186)
        at com.android.okhttp.internal.http.Http1xStream.readResponseHeaders(Http1xStream.java:127)
        at com.android.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:737)
        at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:609)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:471)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:407)

holparb avatar Feb 14 '19 14:02 holparb

@holparb Depends. When bundler is running RNN tries to fetch it from there. Otherwise its looking for static image. The issue is how RNN resolves the way of looking for the image.

opiruyan avatar Feb 14 '19 15:02 opiruyan

+1

WASHSWATDAVID avatar Feb 18 '19 13:02 WASHSWATDAVID

@guyca maybe you have a good overview of the image resolving mechanism and how to fix this? Although I'm not an expert but maybe if we could locate where this issue is in the RNN logic I could also try fixing it.

holparb avatar Feb 19 '19 06:02 holparb

@holparb I am using the bottom tabs in my Android app. I was looking at the root configuration and I think the options are not defined at the right place for each bottom tab. The options should be a part of the component configuration. For e.g., the first children should look like

                  stack: {
                    children: [{
                        component: {
                            name: "Tab1",
                            passProps: {
                                text: "Tab1"
                            },
                            options: {
                              bottomTab: {
                                  text: "Tab1",
                                  icon: require("../../assets/icons/dummy_icon.png"),
                                  testID: "TAB_1"
                              }
                          }
                        } 
                    }]
                  }

I am not sure if options could be defined in multiple ways but that is how I have it. You will need to update all the tabs configuration similarly.

varungupta85 avatar Feb 19 '19 07:02 varungupta85

@varungupta85 But does this order fix the issue? Im afraid it doesn't. Cos I have a kind of configuration you present. And I guess original playground app by wix has right configuration as well but it still crashes.

opiruyan avatar Feb 19 '19 09:02 opiruyan

@varungupta85 I agree with @opiruyan , based on the error logs the configuration order is not the cause of the issue. If you check the BottomTabs documentation (Guide -> Layouts -> bottomTabs), the example there also specifies the options in the same way as my example in the issue description.

holparb avatar Feb 19 '19 09:02 holparb

@holparb I did some. All happens in this method

private Drawable getDrawable(Context context, String source) throws IOException {
        if (isLocalFile(...)) {
            drawable = loadFile(source);
        } else {
        <...>
                drawable = readJsDevImage(context, source);
            }
        <...>
    }

isLocalFile() takes uri of image

private boolean isLocalFile(Uri uri) {
        return FILE_SCHEME.equals(uri.getScheme());
    }

and compares it to FILE_SCHEME, which hardcoded to be

private static final String FILE_SCHEME = "file";

readJsDevImage() in its turn calls openStream() method which also compares to hardcoded string:

private static InputStream openStream(Context context, String uri) throws IOException {
        return uri.contains("http") ? remoteUrl(uri) : localFile(context, uri);
    }

SO, the question is uri we get which is String source in getDrawable:

private Drawable getDrawable(Context context, String source)

There are scenarios when it resolved to be http://smth but its not.

UPDATE this source string with image uri we get from calling icon.get which is a method of wrapper class

public abstract class Param<T> 

which can be found in src/main/java/com.reactnavigation/parse/params. But still, why would this value be such and not another?

opiruyan avatar Feb 19 '19 09:02 opiruyan