findViewById(int)在Android 3.1其他版本中的一个特定按钮上返回null

Mar*_*in 6 java xml android findviewbyid

这是我的第一个问题,但我正在试图正确地提问.

由于标题只在一个按钮上说,只在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);
}
Run Code Online (Sandbox Code Playgroud)

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" />
Run Code Online (Sandbox Code Playgroud)

到目前为止,任何帮助将不胜感激.


回答:

事实证明这不仅仅是丑陋的.由于有几个人在这个项目上工作,似乎在layout-normal/main.xml中更改了一个名称.虽然3.1之前的Android-Versions似乎加载layout/main.xml,但较新的版本使用layout-normal/main.xml.因为如果该按钮的id是不同的findViewById(int)必须返回null,这是可以理解的.

很抱歉打扰你,谢谢你的评论!

由于我的声誉不到100,我无法回答我自己的问题关闭它,如果有人能发布这个答案我会接受它,谢谢.

mch*_*ler 0

以下答案是提问者发现的。详细信息是:

由于有几个人在从事这个项目,其中一个人似乎更改了layout-normal/main.xml 中的名称。虽然 3.1 之前的 Android 版本似乎加载布局/main.xml,但较新的版本使用布局正常/main.xml。而且,如果该按钮的 id 不同,findViewById(int) 必须返回 null,这是非常容易理解的。