2012-02-15 59 views
6

好吧,我只是将我的TimePickerDialog切换为TimePicker小部件,因为客户需求,我正在处理的活动中直接显示它。ICS上的TimePicker NullPointerException

问题是当我按下上述TimePicker上的任何箭头,我得到一个NullPointerException。

只是为了澄清,没有代码什么那么连接到TimePicker除了这条线在我的活动的onCreate()方法:

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

我发现在谷歌论坛关于这个问题this post,但没有太大的帮助。

Here's my error log,我很遗憾地认为这不会有太大的帮助。

此问题仅在ICS(Android 4.0 +)中测试时出现。有没有人能够解决这个问题?

+0

我没能重现它! – dira 2012-05-22 13:56:43

+0

你是否在时间选择器上设置了一个监听器? – LuxuryMode 2012-06-11 14:54:11

+0

有同样的问题,但我已经在4.0中运行了一个简单的应用程序,它的工作原理。哼。 – Mikey 2012-09-21 04:02:27

回答

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); 
       } 

      }); 
     } 
    } 
} 

而且布局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