findViewById(int) 在 Android 3.1 其他版本中的一个特定按钮上返回 null 没问题
这是我的第一个问题,但我试图正确地提出问题。
正如标题所说,仅在一个按钮上且仅在 android 3.1 中 findViewById(int) 返回 null。在 Android 2.2 上进行测试,它可以正常工作,并且所有其他按钮都可以在两个 Android 版本中找到。它会查找该特定行之前和之后的按钮。
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonInfo = (Button)findViewById(R.id.buttonInfo);//fine
buttonInfo.setOnClickListener(this);
...
Button buttonCallOpScreen = (Button)findViewById(R.id.buttonCallOpScreen);//always returns null in android 3.1
...
Button buttonTakePicture = (Button)findViewById(R.id.buttonTakePictureMain);//fine
buttonTakePicture.setOnClickListener(this);
}
main.xml
:(
我确实知道我不应该使用 px
,但它只能在我的设备上工作,所以对于这种情况来说没问题。)
<Button
android:id="@+id/buttonInfo"
android:text=""
android:layout_width="332px"
android:layout_height="100px"
android:background="@drawable/btn_team" />
...
<Button
android:id="@+id/buttonCallOpScreen"
android:text=""
android:layout_width="332px"
android:layout_height="100px"
android:background="@drawable/btn_operator" />
...
<Button
android:id="@+id/buttonTakePictureMain"
android:text=""
android:layout_width="80px"
android:layout_height="80px"
android:background="@drawable/btn_take_picture"
android:layout_gravity="bottom|left" />
任何帮助都是感谢,到目前为止谢谢。
答案:
事实证明,这不仅仅是丑陋。由于有几个人在从事这个项目,其中一个人似乎更改了layout-normal/main.xml 中的名称。虽然 3.1 之前的 Android 版本似乎加载布局/main.xml,但较新的版本使用布局正常/main.xml。如果该按钮的 id 不同,那么 findViewById(int) 必须返回 null,这是可以理解的。
抱歉打扰您并感谢您的评论!
由于我的声誉确实低于 100,所以我无法回答我自己的问题来关闭它,如果有人可以发布这个答案,我会接受它,谢谢。
It's my first question but I'm trying to ask correctly.
As the title says only on one button and only in android 3.1 findViewById(int) returns null. Testing on Android 2.2 it works and all other buttons are found in both Android versions. It finds buttons before this specific line and afterwards.
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonInfo = (Button)findViewById(R.id.buttonInfo);//fine
buttonInfo.setOnClickListener(this);
...
Button buttonCallOpScreen = (Button)findViewById(R.id.buttonCallOpScreen);//always returns null in android 3.1
...
Button buttonTakePicture = (Button)findViewById(R.id.buttonTakePictureMain);//fine
buttonTakePicture.setOnClickListener(this);
}
main.xml
:
(I do know that I should not use px
, but it shall work on my device only, so that's fine for this case.)
<Button
android:id="@+id/buttonInfo"
android:text=""
android:layout_width="332px"
android:layout_height="100px"
android:background="@drawable/btn_team" />
...
<Button
android:id="@+id/buttonCallOpScreen"
android:text=""
android:layout_width="332px"
android:layout_height="100px"
android:background="@drawable/btn_operator" />
...
<Button
android:id="@+id/buttonTakePictureMain"
android:text=""
android:layout_width="80px"
android:layout_height="80px"
android:background="@drawable/btn_take_picture"
android:layout_gravity="bottom|left" />
Any help would be appreciated, thanks so far.
Answer:
It turned out to be a more than ugly. As there are several persons working on this project one of seemed to have changed the name in the layout-normal/main.xml. While Android-Versions before 3.1 seem to load layout/main.xml the newer ones use layout-normal/main.xml. And as it is more than understandable if the id of that button is a different findViewById(int) has to return null.
Sorry for bothering you with that and thanks for the comments!
As I do have less than 100 reputation, I can't answer my own question to close it, if anyone could post this answer I would accept it, thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下答案是提问者发现的。详细信息是:
The following answer was discovered by the person who asked the question. The details are: