为什么findViewById()在这里不能作为getActivity().findViewById()使用? [英] Why findViewById() doesn't work as getActivity().findViewById() here?

查看:224
本文介绍了为什么findViewById()在这里不能作为getActivity().findViewById()使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_detail,
                    container, false);
            // The detail Activity called via intent.  Inspect the intent for forecast data.
            Intent intent=getActivity().getIntent();
            TextView text=(TextView)rootView.findViewById(R.id.text1);
            String b=intent.getStringExtra(Intent.EXTRA_TEXT);
            text.setText(b);

            return rootView;
        }
    }

在我使用此代码时

TextView text=(TextView)getActivity().findViewById(R.id.text1)

然后我的应用崩溃了,Logcat说了

Then my app crashes and Logcat says that

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference

这是与此代码关联的XML文件

This is the XML file associated with this code

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.sunshine.app.DetailActivity$PlaceholderFragment" >

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

推荐答案

在此代码中,当我使用TextView text =(TextView)getActivity().findViewById(R.id.text1)

In this code when I am using TextView text=(TextView)getActivity().findViewById(R.id.text1)

然后我的应用程序崩溃,Logcat指出原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'android.view.View android.view.Window.findViewById(int)'

Then my app crashes and Logcat says that Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference

Activity上的

findViewById()从根视图(称为装饰视图")开始,并遍历可用窗口小部件和容器的树,以查找ID匹配项.因此,在Activity上调用的findViewById()将仅查找属于整个活动视图层次结构的视图.

findViewById() on an Activity starts from the root view (what is referred to as the "decor view") and traverses the tree of available widgets and containers, looking for an ID match. Hence, findViewById() called on an Activity will only find views that are part of the overall activity view hierarchy.

但是,您的inflate()调用不会将膨胀的RelativeLayout附加到活动视图层次结构.这是预期的.从片段onCreateView()返回后的某个时间,当片段本身被附加时,这些视图将被附加到活动视图层次结构中.结果,当您从onCreateView() 内部调用活动上的findViewById()时,它找不到您的TextView,因为该TextView仅由片段管理,尚未被片段管理.活动视图层次结构的一部分.

However, your inflate() call does not attach the inflated RelativeLayout to the activity view hierarchy. This is expected. Those views will be attached to the activity view hierarchy when the fragment itself is attached, sometime after you return from onCreateView(). As a result, when you call findViewById() on the activity from inside onCreateView(), it cannot find your TextView, because that TextView is only being managed by the fragment and is not yet part of the activity view hierarchy.

findViewById()从该ViewViewGroup开始,并遍历可用窗口小部件和容器的树,以查找ID匹配项. RelativeLayout可以访问您的TextView,因此当您在RelativeLayout上调用findViewById()进行充气时,它将起作用.

findViewById() on a View or ViewGroup starts from that View or ViewGroup and traverses the tree of available widgets and containers, looking for an ID match. The RelativeLayout has access to your TextView, so when you call findViewById() on the RelativeLayout that you inflate, it works.

这篇关于为什么findViewById()在这里不能作为getActivity().findViewById()使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆