代码之家  ›  专栏  ›  技术社区  ›  Joseph P Nardone

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

  •  0
  • Joseph P Nardone  · 技术社区  · 6 年前

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

    @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 回复  |  直到 6 年前
        1
  •  0
  •   Birju Vachhani    6 年前

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

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

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

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