的片段查看我使用SherlockActionBar進行標籤導航。 在活動MyFragmentActivity延伸SherlockFragmentActivity我添加3標籤與片段在我的應用程序中查找活動
mTabsAdapter.addTab(
bar.newTab().setText(getString(R.string.filmtab)),
FragmentFilm.class, null);
mTabsAdapter.addTab(
bar.newTab()
.setText(getString(R.string.cinematab)),
FragmentCinema.class, null);
mTabsAdapter.addTab(
bar.newTab()
.setText(getString(R.string.dintornitab)),
FragmentPdi.class, null);
的FragmentPdi類有這樣的代碼:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.tab_pdi_info, container, false);
return view;
}
和佈局是
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="@+id/list_places"
android:layout_width="wrap_content"
android:layout_height="fill_parent" >
</ListView>
</RelativeLayout>
在MyFragmentActivity我想要以這種方式找到列表視圖來填充它:
final ListView elencoPlaces = (ListView)findViewById(R.id.list_places);
....
但我有NullPointerException
!
爲什麼?
在另一個片段(FragmentFilm和FragmentCinema)我有其他的看法像textview沒有這個 問題!
請幫幫我! 謝謝。
P.S.這是用於填充片段中視圖的正確方法,還是必須填充片段類中的視圖?如果第二我如何可以從活動傳遞到片段?
你好感謝你的答案。很抱歉,但我不明白,我必須FragmentPdi改用片段的ListFragment! ?請你解釋一下如何使用一個例子!?謝謝。 – user1437101
只需要用FragmentPdi擴展SherlockListFragment而不是SherlockFragment。刪除你的onCreateView方法。當你需要獲取listview時,調用FragmentPdi上的getListView()。 – toadzky
好的,謝謝,但我需要在MyFragmentActivity中獲得一個列表視圖。我怎樣才能在這個活動中得到它? – user1437101