2013-04-27 169 views
0

我有下面的XML佈局文件片段片段返回null

public class competitionFragment extends Fragment { 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
     View view = inflater.inflate(R.layout.competition_details_fragment, 
       container, false); 
    return view;  
} 
public void setText(String item) { 
    TextView view = (TextView) getView().findViewById(R.id.detailsText); 
    view.setText(item); 
} 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/detailsText" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="center_horizontal|center_vertical" 
     android:layout_marginTop="20dip" 
     android:text="Default Text" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:textSize="30dp" /> 

</LinearLayout> 

這個片段被放在我的活動誰擁有此佈局如下:

<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:background="@drawable/background" 
tools:context=".SearchCompetetionsActivity" > 

<ListView 
    android:id="@+id/android:list" 
    android:layout_width="100dp" 
    android:layout_height="504dp" 
    android:layout_alignLeft="@+id/searchView1" 
    android:layout_alignParentBottom="true" 
    android:layout_alignRight="@+id/searchView1" 
    android:layout_below="@+id/searchView1" > 
</ListView> 

<EditText 
    android:id="@+id/searchView1" 
    android:layout_width="500dp" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:layout_marginLeft="72dp" 
    android:layout_marginTop="53dp" 
    android:background="@android:color/background_light" 
    android:ems="10" 
    android:hint="@string/search" 
    android:inputType="text" > 

    <requestFocus /> 
</EditText> 

<fragment 
    android:id="@+id/competitionDetails" 
    android:layout_width="500dp" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignTop="@+id/searchView1" 
    android:layout_marginLeft="101dp" 
    android:layout_toRightOf="@+id/android:list" 
    class="com.example.konkurrencesigner.competitionFragment" /> 

在我的活動(存有片段)我有當列表項被選中下面的代碼:

public void onListItemClick(ListView parent, View v, int position, long id){ 
    competitionFragment fragment = (competitionFragment) getFragmentManager().findFragmentById(R.layout.competition_details_fragment); 

Toast.makeText(this, 
      ""+fragment.isAdded(), 
      Toast.LENGTH_SHORT).show(); 

} 

當我運行我的應用程序可以看到的TextView的默認文本然而,當我選擇一個項目我在這裏得到一個空指針豁免:competitionFragment fragment = (competitionFragment) getFragmentManager().findFragmentById(R.layout.competition_details_fragment);任何人都可以告訴我爲什麼會發生這種情況?

+0

林不知道,但當你infalte它可能會失去與佈局ID conexion。嘗試在充氣後手動將該ID放入片段中。 – 2013-04-27 15:19:21

回答

1

您的分段的ID是competitionDetails每個XML。因此,你的電話應該是

competitionFragment fragment = (competitionFragment) 
    getFragmentManager().findFragmentById(R.id.competitionDetails); 

記住,如果你正在做的任何findById通話,您必須使用R.id值。 R.layout僅用於充氣佈局。

+0

非常感謝您的幫助 – 2013-04-27 16:10:10

相關問題