Reputation: 113
I newbie to Android development and I have a question. I already look for this question in forum but no luck.
My MainActivity onCreate tries to connect to a sever. In some cases the server can be down, and in this case, my app throw exception. What should I do to finish the Activity gracefully?
I have tried to Toast a message and to finish() the activity but no message appears and the activity still running.
Whats wrong with my code ?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
mSocket = new Socket("127.0.0.1", 6000);
try {
conn = new ClientConnection(mSocket.getInputStream(),mSocket.getOutputStream());
GameSurfaceView gameSurface = (GameSurfaceView)findViewById(R.id.Game);
conn.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (UnknownHostException e) {
Log.e(TAG, "Unknown Host Exception");
e.printStackTrace();
Toast.makeText(this, "Unknown Host Exception", Toast.LENGTH_SHORT);
finish();
} catch (IOException e) {
Log.e(TAG, "IO Exception");
Toast.makeText(this, "IO Exception", Toast.LENGTH_SHORT);
finish();
}
catch (Exception e) {
Log.e(TAG, "Exception");
Toast.makeText(this, "IO Exception", Toast.LENGTH_SHORT);
finish();
}
Log.v(TAG,"Activity created");
}
Upvotes: 0
Views: 72
Reputation: 1699
Edit: As pointed out in a deleted answer, calling show() is of course also needed in top of what I already mentioned:
Toast.makeText(this, "Unknown Host Exception", Toast.LENGTH_SHORT).show();
Upvotes: 1