xgfe / react-native-datepicker

react native datePicker component for both Android and IOS, useing DatePikcerAndroid, TimePickerAndroid and DatePickerIOS
MIT License
2.12k stars 725 forks source link

Breaks on android #31

Open jnrepo opened 8 years ago

jnrepo commented 8 years ago

Running into this issue below:

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.SimpleMonthView.getMonthHeight()' on a null object reference
                                                                      at android.widget.DayPickerView.onLayout(DayPickerView.java:229)
                                                                      at android.view.View.layout(View.java:16630)
                                                                      at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                                                      at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                                                      at android.view.View.layout(View.java:16630)
                                                                      at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                      at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
                                                                      at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
                                                                      at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
                                                                      at android.view.View.layout(View.java:16630)
                                                                      at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                                                      at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                                                      at android.view.View.layout(View.java:16630)
                                                                      at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                                                      at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                                                      at android.view.View.layout(View.java:16630)
                                                                      at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                                                      at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                                                      at android.view.View.layout(View.java:16630)
                                                                      at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                      at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
                                                                      at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
                                                                      at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
                                                                      at android.view.View.layout(View.java:16630)
                                                                      at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                                                      at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                                                      at android.view.View.layout(View.java:16630)
                                                                      at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                                                      at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                                                      at android.view.View.layout(View.java:16630)
                                                                      at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                                                      at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                                                      at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2678)
                                                                      at android.view.View.layout(View.java:16630)
                                                                      at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                      at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2171)
                                                                      at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1931)
                                                                      at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
                                                                      at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
                                                                      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
                                                                      at android.view.Choreographer.doCallbacks(Choreographer.java:670)
                                                                      at android.view.Choreographer.doFrame(Choreographer.java:606)
                                                                      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
                                                                      at android.os.Handler.handleCallback(Handler.java:739)
                                                                      at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                      at android.os.Looper.loop(Looper.java:148)
                                                                      at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Only on Android, the module works well on iOS. The error seems to trigger after tapping the datepicker button. Thanks in advance!

specs: Android: M react: 15.2.1 react-native: 0.30.0

basequatro commented 8 years ago

Same here.. not working for android anymore.

feyy commented 8 years ago

I'm so sorry, but I can't replay your issue. Could you paste some code?

basequatro commented 8 years ago

@feyy I\ve got this when working in customStyles, seems to work until now, maybe it was some color on a view or something. Also you could add more class examples in readme, like placeholderText: and the other ones in the code.

jnrepo commented 8 years ago

@feyy sorry, didn't have much time to get on my project with the code. but this is what is giving me the error:

<DatePicker
          style={{
            width: deviceWidth * 0.84,
            left: 0.0781 * deviceWidth,
            top: 0.5231 * deviceHeight,
            position: 'absolute'
          }}
          date={this.state.birthday}
          minDate={moment('1920-01-01').format('YYYY-MM-DD')}
          maxDate={moment().format('YYYY-MM-DD')}
          mode="date"
          placeholder="Birthday"
          format={DATE_FORMAT}
          confirmBtnText="Confirm"
          cancelBtnText="Cancel"
          showIcon={false}
          customStyles={{
            dateText: {
              color: '#ffffff',
              textAlign: 'left',
              marginBottom: 3
            },
            dateTouchBody: {
              // color: '#ffffff',
              alignItems: 'flex-start',
              justifyContent: 'flex-end',
              flexDirection: 'row',
              borderColor: '#ffffff'
            },
            dateInput: {
              width: deviceWidth * 0.835,
              // color: '#ffffff',
              left: 0.01 * deviceWidth,
              alignItems: 'flex-start',
              justifyContent: 'flex-end',
              borderColor: '#ffffff',
              borderTopWidth: 0,
              borderRightWidth: 0,
              borderLeftWidth: 0
            },
            placeholderText: {
              color: '#ffffff',
              marginBottom: 3
            }
          }}
          onDateChange={(birthday) => {
            if(birthday === 'Invalid date') {
              birthday = moment().format(DATE_FORMAT)
            }
            this.setState({birthday})
          }}
        />
jnrepo commented 8 years ago

@feyy alright, I was able to narrow down the issue after playing with the code a little bit. It seems that for android when the format doesn't have dashes (ie. "MMM DD YYYY", "MMMM DD YYYY", "MMM Do YYYY") it doesn't work. However, when the format is set to "MMM-DD-YYYY" it will work...

Hope that helps

feyy commented 8 years ago

@jnrepo It work for me. Could show your code?

jnrepo commented 8 years ago
<DatePicker
          style={{
            width: deviceWidth * 0.84,
            left: 0.0781 * deviceWidth,
            top: 0.5231 * deviceHeight,
            position: 'absolute'
          }}
          date={this.state.birthday}
          mode="date"
          placeholder="Birthday"
          format="MMM DD, YYYY"
          confirmBtnText="Confirm"
          cancelBtnText="Cancel"
          minDate={moment('1920-01-01')}
          maxDate={moment().format('YYYY-MM-DD')}
          showIcon={false}
          customStyles={{
            dateText: {
              color: '#ffffff',
              textAlign: 'left',
              marginBottom: 3
            },
            dateTouchBody: {
              // color: '#ffffff',
              alignItems: 'flex-start',
              justifyContent: 'flex-end',
              flexDirection: 'row',
              borderColor: '#ffffff'
            },
            dateInput: {
              width: deviceWidth * 0.835,
              // color: '#ffffff',
              left: 0.01 * deviceWidth,
              alignItems: 'flex-start',
              justifyContent: 'flex-end',
              borderColor: '#ffffff',
              borderTopWidth: 0,
              borderRightWidth: 0,
              borderLeftWidth: 0
            },
            placeholderText: {
              color: '#ffffff',
              marginBottom: 3
            }
          }}
          onDateChange={(birthday) => {
            if(birthday === 'Invalid date') {
              birthday = moment().format("MMM-DD-YYYY")
            }
            this.setState({birthday})
          }}
        />

^ will only work on iOS and will return the error in the OP for Android.

Using...

Android M react 15.2.0 react native 32.0

pengan1987 commented 7 years ago

I got exact same error message in my Android app (a native Android app, not using ReactNative), this issue seems caused by the value of maxDate is smaller than the minDate, when the DatePickerDialog.show() calling, the app will raise this error and crashes.

Both minDate and maxDate in native Android API is long value contains the milliseconds level UNIX timestamp, print a log with these two values may help you find the reason of this issue.

kanaujiapushpa commented 7 years ago

You are right pengan , Also check the timestamp u are passing is in mili seconds or not ,I forgot to change it to mili seconds so it was giving me this problem.

jonajgs commented 6 years ago

I found the solution.

The date value format must be same from the maxDate format value and the format prop

// wrong date="07-01-2018" format="YYYY-MM-DD" maxDate="2018-07-01"

// good date={'2018-06-19'} format="YYYY-MM-DD" maxDate="2018-07-01"

I used moment and pass the format in props.

var today = new Date()

value={moment(props.value).format(props.format)}
format={props.format}
maxDate={moment(today).format(props.format)}
vishnuharidas commented 5 years ago

I got exact same error message in my Android app (a native Android app, not using ReactNative), this issue seems caused by the value of maxDate is smaller than the minDate, when the DatePickerDialog.show() calling, the app will raise this error and crashes.

Both minDate and maxDate in native Android API is long value contains the milliseconds level UNIX timestamp, print a log with these two values may help you find the reason of this issue.

Same here, pure native development. Saw the problem on a Nexus 7 tablet running 6.0.1.

Fix applied: In my case, I was setting the maxDate as Long.MAX_VALUE just for the sake of it. I just removed that line and it worked just fine.

KamranKhankhail commented 5 years ago

I suffered from same issue and removing maxDate fix the problem.

jeydi243 commented 5 years ago

Me it doesn't work. Please see my code: ` const ModalPicker = require("nativescript-modal-datetimepicker").ModalDatetimepicker; const picker = new ModalPicker(); const SQLite = require( "nativescript-sqlite" ); const kad = require('../js/myfonction'); const moment = require('moment'); var Toast = require("nativescript-toast"); var EventEmitter = require('events'); import { TNSFancyAlert, TNSFancyAlertButton } from "nativescript-fancyalert";

moment.locale('fr');

export default {
    name: 'HomePage',
    data(){
        return{
            madate: moment(new Date().toISOString()).format("LLLL"),
            mine: '45',
            my_db: null,
            nb:  ''
        }
    }, 
    mounted(){
        var clock = require('date-events')()
        this.my_db = kad ;
        clock.on('minute', function (min) {
            console.log('Minute ' + min + ' has arrived!')
        })
    },
    computed:{
        epa(){
            return this.mine;
        }
    },
    methods: {
        lancemoi() {  
            picker
                .pickDate({
                    title: "Choisis la date à inscrire!",
                    theme: "light",
                    maxDate: new Date(),
                    minDate: new Date(moment().subtract(30, 'days').calendar())
                })
                .then(result => {
                    var carma = new Date(result.year+"-"+(result.month)+"-"+ result.day);
                    this.madate = moment(carma.toISOString()).format("LLLL");       
                    //console.log("la date: "+ moment(carma.toISOString()).format("LLLL"));
                })
                .catch(error => {
                    console.log("Error: " + error);
                });
        },
        insertData(){
            if(this.madate != '' && this.nb != ''){
                if(kad.Insert(this.madate,this.nb) == 1){
                   TNSFancyAlert.showSuccess(
                       "Enregistrement",
                       "Le champ a bien été enregistrée",
                       "Okay",
                   ).then(()=>{
                       this.madate = '';
                       this.nb ='';
                   });

                }else{
                    TNSFancyAlert.showError("Error!", "Le champ n'a pu etre inseré! .", "Fermer");
                }
            }else{
                Toast.makeText("Veullez Choisir une date et un nombre").show();
            }
        },
        selectData(){
            kad.SelectAll();
        }
    }
};

`

ravindranpandu commented 5 years ago

Hey Guys, i had the same issue and was breaking my head for couple of days and finally fixed the issue, here is the fix for people who needs help in the android crash issue if your app crashes when you tap the datepicker.

The above two are the main reasons to make your app crash, apart from that you can use any format you want to display your date in the component using the format props, doesn't matter whether you have dash/hyphen.

Hope this will make someone day better, cheers 😃