1

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)
M.A.Murali
  • 9,988
  • 36
  • 105
  • 182

2 Answers2

0

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);

Aparna Suresh
  • 415
  • 2
  • 11
  • hi i give as you told initWheel1() ... private void initWheel1(){ WheelView wheel = (WheelView) findViewById(R.id.p1);... } . but i get same null pointer exception on the same lines. please help me. – M.A.Murali Jul 18 '11 at 11:58
  • thinkso u might not have intialized the wheelmenu items – Aparna Suresh Jul 18 '11 at 12:06
  • i have initialized with String wheelMenu1[] = new String[]{"10", "20","30","40","50","60"}; in class for wheel.setAdapter(new ArrayWheelAdapter(wheelMenu1)); if i am wrong please correct me.. – M.A.Murali Jul 18 '11 at 12:30
0

The wheelView whose id is R.id.p1, defined in which layout? main.xml or dp.xml?

Mani
  • 2,599
  • 4
  • 30
  • 49
  • Inside initWheel1() Inflate the layout and refer the wheel like below private void initWheel1() { View view = LayoutInflater.from(this).inflate( R.layout.dp, null); WheelView wheel = (WheelView) view.findViewById(R.id.p1); // Rest of your code – Mani Jul 18 '11 at 12:31
  • Before calling initWheel1 and setting view for dialog inflate the view View myView = LayoutInflater.from(getApplicationContext()).inflate( R.layout.dp, null); Now set view for dialog has dialog.setContentView(myView); Pass the myView object to initWheel1(View dialogView) Inside initWhee1(View dialogView) get refrence for wheel like WheelView wheel = (WheelView) dialogView.findViewById(R.id.p1); – Mani Jul 18 '11 at 12:34
  • You are getting NULLPOINTER exception because of R.id.p1 is outside of activity context. Hence you need to inflate it in a view. Set the view for dialog and pass it for initwheel1(). Inside initWheel1() using the view call findViewById(). – Mani Jul 18 '11 at 13:23
  • please see my edited post. i get ui displayed as you told in your comment. my problem is if touch number wheel i get null pointer exception. please help me. – M.A.Murali Jul 18 '11 at 13:31
  • @user630386 let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/1581/discussion-between-murali-ma-and-user630386) – M.A.Murali Jul 18 '11 at 13:31
  • please see my edited post. i get ui displayed as you told in your comment. my problem is if touch number wheel i get null pointer exception. please help me. – M.A.Murali Jul 18 '11 at 13:32
  • i tried but i do not know why i am get null pointer exception i sent id by directly R.id.p1 please help me. – M.A.Murali Jul 18 '11 at 13:39
  • In the edited code what exception you get and in which line? Could you post the log elaborately? Where you have initialized the wheelMenu1? – Mani Jul 19 '11 at 04:15
  • i initialized the wheel menu in the class(before the OnCreate method) as String wheelMenu1[] = new String[]{"10", "20","30","40","50","60"}; in class for wheel.setAdapter(new ArrayWheelAdapter(wheelMenu1));.. i get null pointer exception updateStatus();. i have mentioned in the coding where the null pointer exception i get. – M.A.Murali Jul 19 '11 at 04:55
  • Make "myView" as class variable, initialize it on oncreate() (make sure before button1main declaration). And wherever you refer R.id.p1 do like this myView.findViewById(R.id.p1) – Mani Jul 19 '11 at 08:39
  • The problem is R.id.p1 is outside main.xml context. To refer the any control of outside the activity's layout, You require an context. – Mani Jul 19 '11 at 08:44