私はアクティビティがある小さなゲームを作成しています:
ゲームアクティビティ.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)
、自分で使用してみました。GameSurfaceView
OnClickListener
前もって感謝します