Tuesday, June 4, 2013

Andorid Code Zone: findViewById returns null

If  findViewById returns null,

you are probably callingfindViewById before calling setContentView.
If that's the case, try callingfindViewById AFTER calling setContentView like:

setContentView(R.layout.home);

try {

runOnUiThread(new Runnable() {

@Override
public void run() {

((Button) findViewById(R.id.my_button)).setVisibility(View.GONE);
}
});
} catch (Exception e) {

Toast.makeText(Home.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}

No comments:

Post a Comment