findViewById返回null

findViewById返回null

我正在编写带有多个片段的Android应用程序。在电话上,将仅显示一个片段,而在平板电脑上,将显示三个片段。这是我针对不同设备的布局:

平板电脑布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainTopLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:baselineAligned="false"
    android:orientation="horizontal"
    android:background="@color/activity_background"
    android:tag="@string/layout_tablet_landscape" >

<FrameLayout android:id="@+id/leftContainerLayout"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="0.33" />

<FrameLayout android:id="@+id/centerContainerLayout"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="0.33" />

<FrameLayout android:id="@+id/rightContainerLayout"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="0.33" />

</LinearLayout>


手机布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainTopLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/activity_background"
    android:tag="@string/layout_phone_portrait" >

<FrameLayout android:id="@+id/mainContainerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_above="@+id/mainBrewAd" />

<com.google.ads.AdView xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:id="@+id/mainBrewAd"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    ads:adUnitId="##############"
    ads:adSize="BANNER"
    ads:loadAdOnCreate="true"/>

</RelativeLayout>


这是将片段添加到主要活动的OnCreate方法中的布局的代码:

if ((layoutTag == LAND_PHONE) || (layoutTag == PORT_PHONE)) {
        GravityFragment gravityFragment = (GravityFragment)fragmentManager.findFragmentById(R.id.mainContainerLayout);
        if (gravityFragment == null) {
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            transaction.add(R.id.mainContainerLayout, new GravityFragment());
            transaction.commit();
        }
    }
    if ((layoutTag == LAND_TABLET) || (layoutTag == PORT_TABLET)) {
        GravityFragment gravityFragment = (GravityFragment)fragmentManager.findFragmentById(R.id.leftContainerLayout);
        if (gravityFragment == null) {
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            transaction.add(R.id.leftContainerLayout, new GravityFragment());
            transaction.commit();
        }
    }


这是片段的onStart方法:

@Override
public void onStart() {
    super.onStart();
    //Init a brewcalc object
    brewCalc = new BrewCalc(getActivity());
    //Assign the click listener
    Activity topActivity = getActivity();
    Button calcButton = (Button)getActivity().findViewById(R.id.gravityCalculateButton);
    calcButton.setOnClickListener(calculateListener);
}


当我在手机上启动它时,一切正常。在平板电脑上启动时,出现空指针异常。显然,在这种情况下,findViewById返回null。为什么是这样?

最佳答案

只需将您的代码放在Fragment的OnCreateView()方法中即可。

请参阅以下示例

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        activityContext = getActivity();
        View parentView = inflater.inflate(R.layout.sync_fargment,container,false);

        ImageView abc= (ImageView) parentView.findViewById(R.id.abc);
        ImageView xyz= (ImageView) parentView.findViewById(R.id.xyz);

    }

10-08 12:04