android上自定义DialogBox中的NullPointerException

时间:2011-07-18 11:34:43

标签: android alertdialog

在我的应用程序中,我有自定义对话框,通过第一个活动中的按钮单击显示。在自定义dialog box我有数字轮子来选择数字。用户界面显示正确,但当我实现编码时,我得到null pointer exception

main.java:

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //set up main content view
    setContentView(R.layout.main);
    //this button will show the dialog
    Button button1main = (Button) findViewById(R.id.Button01main);

    button1main.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
            Dialog dialog = new Dialog(main.this);
            View myView = LayoutInflater.from(getApplicationContext()).inflate( R.layout.dp, null);

            dialog.setContentView(myView);
            dialog.setTitle("This is my custom dialog box");
            dialog.setCancelable(true);

            initWheel1(myView); 

            text = (TextView) findViewById(R.id.result);

            dialog.show();
        }
    });
}
.......
private void initWheel1(View dialogView)    
 {
WheelView wheel = (WheelView) dialogView.findViewById(R.id.p1);
wheel.setAdapter(new ArrayWheelAdapter<String>(wheelMenu1));  
wheel.setVisibleItems(2);
wheel.setCurrentItem(0);
wheel.addChangingListener(changedListener);
wheel.addScrollingListener(scrolledListener);
 }

 // Wheel scrolled listener
OnWheelScrollListener scrolledListener = new OnWheelScrollListener()
    {
        public void onScrollStarts(WheelView wheel)
            {
                wheelScrolled = true;
            }

        public void onScrollEnds(WheelView wheel)
            {
                wheelScrolled = false;
                updateStatus();  // null pointer exception 
            }
    };

// Wheel changed listener
private final OnWheelChangedListener changedListener = new OnWheelChangedListener()
    {
        public void onChanged(WheelView wheel, int oldValue, int newValue)
            {
                if (!wheelScrolled)
                    {
                          updateStatus();                       }
            }
    };

    private void updateStatus()
    {
        text.setText(wheelMenu1[getWheel(R.id.p1).getCurrentItem()]);  // null pointer exception.
    }

    private WheelView getWheel(int id)
    {
        return (WheelView) findViewById(id);
    }

    private int getWheelValue(int id)
    {
        return getWheel(id).getCurrentItem();
    }

记录猫:

  07-18 18:26:56.061: ERROR/AndroidRuntime(4650):     at com.custom.dialog.main.updateStatus(main.java:124)
  07-18 18:26:56.061: ERROR/AndroidRuntime(4650):     at com.custom.dialog.main.access$1(main.java:122)
  07-18 18:26:56.061: ERROR/AndroidRuntime(4650):     at com.custom.dialog.main$1.onScrollEnds(main.java:106)
  07-18 18:26:56.061: ERROR/AndroidRuntime(4650):     at com.custom.dialog.WheelView.notifyScrollingListenersAboutEnd(WheelView.java:262)
  07-18 18:26:56.061: ERROR/AndroidRuntime(4650):     at com.custom.dialog.WheelView.onTouchEvent(WheelView.java:720)

2 个答案:

答案 0 :(得分:0)

你不能直接给出你想要检索的视图的id。如果你想要检索与id R.id.p1相关联的视图,那么你必须给它具有findViewByID(R.id.p1); < / p>

答案 1 :(得分:0)

其id为R.id.p1的wheelView,在哪个布局中定义? main.xml或dp.xml?