【问题标题】:Find List View in fragment from Activity在 Activity 的片段中查找列表视图
【发布时间】:2012-10-08 21:37:16
【问题描述】:

在我的应用程序中,我使用带有标签导航的 SherlockActionBar。 在扩展 SherlockFragmentActivity 的活动 MyFragmentActivity 中,我添加了 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 中,我想找到 listview 以这种方式填充它:

final ListView elencoPlaces = (ListView)findViewById(R.id.list_places);

....

但我有NullPointerException

为什么??

在另一个片段(FragmentFilm 和 FragmentCinema)中,我有其他视图,例如 textview 没有这个 问题!!

请帮帮我! 谢谢。

附:这是在片段中动态填充视图的正确方法,还是我必须在片段类中填充视图?如果第二个我如何将变量从活动传递到片段??

【问题讨论】:

    标签: android tabs actionbarsherlock fragment


    【解决方案1】:

    如果您在布局文件中只有一个列表视图,请不要费心覆盖 oncreateview。默认布局会给你那么多。那么您可以使用 ListFragment 中内置的方法 - 例如 getListView() 和 setListAdapter()。

    【讨论】:

    • 您好,谢谢您的回答。抱歉,我不明白,我必须在 FragmentPdi 中使用 ListFragment 而不是 Fragment!?请你能给我解释一个例子!谢谢。
    • 只需让 FragmentPdi 扩展 SherlockListFragment 而不是 SherlockFragment。删除您的 onCreateView 方法。当您需要获取列表视图时,请在 FragmentPdi 上调用 getListView()。
    • 好的,谢谢,但我需要在 MyFragmentActivity 中填充列表视图。我怎样才能在这个活动中获得它???
    • listFragment.getListView() 或 listFragment.setListAdapter()。只需保留对您的片段的引用,然后您就可以调用fragment.getListView()。如果将引用保存为常规片段,则必须将 FragmentPdi 实例强制转换为 ListFragment,因此它将是 ((ListFragment)fragment).getListView()
    • 如何保持对我的片段的引用?抱歉,我是安卓编程新手!!
    【解决方案2】:

    好的,但我不明白为什么我有另一个片段 FragmentCinema 我有这个 xml:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout android:paddingTop="10dip" android:id="@+id/otherCinemas" android:layout_width="fill_parent" android:layout_height="wrap_content"
      xmlns:android="http://schemas.android.com/apk/res/android">
        <LinearLayout android:orientation="horizontal" android:id="@+id/plotSeparatorOtherCin" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/txtProgrammation">
            <ImageView android:layout_gravity="center_vertical" android:background="@drawable/palm_divider_line" android:layout_width="20.0dip" android:layout_height="wrap_content" android:layout_marginRight="5.0dip" />
            <TextView android:layout_gravity="center_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/altricinema" android:textStyle="bold" />
             <ImageView android:id="@+id/showOtherCinemas" 
              android:layout_width="wrap_content" 
              android:layout_height="wrap_content" 
              android:src="@drawable/ic_action_freccia_cinema"
              />
           <ImageView android:layout_gravity="center_vertical" android:background="@drawable/palm_divider_line" android:layout_width="fill_parent" android:layout_height="wrap_content" /> 
        </LinearLayout>
    
    
        <ListView
            android:id="@+id/listOtherCinemas"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_below="@id/plotSeparatorOtherCin"
            android:visibility="gone" >
        </ListView>
    
    </RelativeLayout> 
    

    还有这个类的代码:

    public class FragmentCinema extends SherlockFragment
    

    {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.tab_cinema_info, container, false);
    
        return view;
    }
    

    }

    当我使用 findViewbyId 最终 ListView 电影院Other = (ListView)findViewById(R.id.listOtherCinemas); 有用!但是在 FragmentPdi 中不起作用!

    【讨论】:

      【解决方案3】:

      如果你想在 ListFragments 中使用 ListView,你的 ListView 必须有 id android:id="@+id/android:list"!

      【讨论】:

      • 您可能需要考虑扩展此答案以提高其实用性
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-23
      • 2016-07-24
      • 1970-01-01
      • 2015-06-05
      • 1970-01-01
      相关资源
      最近更新 更多