2

In my activity I am inflating a layout from a xml file which contains a Button and a fragment. activityView is my View object.

     LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    Intent intent = getIntent();
    View activityView = layoutInflater.inflate(R.layout.my_profile, null,false);

I get button lik this: docButton=(Button) activityView.findViewById(R.id.doc_prof_name);

My appforce closes if I get fragment lik this: FragmentManager fm = getFragmentManager(); final Fragment f1= fm.findFragmentById(R.id.fragment1);

I am not getting how to get fragment instance which is in activityView i.e my_profile.xml

        <Button
    android:id="@+id/btn1"  
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="2"
    android:text="abc" />

<fragment android:name="com.example.myapp.MainActivity$FirstFragment"
            android:id="@+id/fragment1" android:layout_weight="1"
            android:layout_width="0px" android:layout_height="wrap_content"
             />

this is my logcat error

09-29 15:08:18.319: E/AndroidRuntime(12901):    ... 11 more
09-29 15:08:18.319: E/AndroidRuntime(12901): Caused by: java.lang.NullPointerException
09-29 15:08:18.319: E/AndroidRuntime(12901):    at com.example.tdoapp.DocProfile$SecondFragment.onCreateView(DocProfile.java:130)
09-29 15:08:18.319: E/AndroidRuntime(12901):    at android.app.Fragment.performCreateView(Fragment.java:1700)
09-29 15:08:18.319: E/AndroidRuntime(12901):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:866)
09-29 15:08:18.319: E/AndroidRuntime(12901):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1040)
09-29 15:08:18.319: E/AndroidRuntime(12901):    at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1142)
09-29 15:08:18.319: E/AndroidRuntime(12901):    at android.app.Activity.onCreateView(Activity.java:4835)
09-29 15:08:18.319: E/AndroidRuntime(12901):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
09-29 15:08:18.319: E/AndroidRuntime(12901):    ... 19 more
gprathour
  • 14,813
  • 5
  • 66
  • 90
Manasi
  • 348
  • 1
  • 4
  • 14
  • possible duplicate of [App crashes with FragmentManager Replace when using GoogleMaps](http://stackoverflow.com/questions/22119051/app-crashes-with-fragmentmanager-replace-when-using-googlemaps) – rudedude Sep 29 '14 at 09:31
  • no i am not understanding where would I use View object here. FragmentManager fm = getFragmentManager(); final Fragment f1= fm.findFragmentById(R.id.fragment1); – Manasi Sep 29 '14 at 09:56
  • Can you post the FirstFragment class's code? – rudedude Sep 30 '14 at 02:29
  • firstFragment is a linearLayout with a single TextView. – Manasi Oct 01 '14 at 05:57
  • No, i mean, to use fragments, you have to subclass the fragment class and in the onCreateView method you manage the Fragment's view. I wanted to see the Fragment Controller's class. – rudedude Oct 01 '14 at 06:32

1 Answers1

0

The question is a bit old, but it is possible to find a fragment knowing one of its views (and even call an onClick handler from a fragment). But it will require registration of the root view in the fragment's onCreateView().

https://stackoverflow.com/a/26887675/755804

Basically, each view has a tag (probably null). We set the root view's tag to the fragment that inflated that view. Then, it is easy to search the view parents and retrieve the fragment containing the view (e.g. clicked button).

Community
  • 1
  • 1
18446744073709551615
  • 16,368
  • 4
  • 94
  • 127