Android 上自定义对话框中的 NullPointerException

发布于 2024-11-24 10:27:29 字数 3043 浏览 0 评论 0原文

在我的应用程序中,我有自定义对话框,通过在第一个活动中单击按钮来显示该对话框。在自定义对话框中,我有数字轮来选择数字。 UI 显示正确,但当我实现编码时,我收到空指针异常

ma​​in.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

阳光的暖冬 2024-12-01 10:27:29

你不能直接给出你想要检索的视图的 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);

此生挚爱伱 2024-12-01 10:27:29

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文