我創建一個小遊戲,我有一個活動:試圖獲得或從相同的佈局/通過按鈕自定義SurfaceView
GameActivity.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>
正如你可能已經注意到有一個自定義組件/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
/組件從我GameActivity
/game_layout
到GameSurfaceView
它包含在該活動佈局內的game_layout.xml
類中。
我也嘗試過在我的GameSurfaceView
中使用findViewById(R.id.buttonPause)
,並且加入了產生相同結果(NPE)的OnClickListener
。
在此先感謝
嗯,謝謝你的建議我在GameActivity中試過這樣的事情:'FrameLayout fl =(FrameLayout)findViewById(R.layout.gam e_layout); fl.addView(new GameSurfaceView(this)); setContentView(fl);'但是我得到一個NPE:'fl.addView(new GameSurfaceView(this));' – 2013-05-12 18:22:25
由於init()調用而異常嗎?構造函數應該是GameSurfaceView(上下文上下文,按鈕按鈕)和init(按鈕按鈕)。所以你找到ViewById暫停按鈕,然後傳遞給構造函數。 – 2013-05-12 18:28:02
我甚至沒有嘗試通過按鈕只是想首先獲得'GameView''動態添加到GameAlay''game_layout .xml'但它不好... – 2013-05-12 18:31:07