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