Android 片段崩溃中的时间选择器

Android Time Picker In Fragment Crashing

我有一个包含三个片段的应用程序。在第二个片段上,我有一个时间选择器,我想将在这个时间选择器上选择的值提供给主 activity.

我认为这部分代码在构建和部署到我的设备时存在错误,但在打开时崩溃了。

@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));


        }

我是否将这段代码放在了错误的 java 文件中?我认为这将是一个主要的 activity 命令,因为选择的时间值对于应用程序需要是全局的,并且必须在应用程序终止期间保存。

这是错误日志:

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)

发生这种情况是因为您试图在不在 MainActivity 中的 TextView 中设置文本。

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

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

确保 R.id.textView3 在 MainActivity 中,而不是在任何 Fragment 中。