【问题标题】:how to access UI elements in parent activity from fragment如何从片段访问父活动中的 UI 元素
【发布时间】:2013-01-24 03:58:01
【问题描述】:

父活动布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".LockerCodeActivity" >

    <LinearLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </LinearLayout>

    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:id="@+id/ctrlActivityIndicator"
        android:indeterminateOnly="true"
        android:keepScreenOn="false"
     />

    <TextView
        android:id="@+id/tv_results"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="" />

</RelativeLayout>

在父activity的onCreate函数中为fragment充气

FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    Fragment scannerFragment = new ScanFragment();
    fragmentTransaction.add(R.id.fragment_container, scannerFragment);
    fragmentTransaction.commit();

到目前为止工作得很好...现在如何隐藏进度条? 这是我尝试过的

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_scan, container, false);

    ProgressBar progressBar = (ProgressBar) view.findViewById(R.id.ctrlActivityIndicator);
    progressBar.setVisibility(View.INVISIBLE);
    return view;
    }

我得到一个空指针异常

【问题讨论】:

    标签: android


    【解决方案1】:

    既然你想要 Activity 的视图,你会想要这样做:

    ProgressBar progressBar = (ProgressBar) getActivity().findViewById(R.id.ctrlActivityIndicator);
    

    您调用 getActivity() 来获取 Activity 实例,然后像往常一样使用 findViewById()(前提是 R.id.ctrlActivityIndicator 是 Activity 布局的一部分,您不会得到 NPE)。

    【讨论】:

    • 谢谢,经过几个小时的搜索找到了这个。
    • getActivity() 的可能 NPE 怎么样?
    • @EdLee 我们必须在代码本身中处理这种情况,在进行相关调用之前检查 null。
    【解决方案2】:

    这对我不起作用,我将 getActivity 的结果转换为父活动。请参阅下面的链接: Call parent's activity from a fragment

    【讨论】:

    • 最好在答案中包含链接中的相关信息。如果将来链接断开怎么办?
    • 作为一个经验法则,您应该始终将 getActivity() 方法调用强制转换为它本应返回代码逻辑的活动。为了安全地做到这一点,您在铸造前进行检查。下面的示例,将片段内部的getActivity方法返回的活动转换为MainActivity if (getActivity instanceof MainActivity){ MainActivity activity = (MainActivity) getActivity(); }
    【解决方案3】:

    你可以使用:

    ((ParentActivity)getActivity()).UI_COMPONENT_NAME 
    

    访问父活动的ui组件。

    UI_COMPONENT_NAME 是在 ParentActivity 中初始化的组件的名称。

    【讨论】:

      猜你喜欢
      • 2022-01-02
      • 1970-01-01
      • 1970-01-01
      • 2016-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多