我想用 SurfaceView 创建演示应用程序。但是在一行“ GameThread = gameView.getThread(); ”中收到错误 NullPointerException这是我的整个代码片段
主要活动:
public class MainActivity extends Activity {
GameView gameView;
TutorialThread GameThread;
Button btnStart, btnStop;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("OnCreate -", "I am in onCreate method..");
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
// get handles to the GameView from XML and the Tutorial thread.
gameView = (GameView) findViewById(R.id.gameView);
//Log.d("OnCreate -", ""+gameView.TAG);
GameThread = gameView.getThread();
//GameThread.start();
ClickListener clickListener = new ClickListener();
// look up the happy shiny button
btnStart = (Button) findViewById(R.id.btnStart);
btnStart.setOnClickListener(clickListener);
btnStop = (Button) findViewById(R.id.btnStop);
btnStop.setOnClickListener(clickListener);
setContentView(R.layout.activity_main);
}
class ClickListener implements View.OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btnStart:
/*
* if (GameThread.mState == GameThread.STATE_PLAY) {
* GameThread.setGameState(TutorialThread.STATE_RUNNING);
*
* }
*/
makeToast("This is Start Button");
break;
case R.id.btnStop:
/*
* if (GameThread.mState == GameThread.STATE_RUNNING) { //
* GameThread.setGameState(TutorialThread.STATE_PLAY); }
*/
makeToast("This is Stop Button");
break;
}
}
private void makeToast(String string) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, string, Toast.LENGTH_LONG).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
和 GameView 类是: -
public class GameView extends SurfaceView implements SurfaceHolder.Callback {
String TAG = "GameView";
private TutorialThread _thread;
public GameView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
getHolder().addCallback(this);
_thread = new TutorialThread(getHolder(), context);
setFocusable(true);
}
public TutorialThread getThread() {
return _thread;
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
_thread.setRunning(true);
_thread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
// we have to tell thread to shut down & wait for it to finish, or else
// it might touch the Surface after we return and explode
boolean retry = true;
_thread.setRunning(false);
while (retry) {
try {
_thread.join();
retry = false;
} catch (InterruptedException e) {
// we will try it again and again...
}
}
}
}
class TutorialThread extends Thread {
String TAG = "TutorialThread";
private SurfaceHolder _surfaceHolder;
private boolean _run = false;
Bitmap bitmapDroid;
/**
* State-tracking constants.
*/
public static final int STATE_PLAY = 0;
public static final int STATE_RUNNING = 1;
public int mState = STATE_PLAY;
Resources mRes;
/** Handle to the application context, used to e.g. fetch Drawables. */
private Context mContext;
float droidx = 200, droidy = 200;
public TutorialThread(SurfaceHolder surfaceHolder, Context context) {
_surfaceHolder = surfaceHolder;
mContext = context;
mRes = context.getResources();
// create droid and load bitmap
bitmapDroid = BitmapFactory
.decodeResource(mRes, R.drawable.ic_launcher);
}
public void setRunning(boolean run) {
_run = run;
}
@Override
public void run() {
Canvas c;
while (_run) {
c = null;
try {
c = _surfaceHolder.lockCanvas(null);
synchronized (_surfaceHolder) {
onDraw(c);
}
} finally {
// do this in a finally so that if an exception is thrown
// during the above, we don't leave the Surface in an
// inconsistent state
if (c != null) {
_surfaceHolder.unlockCanvasAndPost(c);
}
}
}
}
public void onDraw(Canvas canvas) {
canvas.drawColor(Color.rgb(187, 255, 255));
canvas.drawBitmap(bitmapDroid, droidx - (bitmapDroid.getWidth() / 2),
droidy - (bitmapDroid.getHeight() / 2), null);
}
}
布局声明为:
<com.example.surfaceviewdemo.GameView
android:id="@+id/gameView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />