问题
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
来源:https://stackoverflow.com/questions/9299812/timepicker-nullpointerexception-on-ics