1

Actually i am tying to make an app with swipe tabs. Downloaded the codes for swipe tabs. There it has 3 fragments and MainActivity which extends the FragmentActivityand the viewpager is used. The swipe tabs are working fine. Now i am trying to add a button in one of the fragments and show a toast when the button is clicked. Below is the code i added in the xml file for the fragment:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#fa6a6a" >

    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Design Top Rated Screen"
        android:textSize="20dp"
        android:layout_centerInParent="true"/>

    <Button
        android:id="@+id/btn_frgmnt" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Go to actvity"
        android:onClick="navToActivity"
        />
</RelativeLayout>

And inside the java file for this fragment i have done :

public class TopRatedFragment extends Fragment 
{

    Button btnNav;

      Context context;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_top_rated, container, false);
        btnNav = (Button)rootView.findViewById(R.id.btn_frgmnt);

        return rootView;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        super.onCreate(savedInstanceState);
        //btnNav = (Button)rootView.findViewById(R.id.btn_frgmnt);
        context = getActivity().getApplicationContext();

    }


    public void navToActivity(View v)
    {
        Toast.makeText(getActivity().getApplicationContext(), "Clicked inside fragment!", 2000).show();
    }
}

But its showing error! Where did i goo wrong. Or what to do to make the button click work. Its this fragment class where i should write the codes for the button click, right?

    08-23 11:58:31.385: E/AndroidRuntime(3033): FATAL EXCEPTION: main
    08-23 11:58:31.385: E/AndroidRuntime(3033): Process: info.androidhive.tabsswipe, PID: 3033
    08-23 11:58:31.385: E/AndroidRuntime(3033): java.lang.IllegalStateException: Could not find a method navToActivity(View) in the activity class info.androidhive.tabsswipe.MainActivity for onClick handler on view class android.widget.Button with id 'btn_frgmnt'
    08-23 11:58:31.385: E/AndroidRuntime(3033):     at android.view.View$1.onClick(View.java:3810)
    08-23 11:58:31.385: E/AndroidRuntime(3033):     at android.view.View.performClick(View.java:4438)
    08-23 11:58:31.385: E/AndroidRuntime(3033):     at android.view.View$PerformClick.run(View.java:18422)
    08-23 11:58:31.385: E/AndroidRuntime(3033):     at android.os.Handler.handleCallback(Handler.java:733)
    08-23 11:58:31.385: E/AndroidRuntime(3033):     at android.os.Handler.dispatchMessage(Handler.java:95)
    08-23 11:58:31.385: E/AndroidRuntime(3033):     at android.os.Looper.loop(Looper.java:136)
    08-23 11:58:31.385: E/AndroidRuntime(3033):     at android.app.ActivityThread.main(ActivityThread.java:5001)
    08-23 11:58:31.385: E/AndroidRuntime(3033):     at java.lang.reflect.Method.invokeNative(Native Method)
    08-23 11:58:31.385: E/AndroidRuntime(3033):     at java.lang.reflect.Method.invoke(Method.java:515)
    08-23 11:58:31.385: E/AndroidRuntime(3033):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
    08-23 11:58:31.385: E/AndroidRuntime(3033):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
    08-23 11:58:31.385: E/AndroidRuntime(3033):     at dalvik.system.NativeStart.main(Native Method)
    08-23 11:58:31.385: E/AndroidRuntime(3033): Caused by: java.lang.NoSuchMethodException: navToActivity [class android.view.View]
    08-23 11:58:31.385: E/AndroidRuntime(3033):     at java.lang.Class.getConstructorOrMethod(Class.java:472)
    08-23 11:58:31.385: E/AndroidRuntime(3033):     at java.lang.Class.getMethod(Class.java:857)
    08-23 11:58:31.385: E/AndroidRuntime(3033):     at android.view.View$1.onClick(View.java:3803)
    08-23 11:58:31.385: E/AndroidRuntime(3033):     ... 11 more
Rhythm Shaon
  • 127
  • 1
  • 6
  • 1
    possible duplicate of [onClick inside fragment called on Activity](http://stackoverflow.com/questions/7570575/onclick-inside-fragment-called-on-activity) – Simas Aug 23 '15 at 06:12

3 Answers3

3

In order to catch button event of fragment use View of fragment xml or simply register callback like this:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_top_rated, container, false);
        btnNav = (Button)rootView.findViewById(R.id.btn_frgmnt);

        btnNav.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Action to be performed on button click
            }
        });

        return rootView;
    }
Shahzeb
  • 3,696
  • 4
  • 28
  • 47
0

Call back function for the button for which click events declared in xml file, must be in your activity class. So you must have the call back function declared in your xml, Otherwise you will get an exception.

To get the click event in your fragment, you need to call the 'navToActivity' method in your fragment explicitly.So add the same method in your Activity and call the fragment method from button click callback in activity

public void navToActivity(View v)
    {
        topRatedFragment.navToActivity(v);
    }
Prasad
  • 3,462
  • 1
  • 23
  • 28
  • Thanks. But where to initialize the button and write the code for button clickListener if i didnt define the method in xml file for fragment? – Rhythm Shaon Aug 23 '15 at 06:11
  • Don't get confused. Initialization part will be done in your fragment only.. But click callbacks will be go to activity.. – Prasad Aug 23 '15 at 06:16
0

Note that with the XML above, Android will look for the onClick method navToActivity() only in the current Activity. This is important to remember if you are using fragments, since even if you add the XML above using a fragment, Android will not look for the onClick method in the .java file of the fragment used to add the XML.

sara1010
  • 11
  • 5