Android 上自定义对话框中的 NullPointerException
在我的应用程序中,我有自定义对话框,通过在第一个活动中单击按钮来显示该对话框。在自定义对话框
中,我有数字轮来选择数字。 UI 显示正确,但当我实现编码时,我收到空指针异常
。
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)
In my app i have custom dialog which is displayed by a button click in first activity. In the custom dialog box
i have number wheels to select numbers. The UI is displayed correctly but when i implement coding i am getting 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();
}
Log cat:
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不能直接给出你想要检索的视图的 id。如果你想检索与 id R.id.p1 关联的视图,那么你必须给它 has findViewByID(R.id.p1);
you cannot directly give the id of the view you want to retrieve.if u want to retrieve the view associated with the id R.id.p1 then u have to give it has findViewByID(R.id.p1);
id为R.id.p1的wheelView,定义在哪个布局中?
main.xml 还是 dp.xml?
The wheelView whose id is R.id.p1, defined in which layout?
main.xml or dp.xml?