21

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) {

    }
}

我是新手,我不知道为什么我的应用程序在按下按钮时崩溃?谁能解释一下?

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
4

5 回答 5

58

活动:

如果有活动并且如果您android:onClick在 XML 中定义属性,那么您只需要在 Activity 中定义一个具有相同名称的方法。

分段:

但是,只要您有 Fragment 并且如果您想通过仅定义android:onClick属性来定义单击侦听器,那么您必须在调用 Fragment 的实际活动中定义一个具有相同名称的方法。

或者您可以简单地以编程方式实现点击侦听器。

于 2013-09-19T13:56:10.647 回答
11

onClickListener通过以编程方式设置(而不是通过 XML),您可以省去很多麻烦。这应该可以帮助您做您想做的事情:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.second_fragment, container, false);
    final View button = view.findViewById(R.id.f2_button);
    button.setOnClickListener(
        new OnClickListener() {
            @Override
            public void onClick(View v) {
                /* DO SOMETHING UPON THE CLICK */
            }
        }
    );
    return view;
}
于 2013-01-03T14:03:15.060 回答
5

如果您在 .xml 中定义,则需要在调用片段的ActivityonClick="button"中定义一个名为的方法,而不是在片段本身中定义。public void button(View v)

public class MainActivity extends FragmentActivity{

...

  public void button(View v){
    ...
  }

...

}
于 2013-01-03T13:28:37.293 回答
1

您的方法按钮必须在 MainActivity 类中定义。

于 2013-01-03T13:35:31.340 回答
1

这一直为我工作:(Android工作室)

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

        View rootView = inflater.inflate(R.layout.update_credential, container, false);
        Button bt_login = (Button) rootView.findViewById(R.id.btnSend);

        bt_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                System.out.println("Hi its me");


            }// end onClick
        });

        return rootView;

    }// end onCreateView
于 2014-11-26T13:11:45.793 回答