-1

TabBookList.java

public class TabBooklist extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.tab_booklist, container, false);
    return rootView;

    //error->
    Button btn_test = getActivity().findFragmentById(R.id.btn_test);
    //<-
    }
}


tab_booklist.xml

...
tools:context="org.androidtown.mobile_termproj_0408.MainActivity$PlaceholderFragment">

<TableRow>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="push"
        android:id="@+id/btn_test"
    />

    <TextView
        android:id="@+id/section_label2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="book list tab2" />

</TableRow>


The tab_booklist.xml is created by fragment and TabBookList.java is link with tab_booklist.xml.
I want to get button object in xml file.
I found using getActivity().findFragmentById(R.id.btn_test); that get xml object in fragment.
But I can't do that.
How to get xml object in fragment?

redchicken
  • 311
  • 1
  • 5
  • 18

1 Answers1

3

The button is a descendant of the root view of your fragment, so just use rootView. And Button is a view, not a fragment.

Button btn_test = (Button) rootView.findViewById(R.id.btn_test);
ephemient
  • 198,619
  • 38
  • 280
  • 391