我遇到了一個問題,在啓動的Intent中,findViewById返回null。findViewById在新的Intent中返回null
有什麼特別的我應該知道開始一個新的意圖?
它是這樣對我說:
//in the MainList class
Intent stuffList = new Intent(this, StuffList.class);
然後在新的東西的構造函數:
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));
,並在StuffAdapter我做我通常的看法和數據檢索。
記下findViewById返回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"));
}
}
好奇的是,在調用意圖(MainList類)類的,我做同樣的事情(我列出了一堆的對象)它的工作原理!然而,當我嘗試以Intent執行它時,它似乎無法通過id找到視圖。
爲了完整,我想補充一點,我的列表資源文件叫做「stuff_list.xml」,我的行佈局文件叫做「stuff_row.xml」。
人,這個錯誤我發瘋... – drozzy 2010-06-17 13:02:30