Null Pointer实现Android Wheel

时间:2013-05-21 04:51:16

标签: android widget scrollwheel

我正在尝试实施Yuri Kanivets的Android车轮选择器。我只是想把一些琴弦放在一个轮子上。是什么让我的实现有点不同,我试图在AlertDialog.Builder上显示轮子。我可以在我的对话框中显示一个轮,但我似乎无法用任何东西填充它。每次我尝试向滚轮添加值时,我都会得到一个空指针异常。

如果有人熟悉这个,你能看出我做错了吗?我强烈怀疑我在ArrayWheelAdapter中使用的上下文是错误的,但我不知道如何做到这一点。这是我的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_find_events);

    LayoutInflater inflater = LayoutInflater.from(this);
    builderView = inflater.inflate(R.layout.wheel_range_layout, null);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setView(builderView);       
    alert = builder.create();

    final WheelView range = (WheelView) findViewById(R.id.range);
    int defaultRange = 0;
    String ranges[] = new String[] {"10 miles", "25 miles", "50 miles", "Everywhere"};

    ArrayWheelAdapter<String> adapter = new ArrayWheelAdapter<String>(this, ranges);
    adapter.setTextSize(18);

    //this next line is where it is crashing -- maybe adapter is null???
    range.setViewAdapter(adapter);
    range.setCurrentItem(defaultRange);

    alert.show();
}

1 个答案:

答案 0 :(得分:1)

使用

final WheelView range = (WheelView)builderView. findViewById(R.id.range);

而不是

final WheelView range = (WheelView) findViewById(R.id.range);

用于初始化WheelView range实例,因为WheelView View位于DialogBox wheel_range_layout布局而不是Activity布局