문제

I am pretty newbie with android development and I am trying to make work an easy example.

When the layout contains fragments, this fragments are inflated and the method findByViewId returns a NULL pointer. I would like to konw how to avoid that.

activity_main.xml 

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.todoit.MainActivity"
    tools:ignore="MergeRootFrame" />

so far the only solution I found was to dont use fragment layout :/

fragment_main.xml 
    <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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.todoit.MainActivity$PlaceholderFragment">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/myEditText"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true" />

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/myListView"
        android:layout_below="@+id/myEditText"
        android:layout_alignRight="@+id/myEditText"
        android:footerDividersEnabled="false" /> </RelativeLayout>

this code returned a NULL pointer

setContentView(R.layout.activity_main); ListView myListView= (ListView)findViewById(R.id.myListView);

the only way I found was to use this one instead.

setContentView(R.layout.fragment_main); ListView myListView= (ListView)findViewById(R.id.myListView);

all this code in the onCreate method.

could someone tell me how to solve this issue? is there a way to inflate the whole layout including fragments?

thanks a lot. Jose

도움이 되었습니까?

해결책

You should inflate the fragment's view in the fragment code only, not in the activity code. You use onCreateView method to do this.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_main, container, false);
    ListView myListView= (ListView) v.findViewById(R.id.myListView);
    return v;
}

If you want to find your ListView after onCreateView has already been called, you can use getView() method:

ListView myListView= (ListView) getView().findViewById(R.id.myListView);

다른 팁

ListView myListView= (ListView) fragment.getView().findViewById(R.id.myListView); needs to be called from inside the fragment class code and not the main activity. Call it inside method onActivityCreated, by that time the onCreateView function of fragment would have succeeded

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top