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

时间:2018-11-16 05:03:58

标签: android android-timepicker

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

我相信这段代码在构建和部署到我的设备时会出错,但是在打开时会崩溃。

@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文件中吗?我认为这将是一个主要的活动命令,因为选择的时间值必须是应用程序的全局值,并且必须在应用程序终止期间保存。

这是错误日志:

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)

1 个答案:

答案 0 :(得分:0)

之所以发生这种情况,是因为您试图在MainActivity之外的TextView中设置文本。

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

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

确保 R.id.textView3 在MainActivity中,而不在任何片段中。