0

我有一个片段

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

使用以下 xml 布局文件:

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

in my activity ( the one holding the fragment) i have the following code when a list item is selected:

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);谁能告诉我为什么会这样?

4

1 回答 1

1

您的片段的 ID 是competitionDetails每个您的 XML。因此你的电话应该是

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

请记住,如果您正在执行任何 findById 调用,则必须使用一个R.id值。R.layout仅用于膨胀布局。

于 2013-04-27T15:20:35.437 回答