2013-08-01 147 views
1

我正在用幾個片段編寫一個Android應用程序。在手機上,只會顯示一個片段,並在平板電腦上顯示三個片段。這裏是我的佈局不同的設備:findViewById返回null

平板佈局:

<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。爲什麼是這樣?

回答

0

只需將代碼放入片段中的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); 

    }