简体   繁体   English

findViewById返回null

[英]findViewById returns null

I'm writing an Android app with several fragments. 我正在编写带有多个片段的Android应用程序。 On a phone, only one fragment will be displayed, and on a tablet, three fragments. 在电话上,将仅显示一个片段,而在平板电脑上,将显示三个片段。 Here are my layouts for the different devices: 这是我针对不同设备的布局:

Tablet layout: 平板电脑布局:

<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>

Phone layout: 手机布局:

<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>

Here's the code for adding the fragments to the layout in the main activity's OnCreate method: 这是将片段添加到主要活动的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();
        }
    }

And here's the fragment's onStart method: 这是片段的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);
}

When I start it on a phone, everything works fine. 当我在手机上启动它时,一切正常。 When I start it on a tablet, I get a null pointer exception. 在平板电脑上启动时,出现空指针异常。 Apparently, findViewById returns null in this case. 显然,在这种情况下,findViewById返回null。 Why is this? 为什么是这样?

Just Put your code in OnCreateView() Method in Fragment . 只需将您的代码放在Fragment的OnCreateView()方法中即可。

See the following example 请参阅以下示例

@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);

    }

Please verify that you have put your layout files in corresponding layout folder for tablets. 请确认您已将布局文件放置在平板电脑的相应布局文件夹中。 Please check out this document and it will describe how organize your layout files in tablets, http://developer.android.com/guide/practices/screens_support.html 请查看此文档,它将描述如何在平板电脑中组织布局文件, http://developer.android.com/guide/practices/screens_support.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM