TimePicker NullPointerException on ICS

匿名 (未验证) 提交于 2019-12-03 08:46:08

问题:

Alright so I just switched my TimePickerDialog to a TimePicker widget directly visible in the Activity I'm working on because of customer demand.

The problem is when I press any of the arrows on said TimePicker, I get a NullPointerException.

Just to clarify, no code what so ever is attached to the TimePicker except this line in the onCreate() method of my Activity:

((TimePicker) findViewById(R.id.time_picker)).setIs24HourView(true);

I found this post on the Google Forums regarding this issue, but not much help.

Here's my error log, I sadly don't think it'll be much help though.

This issue only appears when testing in ICS (Android 4.0 +). Has anyone been able to work around this issue ?

回答1:

Just wrote a sample application and no error found in Galaxy SIII (4.0):

import android.app.Activity; import android.os.Bundle; import android.widget.TextView; import android.widget.TimePicker; import android.widget.TimePicker.OnTimeChangedListener;  public class TimePickerTestActivity extends Activity implements OnTimeChangedListener{      private TimePicker tpTime = null;     private TextView tvTime = null;     /** Called when the activity is first created. */     @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);          tvTime = (TextView)this.findViewById(R.id.tv_time);          tpTime = (TimePicker)this.findViewById(R.id.tp_time);         tpTime.setIs24HourView(Boolean.TRUE);         tpTime.setOnTimeChangedListener(this);      }     public void onTimeChanged(TimePicker tp, final int hrOfDay, final int min) {         if(tp.equals(tpTime)){             tvTime.post(new Runnable(){                  public void run() {                     tvTime.setText(hrOfDay+":"+min);                 }              });         }     } }

And the layout xml:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout   xmlns:android="http://schemas.android.com/apk/res/android"                 android:layout_width="fill_parent"                 android:layout_height="fill_parent"                 android:orientation="vertical" >      <TimePicker android:id="@+id/tp_time"                 android:layout_width="fill_parent"                 android:layout_height="wrap_content" />      <TextView   android:id="@+id/tv_time"                 android:layout_width="fill_parent"                 android:layout_height="wrap_content" /> </LinearLayout>

FYI: http://developer.android.com/reference/android/widget/TimePicker.html



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!