I have been struggling with this issue since a long time.
My app code is based on lunar lander with modifications. I have got a splash screen which then displays a menu screen which launches my main activity.
In my App, I have a claSS Game which I have declared in the main.xml as follows:-
I am pasting the portion of the xml file:-
<horizontalscrollview xmlns:android="http://schemas.android.com/apk/res/android">
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<relativelayout>
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:removed="@drawable/desert"
android:clipToPadding="true">
<com.example.android.game>
android:id="@+id/Game"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/></com.example.android.game></relativelayout></horizontalscrollview>
I am trying to inflate this activity from my app's MainActivity as follows:-
Unlike lunar lander my thread is seperate class from the Main activity.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
mGame = (Game) findViewById(R.id.Game);
mGameThread = mGame.getThread();
I have created a thread the definition of which is in a seperate file but but which is declared as part of Game class..
The problem I am facing is that the surfacecreated for my Game class is never called.So the thread never runs.
Below I am pasting the Game Class
public class Game extends SurfaceView implements SurfaceHolder.Callback{
private int mX;
private int mY;
private int mSpeedX;
private int mSpeedY;
private Bitmap mBitmap;
private GameThread thread;
public static Context mContext;
private TextView mStatusText;
private boolean mSurfaceExists;
public Game(Context context)
{
super(context);
SurfaceHolder holder = getHolder();
holder.addCallback(this);
Log.w(this.getClass().getName(),"Before thread create");
thread = new GameThread(holder, context, new Handler() {
@Override
public void handleMessage(Message m) {
Log.w(this.getClass().getName(),"In Handle message");
mStatusText.setVisibility(m.getData().getInt("viz"));
mStatusText.setText(m.getData().getString("text"));
}
});
setFocusable(true);
}
public Game(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
SurfaceHolder holder = getHolder();
holder.addCallback(this);
Log.w(this.getClass().getName(),"Before thread create");
thread = new GameThread(holder, context, new Handler() {
@Override
public void handleMessage(Message m) {
Log.w(this.getClass().getName(),"In Handle message");
mStatusText.setVisibility(m.getData().getInt("viz"));
mStatusText.setText(m.getData().getString("text"));
}
});
setFocusable(true);
}
public Game(Context context, AttributeSet attrs) {
super(context, attrs);
SurfaceHolder holder = getHolder();
holder.addCallback(this);
Log.w(this.getClass().getName(),"Before thread create");
thread = new GameThread(holder, context, new Handler() {
@Override
public void handleMessage(Message m) {
Log.w(this.getClass().getName(),"In Handle message");
mStatusText.setVisibility(m.getData().getInt("viz"));
mStatusText.setText(m.getData().getString("text"));
}
});
setFocusable(true);
}
public GameThread getThread() {
return thread;
}
@Override
public void onWindowFocusChanged(boolean hasWindowFocus) {
if (!hasWindowFocus) thread.pause();
}
public void setTextView(TextView textView) {
mStatusText = textView;
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Log.w(this.getClass().getName(),":In surface changed");
thread.setSurfaceSize(width, height);
}
public void surfaceCreated(SurfaceHolder arg0) {
mSurfaceExists = false;
Log.w(this.getClass().getName(),"In Surface create");
Log.w(this.getClass().getName(),":Before starting thread at create");
thread.setRunning(true);
thread.start();
}
public void surfaceDestroyed(SurfaceHolder arg0) {
boolean retry = true;
mSurfaceExists = false;
Log.w(this.getClass().getName(),"In Surface destroy");
thread.setRunning(false);
while (retry) {
try {
thread.join();
retry = false;
} catch (InterruptedException e) {
}
}
}