Marc Rasmussen
Marc Rasmussen

Reputation: 20565

Fragment returns null

i have a fragment

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

with the following xml layout file:

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

This fragment is placed in my activity who has this following layout:

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

}

when i run my application i can see the default text of the textview however when i select an item i get a nullpointer exepection here: competitionFragment fragment = (competitionFragment) getFragmentManager().findFragmentById(R.layout.competition_details_fragment); can anyone tell me why this is happening?

Upvotes: 0

Views: 79

Answers (1)

ianhanniballake
ianhanniballake

Reputation: 200130

The ID of your fragment is competitionDetails per your XML. Therefore your call should be

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

Remember if you are doing any findById call, you must use a R.id value. R.layout is only used for inflating layouts.

Upvotes: 1

Related Questions