我正在创建一个小游戏我有一个活动:
游戏活动.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();
}
}
上面的布局
游戏布局.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>
您可能已经注意到,有一个自定义组件/ SurfaceView
GameSurfaceView,它看起来也很相似:
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 从我的GameActivity
/game_layout
传递到GameSurfaceView
包含game_layout.xml
在该活动布局中的类中的 a 。
我也尝试过findViewById(R.id.buttonPause)
在我的和中使用,GameSurfaceView
而不是添加OnClickListener
产生相同结果(NPE)的。
提前致谢