0

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)
Milind Mevada
  • 3,145
  • 1
  • 14
  • 22
Joseph P Nardone
  • 150
  • 2
  • 12
  • 2
    add logcat pls.. – Basi Nov 16 '18 at 05:05
  • Post your error log – Piyush Nov 16 '18 at 05:06
  • 1
    If the `TimePicker` is in a `Fragment`, you should only be directly handling it in the `Fragment`, and delivering its data to the `Activity` through some other mechanism; e.g., an `interface`. Since not only is it in a `Fragment`, but also that `Fragment` is inside a `ViewPager`, the `TimePicker` is definitely not going to created and attached to the `Activity`'s hierarchy by the time the `Activity`'s `onCreate()` finishes, and the `findViewById()` for it will return null there. – Mike M. Nov 16 '18 at 05:08
  • If your time picker is in fragment then you need to change `findViewById` with `view.findViewById`. – Piyush Nov 16 '18 at 05:08
  • My apologies, just a moment, I have been running on a physical device so I did not have an error log. Good to know you can get it somehow. – Joseph P Nardone Nov 16 '18 at 05:09
  • @JosephPNardone please check [this](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – Basi Nov 16 '18 at 05:11
  • I have edited the post to include the error log. I think Mike is right that I have a fundamental misunderstanding of how the hierarchy works in Android. My apologies, I will post an answer to this once I have reworked a solution. – Joseph P Nardone Nov 16 '18 at 05:18
  • do you have timePicker1 object in activity class ? – Basi Nov 16 '18 at 05:19
  • 1
    The Exception is happening at the `time.setText()` call. If that `TextView` is in the same `Fragment` as the `TimePicker`, the same thing as I mentioned above applies: you should be handling it in the `Fragment`, not directly in the `Activity`. – Mike M. Nov 16 '18 at 05:21
  • @Basi Yes, the timePicker1 is in the main activity java file but the widget itself is in another fragment. – Joseph P Nardone Nov 16 '18 at 05:23
  • @MikeM. The text view is in the main activity, while the time picker gui is in the fragment. I believe you are correct that it need to be handled in the fragment. – Joseph P Nardone Nov 16 '18 at 05:24
  • post your activity_main file also please – Quick learner Nov 16 '18 at 05:34

1 Answers1

0

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.

Birju Vachhani
  • 6,072
  • 4
  • 21
  • 43