NullPointerException in custom DialogBox on android
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);
开发者_JS百科 }
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)
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);
The wheelView whose id is R.id.p1, defined in which layout? main.xml or dp.xml?
精彩评论