findViewById返回null

时间:2013-08-01 10:22:43

标签: java android pointers 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。这是为什么?

2 个答案:

答案 0 :(得分:0)

将代码放入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);

    }

答案 1 :(得分:0)

请确认您已将布局文件放入平板电脑的相应布局文件夹中。请查看此文档,它将介绍如何在平板电脑http://developer.android.com/guide/practices/screens_support.html

中整理布局文件