それは私の最初の質問ですが、正しく尋ねようとしています。
タイトルにあるように、1 つのボタンでのみ、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" />
これまでのところ、ありがとうございました。
答え:
それは醜い以上のものであることが判明しました。このプロジェクトには複数の人が取り組んでいるため、そのうちの 1 人が layout-normal/main.xml で名前を変更したようです。3.1 より前の Android バージョンは layout/main.xml をロードするように見えますが、新しいバージョンは layout-normal/main.xml を使用します。そして、そのボタンのIDが異なる場合は理解できるので、findViewById(int)はnullを返す必要があります。
ご迷惑をおかけして申し訳ありません。コメントありがとうございます。
私の評判は100未満なので、自分の質問に答えて閉じることはできません。誰かがこの回答を投稿できれば、私はそれを受け入れます、ありがとう。