0

だから私はCustomViewから拡張された を持っていViewます。そして、XML からの線形レイアウトがあります。という名前の XML example:

<?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ます.nullCustomViewGameThread

4

3 に答える 3

0

何なのかわかりませんがJembalang、削除するべきだと思います。

gameView = (GameThread) findViewById(R.id.game_view);
于 2011-11-14T18:49:30.830 に答える
0

あなたの行は次のようになります。

gameView = (GameThread) Jembalang.this.findViewById(R.id.game_view);

作成したビューのIDではなく、レイアウトのIDを渡しています。

コードの残りの部分は正常に見えます

于 2011-05-31T11:23:20.040 に答える