问题描述
|
所以我有一个ѭ0,它是从ѭ1扩展而来的。而且我有一个来自XML的线性布局。
名为
example
的XML:
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<LinearLayout
xmlns:android=\"http://schemas.android.com/apk/res/android\"
xmlns:app=\"http://schemas.android.com/apk/res/jembalang.comfest.game\"
android:layout_width=\"fill_parent\"
android:layout_height=\"fill_parent\">
<jembalang.compfest.game.GameThread
android:id=\"@+id/game_view\"
android:layout_width=\"fill_parent\"
android:layout_height=\"fill_parent\">
</jembalang.compfest.game.GameThread>
<Button
android:text=\"Button\"
android:id=\"@+id/button1\"
android:layout_width=\"wrap_content\"
android:layout_height=\"wrap_content\">
</Button>
</LinearLayout>
以及使用xml的代码
setContentView(R.layout.cobagabung);
gameView = (GameThread) Jembalang.this.findViewById(R.id.game_view);
gameView.setFocusable(true);
gameView.setFocusableInTouchMode(
gameView.start();
如果有帮助,我添加了GameThread构造函数
public class GameThread extends View implements Runnable,OnKeyListener{
public GameThread(Context context,AttributeSet attr){
super(context,attr);
...
}
public GameThread(Context context) {
super(context);
...
}
我认为这样做有问题,因为findViewById
返回null
如何使我的CustomView
(在此示例中为GameThread
)能够插入xml?
解决方法
您的行应显示为:
gameView = (GameThread) Jembalang.this.findViewById(R.id.game_view);
您传递的是布局的ID,而不是您创建的视图的ID。
您的其余代码看起来不错
, 我不知道ѭ11是什么,但是我认为你应该删除它。
gameView = (GameThread) findViewById(R.id.game_view);
, 您说您的布局文件名为\“ example.xml \”,但您调用setContentView(R.layout.cobagabung)。因此,您的视图是从\“ cobagabung.xml \”初始化的。
确保对布局文件名和setContentView调用使用相同的标识符,例如
setContentView(R.layout.example);