1

私はアクティビティがある小さなゲームを作成しています:

ゲームアクティビティ.java:

public class GameActivity extends Activity {

    public static Button btnPause;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // requesting to turn the title OFF
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // making it full screen
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.game_layout);
        btnPause = (Button) findViewById(R.id.buttonPause);

    }

    @Override
    public void onBackPressed() {
        Toast.makeText(this,
                "No effect, use the `||` button at top left of screen!",
                Toast.LENGTH_SHORT).show();
    }

}

上記のレイアウト

game_layout.xml :

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.spaceinvaders.game.GameSurfaceView
        android:id="@+id/surfaceView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/buttonPause"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/button_pause"
                android:textAppearance="?android:attr/textAppearanceLarge" >
            </Button>

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="right|center_vertical"
                android:layout_weight="1"
                android:gravity="right"
                android:text="@string/health"
                android:textAppearance="?android:attr/textAppearanceLarge" />

            <ProgressBar
                style="@android:style/Widget.ProgressBar.Horizontal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right|center_vertical"
                android:layout_marginRight="5dp"
                android:minWidth="175dp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="right|center_vertical"
                android:layout_weight="1"
                android:gravity="right"
                android:text="@string/ammo"
                android:textAppearance="?android:attr/textAppearanceLarge" />

            <ProgressBar
                style="@android:style/Widget.ProgressBar.Horizontal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginRight="5dp"
                android:minWidth="175dp" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom" >

        <com.spaceinvaders.joystick.JoystickView
            android:id="@+id/joystickView"
            android:layout_width="100dp"
            android:layout_height="100dp" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="right|center_vertical"
            android:layout_weight="1" />

        <Button
            android:id="@+id/buttonShoot"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right|center_vertical"
            android:text="X"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </LinearLayout>

</FrameLayout>

お気づきかもしれませんが、似たようなカスタムコンポーネント/ 、GameSurfaceView があります。SurfaceView

GameSurfaceView.java :

public class GameSurfaceView extends SurfaceView implements
        SurfaceHolder.Callback {

    private GameLoop gameLoop;

    private Bitmap background;
    private Button btnPause;

    public GameSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public GameSurfaceView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs);
        init();
    }

    public GameSurfaceView(Context context) {
        super(context);
        init();
    }

    private void init() {

        btnPause = GameActivity.btnPause;
            //causes NPE 
        btnPause.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
            }
        });

        // adding the callback (this) to the surface holder to intercept events
        getHolder().addCallback(this);

        // create the game loop thread
        gameLoop = new GameLoop(getHolder(), this);

        // make the GamePanel focusable so it can handle events
        setFocusable(true);
    }
}

基本的に、ボタンbuttonPauseを からgame_layout.xmlカスタムに渡して;SurfaceView GameSurfaceViewを追加しようとしています。OnClickListener静的変数を使用していますが、NullPointerException.

クラスでは簡単にできますが、GameActivityシュートなどのボタンでは問題になります...

したがって、基本的に私の質問は、Button/component を my GameActivity/game_layoutから、そのアクティビティ レイアウト内のクラスにGameSurfaceView含まれるに渡す方法です。game_layout.xml

私は、同じ結果(NPE)を生成するを追加するよりもfindViewById(R.id.buttonPause)、自分で使用してみました。GameSurfaceViewOnClickListener

前もって感謝します

4

2 に答える 2

1

GameSurfaceView を xml に配置する代わりに、ビューを動的に追加します。このようにして、Button パラメーターを持つ GameSurfaceView のコンストラクターを使用できます。

于 2013-05-12T18:12:54.557 に答える
0

findViewById()あなたの と呼ばれているため、彼は でボタンを見つけることができませんSurfaceView
それが機能するかどうかはよくわかりませんが、次のものを取得してみてButtonくださいSurfaceView

Button btnPause = (Button) getParent().findViewById(R.id.buttonPause);

編集:
ああ、申し訳ありませんが、あなたはViewParent..getParent()次の
ようにコンパイルされます:

View v = (View) getParent();
Button btnPause = (Button) v.findViewById(R.id.buttonPause);
于 2013-05-12T18:18:32.087 に答える