Skip to content

JS: TypeError: Cannot read property 'startActivityForResult' of undefined #334

Open
@aubrey-fowler

Description

@aubrey-fowler

I'm working on an app with Angular 8.2.0 and NS 6.8.0.

tns info

✔ Getting NativeScript components versions information...
✔ Component nativescript has 7.0.11 version and is up to date.
✔ Component tns-core-modules has 6.5.24 version and is up to date.
⚠ Update available for component @nativescript/core. Your current version is 6.5.24 and the latest available version is 7.0.13.
✔ Component tns-ios has 6.5.3 version and is up to date.
✔ Component tns-android has 6.5.3 version and is up to date.

Here is my package.json file:

{
    "nativescript": {
        "id": "org.nativescript.MyApp",
        "tns-android": {
            "version": "6.5.3"
        },
        "tns-ios": {
            "version": "6.5.3"
        }
    },
    "description": "NativeScript Application",
    "license": "SEE LICENSE IN <your-license-filename>",
    "repository": "<fill-your-repository-here>",
    "dependencies": {
        "@angular/animations": "~8.2.0",
        "@angular/common": "~8.2.0",
        "@angular/compiler": "~8.2.0",
        "@angular/core": "~8.2.0",
        "@angular/forms": "~8.2.0",
        "@angular/platform-browser": "~8.2.0",
        "@angular/platform-browser-dynamic": "~8.2.0",
        "@angular/router": "~8.2.0",
        "@danvick/ngx-translate-nativescript-loader": "^2.0.0",
        "@nativescript/core": "^6.4.1",
        "@nativescript/theme": "^2.5.0",
        "@ngx-translate/core": "^12.1.2",
        "@ngx-translate/http-loader": "^4.0.0",
        "@nstudio/nativescript-loading-indicator": "^3.0.3",
        "@schematics/angular": "^8.3.18",
        "autobind-decorator": "^2.4.0",
        "moment": "^2.24.0",
        "moment-timezone": "^0.5.32",
        "nativescript-angular": "~8.2.0",
        "nativescript-appversion": "^1.4.4",
        "nativescript-camera": "^4.5.0",
        "nativescript-feedback": "^1.3.12",
        "nativescript-geolocation": "^5.1.0",
        "nativescript-google-maps-sdk": "^2.9.1",
        "nativescript-imagepicker": "^7.1.0",
        "nativescript-inappbrowser": "^2.2.0",
        "nativescript-iqkeyboardmanager": "^1.5.1",
        "nativescript-store-update": "^1.0.2",
        "nativescript-ui-chart": "^7.1.1",
        "nativescript-ui-dataform": "^6.0.0",
        "nativescript-ui-sidedrawer": "^8.0.1",
        "nativescript-unit-test-runner": "^0.7.0",
        "nativescript-urlhandler": "^1.3.0",
        "reflect-metadata": "~0.1.12",
        "rxjs": "^6.4.0",
        "tns-android": "^6.5.3",
        "tns-core-modules": "^6.5.24",
        "tns-ios": "^6.5.3",
        "zone.js": "~0.9.1"
    },
    "devDependencies": {
        "@angular/cli": "^9.0.5",
        "@angular/compiler-cli": "~8.2.0",
        "@nativescript/schematics": "^1.0.0",
        "@ngtools/webpack": "~8.2.0",
        "@types/jasmine": "3.5.6",
        "@types/jest": "^24.0.20",
        "@types/mocha": "^5.2.7",
        "jasmine-marbles": "^0.6.0",
        "karma": "4.4.1",
        "karma-jasmine": "3.1.1",
        "karma-nativescript-launcher": "0.4.0",
        "karma-typescript": "^4.1.1",
        "karma-typescript-angular2-transform": "^4.1.1",
        "karma-webpack": "3.0.5",
        "nativescript-dev-webpack": "^1.5.1",
        "node-sass": "^4.12.0",
        "sass": "^1.23.7",
        "sass-loader": "^8.0.0",
        "tns-platform-declarations": "^6.4.1",
        "typescript": "~3.5.3",
        "webpack": "^4.44.1"
    },
    "gitHead": "1964ea9e8ca1a0518e0d6370781eedcf6c23db42",
    "readme": "NativeScript Application"
}

I am using this plugin to select photos from the gallery.
When my user's session token expires and they re-login and go back to the photos page, I am getting an error:

Tap on select

JS:   "type": "error",
JS: TypeError: Cannot read property 'startActivityForResult' of undefined
JS:   "error": "Error: Uncaught (in promise): Error: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources$Theme android.content.Context.getTheme()' on a null object reference\ncreateAlertDialog(file:///data/data/org.nativescript.SelfServiceApp/files/app/vendor.js:105515:17)\n\tat (file:///data/data/org.nativescript.SelfServiceApp/files/app/vendor.js:105599:27)\n\tat ZoneAwarePromise(file:///data/data/org.nativescript.SelfServiceApp/files/app/vendor.js:165937:29)\n\tat alert(file:///data/data/org.nativescript.SelfServiceApp/files/app/vendor.js:105596:12)\n\tat module.exports.push../app/authenticated/home-page/components/photo-clocking-button/modals/photo-clocking-modal/photo-clocking-modal.component.ts.PhotoClockingModalComponent.handleError(file:///data/data/org.nativescript.SelfServiceApp/files/app/bundle.js:3424:9)\n\tat push.../node_modules/nativescript-angular/zone-js/dist/zone-nativescript.js.ZoneDelegate.invoke(file:///data/data/org.nativescrip...
JS: Unhandled Promise rejection: java.lang.IllegalArgumentException: Activity cannot be null! ; Zone: <root> ; Task: null ; Value: Error: java.lang.IllegalArgumentException: Activity cannot be null! (file: node_modules/nativescript-feedback/feedback.android.js:17:0)
JS:     at ZoneAwarePromise(file: node_modules/nativescript-angular/zone-js/dist/zone-nativescript.js:902:0)
JS:     at ZoneAwarePromise(file: node_modules/nativescript-angular/zone-js/dist/zone-nativescript.js:902:0)

Picture component

import { Component, OnInit } from '@angular/core';
import autobind from 'autobind-decorator';
import { ModalDialogParams } from 'nativescript-angular/modal-dialog';
import * as camera from 'nativescript-camera';
import * as imagepicker from 'nativescript-imagepicker';
import { ImageAsset } from 'tns-core-modules/image-asset';
import { ImageSource } from 'tns-core-modules/image-source/';
import { Constants } from '~/app/core/constants/constants.service';
import { IPhotoData } from '../../models/iphoto-data';

@Component({
    selector: 'ns-photo-clocking-modal',
    templateUrl: './photo-clocking-modal.component.html'
})
export class PhotoClockingModalComponent implements OnInit {
    private options: camera.CameraOptions = {
        width: 300,
        height: 300,
        keepAspectRatio: true,
        saveToGallery: true
    };

    public image_data: IPhotoData | string = 'res://avatar';

    constructor(private params: ModalDialogParams) {}

    ngOnInit() {}

    public take(): void {
        const isAvailable: boolean = camera.isAvailable() as boolean;

        if (!isAvailable) {
            this.handleError(Constants.DEFAULT_ERRORS.CAMERA_NOT_AVAILABLE);

            return;
        }

        camera
            .requestPermissions()
            .then(
                () => {
                    camera
                        .takePicture(this.options)
                        .then(this.convertPhotoToBase64)
                        .catch((error) => console.log(error));
                },
                (error) => {
                    // tslint:disable-next-line: no-console
                    console.log(error);
                }
            )
            .catch((error) => console.log(error));
    }

    @autobind
    private convertPhotoToBase64(imageAsset: ImageAsset): Promise<string> {
        return ImageSource.fromAsset(imageAsset).then((image: ImageSource) => {
            const base64: string = image.toBase64String('png');
            this.image_data = { mediaTypeField: 'image/png', imageDataField: base64 };

            return base64;
        });
    }

    @autobind
    private handleError(error): void {
        const options = {
            title: 'Error',
            message: error,
            okButtonText: 'OK'
        };

        //alert(options);
    }

    public select(): void {
        const context = imagepicker.create({
            mode: 'single'
        });

        context
            .authorize()
            .then(() => context.present())
            .then(this.handlePhotoSelectionSuccess)
            .catch(this.handleError);
    }

    @autobind
    private handlePhotoSelectionSuccess(selectedPhotos: ImageAsset[]): Promise<string> {
        if (selectedPhotos == null || selectedPhotos.length === 0) {
            return Promise.reject(Constants.DEFAULT_ERRORS.CAMERA_NOT_AVAILABLE);
        }

        const selectedPhoto: ImageAsset = selectedPhotos[0];
        selectedPhoto.options = { width: this.options.width, height: this.options.height };

        return this.convertPhotoToBase64(selectedPhoto);
    }

    public close(): void {
        this.params.closeCallback(false);
    }

    public clock(): void {
        this.params.closeCallback(this.image_data);
    }
}

What could be wrong?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions