Android 按钮数组
无法找到在 android 中制作按钮数组的方法。
这是我尝试过的代码,但我收到了 java.lang.NullPointerException。
private Button[] button = {(Button) findViewById(R.id.cGuess1),
(Button) findViewById(R.id.cGuess2),(Button)
findViewById(R.id.cGuess3),(Button) findViewById(R.id.cGuess4)};
这可能吗?
编辑:
对不起大家。刚刚意识到我的错误!
我试图为整个类声明数组,并尝试在 onCreate 之前从 ids 获取视图,因此没有 setContentView(R.layout.game);
对不起。
Can't work out a way to make an array of buttons in android.
This is the code I have tried but I'm getting a java.lang.NullPointerException.
private Button[] button = {(Button) findViewById(R.id.cGuess1),
(Button) findViewById(R.id.cGuess2),(Button)
findViewById(R.id.cGuess3),(Button) findViewById(R.id.cGuess4)};
Is this even possible?
EDIT:
Sorry everyone. Just realised my mistake!
I was trying to declare the array for my whole class and trying to get the views from the ids before onCreate so there was no setContentView(R.layout.game);
Sorry.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
由于没有其他人发布解决方案的实际代码,因此这里有一个工作片段。
Since no one else posted actual code for a solution, here's a working snippet.
只是猜测,因为这里没有完整的代码,您是否在创建数组之前调用了 setContentView() 。
Just a guess as full code is not available here, have you called setContentView() before creating array.
你能尝试一下吗
Could you try
您的按钮之一可能为空。并且放置 private 关键字不允许我创建数组。另请注意,首先您要为您的活动设置 cententView,然后访问这些按钮。
One of your buttons may be null. And putting a private keyword does not allow me to create the array. Also see that Firstly you are setting the cententView for your activity and then accessing these buttons.