片段返回null

时间:2013-04-27 15:14:18

标签: java android nullpointerexception

我有一个片段

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

在我的活动中(持有片段的那个)我在选择列表项时有以下代码:

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的默认文本,但是当我选择一个项目时,我会在这里得到一个nullpointer exepection:competitionFragment fragment = (competitionFragment) getFragmentManager().findFragmentById(R.layout.competition_details_fragment);任何人都可以告诉我为什么会这样吗?

1 个答案:

答案 0 :(得分:1)

根据您的XML,您的片段ID为competitionDetails。因此,您的电话应该是

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

请记住,如果您正在进行任何findById调用,则必须使用R.id值。 R.layout仅用于夸大布局。