Android app crashing (fragment and xml onclick)

second_fragment.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/f2_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/f2_textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/f2_tv" />

<Button
    android:id="@+id/f2_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="button"
    android:text="@string/f2_bttn" />

SecondFragment.java

   public class SecondFragment extends Fragment {

    FragmentInterface iface;

    public interface FragmentInterface {
        public void buttonPressed();
    }

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

        return inflater.inflate(R.layout.second_fragment, container, false);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception
        try {
            iface = (FragmentInterface) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement FragmentInterface");
        }

    }

    public void button(View view) {

    }
}

I'm newbie and I have no idea why my application crash, when button is pressed ? Can anyone explain?

01-03 13:28:25.612: E/AndroidRuntime(1276): FATAL EXCEPTION: main
01-03 13:28:25.612: E/AndroidRuntime(1276): java.lang.IllegalStateException: Could not find a method button(View) in the activity class com.sp.fragments.MainActivity for onClick handler on view class android.widget.Button with id 'f2_button' 
01-03 13:28:25.612: E/AndroidRuntime(1276): at android.view.View$1.onClick(View.java:3584) 
01-03 13:28:25.612: E/AndroidRuntime(1276): at android.view.View.performClick(View.java:4202) 
01-03 13:28:25.612: E/AndroidRuntime(1276): at android.view.View$PerformClick.run(View.java:17340) 
01-03 13:28:25.612: E/AndroidRuntime(1276): at android.os.Handler.handleCallback(Handler.java:725) 
01-03 13:28:25.612: E/AndroidRuntime(1276): at android.os.Handler.dispatchMessage(Handler.java:92) 
01-03 13:28:25.612: E/AndroidRuntime(1276): at android.os.Looper.loop(Looper.java:137) 
01-03 13:28:25.612: E/AndroidRuntime(1276): at android.app.ActivityThread.main(ActivityThread.java:5039) 
01-03 13:28:25.612: E/AndroidRuntime(1276): at java.lang.reflect.Method.invokeNative(Native Method) 
01-03 13:28:25.612: E/AndroidRuntime(1276): at java.lang.reflect.Method.invoke(Method.java:511) 
01-03 13:28:25.612: E/AndroidRuntime(1276): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
01-03 13:28:25.612: E/AndroidRuntime(1276): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
01-03 13:28:25.612: E/AndroidRuntime(1276): at dalvik.system.NativeStart.main(Native Method) 
01-03 13:28:25.612: E/AndroidRuntime(1276): Caused by: java.lang.NoSuchMethodException: button [class android.view.View] 
01-03 13:28:25.612: E/AndroidRuntime(1276): at java.lang.Class.getConstructorOrMethod(Class.java:460) 
01-03 13:28:25.612: E/AndroidRuntime(1276): at java.lang.Class.getMethod(Class.java:915) 
01-03 13:28:25.612: E/AndroidRuntime(1276): at android.view.View$1.onClick(View.java:3577) 
01-03 13:28:25.612: E/AndroidRuntime(1276): ... 11 more 
01-03 13:28:27.563: I/Process(1276): Sending signal. PID: 1276 SIG: 9

Answers


Activity:

If are having activity and if you define android:onClick attribute in XML then you just need to define a method with the same name in Activity.

Fragment:

But whenever you have Fragment and if you want to define click listener by just defining android:onClick attribute then you have to define a method with the same name in actual activity from where Fragment has been called.

OR and otherwise, you can simply implement a click listener programmatically.


Need Your Help

Struts2 File Upload

jsp file-upload struts2

I want to upload multiple files in one form and that number of files is varying from user to another, ex. a user may upload 2 files and another user could upload 12 files. I want to know how could I

Reading and understanding MCU datasheet and codes

microcontroller specifications electronics datasheet

Are there any tips for reading source code samples from manufacturer of MCUs'.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.