【问题标题】:TimePicker NullPointerException on ICSICS 上的 TimePicker NullPointerException
【发布时间】:2012-03-07 04:10:49
【问题描述】:

好的,所以我刚刚将我的 TimePickerDialog 切换为 TimePicker 小部件,因为客户需求,我正在处理的活动中直接可见。

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

澄清一下,除了我的活动的onCreate() 方法中的这一行之外,没有任何代码附加到TimePicker

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

我在 Google 论坛上找到了有关此问题的 this post,但没有太大帮助。

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

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

【问题讨论】:

  • 我无法重现它!!!!
  • 你在时间选择器上设置了监听器吗?
  • 有同样的问题,但我已经在 4.0 中运行了一个简单的应用程序并且它可以工作。嗯。

标签: android nullpointerexception android-4.0-ice-cream-sandwich timepicker


【解决方案1】:

刚刚写了一个示例应用程序,在 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>

仅供参考:http://developer.android.com/reference/android/widget/TimePicker.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-08
    • 1970-01-01
    • 1970-01-01
    • 2012-12-05
    • 1970-01-01
    • 1970-01-01
    • 2012-07-12
    • 1970-01-01
    相关资源
    最近更新 更多