Jea*_*Roy 6 android nullpointerexception timepicker android-4.0-ice-cream-sandwich
好吧所以我只是因为客户需求而将我转换为我正在进行的活动中直接可见TimePickerDialog的TimePicker小部件.
问题是,当我按下任何箭头时TimePicker,我得到一个NullPointerException.
只是为了澄清,TimePicker在onCreate()我的Activity 的方法中除了这一行之外没有任何代码附加了什么:
((TimePicker) findViewById(R.id.time_picker)).setIs24HourView(true);
我在Google论坛上发现了关于此问题的这篇文章,但帮助不大.
这是我的错误日志,我遗憾地认为这不会有太多帮助.
只有在ICS(Android 4.0 +)中进行测试时才会出现此问题.有没有人能够解决这个问题?
刚刚编写了一个示例应用程序,在 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);
}
});
}
}
}
Run Code Online (Sandbox Code Playgroud)
以及布局 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>
Run Code Online (Sandbox Code Playgroud)
仅供参考:http://developer.android.com/reference/android/widget/TimePicker.html