Thursday 16 June 2011

Null returned when using findViewById


Hello,
sometime you have nullpointerexception when using findViewById like the code snippet bellow
                TextView tv=(TextView)findViewById(R.id.textViewVV);
if(tv!=null)
tv.setText("Not Null");
this is done because you call different of the set layout
the solution is to call views from the layout you use on the current Activity
like this

setContentView(R.layout.test);
TextView tv=(TextView)findViewById(R.id.textViewVV);
if(tv!=null)
tv.setText("Good");
textViewVV is in test layout, so, this code works fine.


No comments:

Post a Comment