android的findviewbyid,android-findViewById在片段中返回null

我在片段中使用findViewByID方法遇到了问题.

Spinner中的微调器spinner =(Spinner)view.findViewById(R.id.tabsearchspinner_location);始终为null.请参见下面的代码.

public class FragmentTabSearch extends SherlockFragment implements OnItemSelectedListener {

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState) {

// TODO Auto-generated method stub

View view = inflater.inflate(R.layout.fragment_tab_suggestions, container, false);

Spinner spinner = (Spinner) view.findViewById(R.id.tabsearchspinner_location);

ArrayAdapter adapter = ArrayAdapter.createFromResource(this.getSherlockActivity(), R.array.spinner_location, android.R.layout.simple_spinner_item);

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

spinner.setAdapter(adapter);

return view;

}

@Override

public void onItemSelected(IcsAdapterView> parent, View view,

int position, long id) {

// TODO Auto-generated method stub

}

@Override

public void onNothingSelected(IcsAdapterView> parent) {

// TODO Auto-generated method stub

}

}

布局文件如下(ps:我已经在values / ids.xml文件中声明了id / tabsearchspinner_location):< item type =“ id” name =“ tabsearchspinner_location”< // item>

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

android:id="@id/tabsearchspinner_location"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

android:id="@id/tablistview_search"

android:layout_width="match_parent"

android:layout_height="wrap_content" >

任何帮助表示赞赏!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值