Search code examples
androidandroid-timepicker

Android Time Picker In Fragment Crashing


I have an app that has three fragments. On the second fragment I have a time picker and I want to feed the value selected on this time picker to the main activity.

I believe there is an error in this section of code as it builds and deploys to my device, but crashes on opening.

@Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

            // Set up the ViewPager with the sections adapter.
            mViewPager = (ViewPager) findViewById(R.id.container);
            mViewPager.setAdapter(mSectionsPagerAdapter);


//In debugging here is the section of code that causes the app to no longer run

            timePicker1 = (TimePicker) findViewById(R.id.alarmTimePickerStart);
            time = (TextView) findViewById(R.id.textView3);
            calendar = Calendar.getInstance();

            int hour = calendar.get(Calendar.HOUR_OF_DAY);
            int min = calendar.get(Calendar.MINUTE);
            showTime(hour, min);
        }

        public void setTime(View view) {
            int hour = timePicker1.getCurrentHour();
            int min = timePicker1.getCurrentMinute();
            showTime(hour, min);
        }

        public void showTime(int hour, int min) {
            if (hour == 0) {
                hour += 12;
                format = "AM";
            } else if (hour == 12) {
                format = "PM";
            } else if (hour > 12) {
                hour -= 12;
                format = "PM";
            } else {
                format = "AM";
            }

            time.setText(new StringBuilder().append(hour).append(" : ").append(min)
                    .append(" ").append(format));


        }

Am I putting this code in the wrong java file? I figured it would be a main activity command as the value of time picked needs to be global to the application and must be saved during termination of the app.

Here is the error log:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: io.mindfulmotivation.mindfulmotivation, PID: 31814
    java.lang.RuntimeException: Unable to start activity ComponentInfo{io.mindfulmotivation.mindfulmotivation/io.mindfulmotivation.mindfulmotivation.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2679)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2740)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1487)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6164)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
        at io.mindfulmotivation.mindfulmotivation.MainActivity.showTime(MainActivity.java:84)
        at io.mindfulmotivation.mindfulmotivation.MainActivity.onCreate(MainActivity.java:62)
        at android.app.Activity.performCreate(Activity.java:6720)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2632)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2740) 
        at android.app.ActivityThread.-wrap12(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1487) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:154) 
        at android.app.ActivityThread.main(ActivityThread.java:6164) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)

Solution

  • This is happening because you are trying to set text in a TextView which is not in the MainActivity.

    time = (TextView) findViewById(R.id.textView3);
    

    And

    time.setText(new StringBuilder().append(hour).append(" : ").append(min)
                        .append(" ").append(format));
    

    Make sure R.id.textView3 is in the MainActivity, not in any Fragment.