findViewById returns null in new Intent
Posted
by drozzy
on Stack Overflow
See other posts from Stack Overflow
or by drozzy
Published on 2010-06-17T12:07:44Z
Indexed on
2010/06/17
12:13 UTC
Read the original article
Hit count: 348
android
|android-sdk
I am having a problem where in the started Intent, the findViewById returns null.
Is there anything special I should know about starting a new intent?
It goes something like this for me:
//in the MainList class
Intent stuffList = new Intent(this, StuffList.class);
then in the new Stuff's constructor:
public class StuffList extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.stuff_list);
...
this.setListAdapter(new StuffAdapter(this, my_cursor));
and in the StuffAdapter I do my usual view and data retrieval.
Note the line where findViewById returns null:
class ViewWrapper{
View base;
TextView label = null;
ViewWrapper(View base){
this.base = base; }
TextView getLabel(){
if(label == null){
label = (TextView)base.findViewById(R.id.my_label); // returns NULL
}
return label;}
}
class StuffAdapter extends CursorAdapter{
StuffAdapter(Context context, Cursor cursor){
super(context, cursor);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.stuff_list, parent, false);
ViewWrapper wrapper = new ViewWrapper(row);
row.setTag(wrapper);
return(row);
}
@Override
public void bindView(View row, Context context, Cursor cursor) {
ViewWrapper wrapper = (ViewWrapper)row.getTag();
TextView label = wrapper.getLabel(); // also NULL
//this throws exception of course
label.setText(cursor.getString("title"));
}
}
The curious thing is that in the class that calls intent (MainList class), I do Exactly the same thing (i list a bunch of objects), and it Works! however when I try to do it in an Intent - it can't seem to find the view by id.
© Stack Overflow or respective owner