这是我的第一个问题,但我试图正确地问。
正如标题所说,仅在一个按钮上且仅在 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 版本似乎加载 layout/main.xml,但较新的版本使用 layout-normal/main.xml。而且,如果该按钮的 id 是不同的 findViewById(int) 必须返回 null,这是可以理解的。
很抱歉打扰您,感谢您的评论!
由于我的声誉确实少于 100,因此我无法回答自己的问题来关闭它,如果有人可以发布此答案,我会接受,谢谢。