为什么 Android 系统在主​​机 Activity 中寻找片段中存在的按钮的 onClick 侦听器?

Why is Android system looking for onClick listener for a button present in a Fragment, in the host Activity?

首先摘自logcat:

...
03-17 13:14:04.887: E/AndroidRuntime(2688): java.lang.IllegalStateException: Could not find a method readInInput(View) in the activity class tests.test.Testee.MainActivity for onClick handler on view class android.widget.Button
...
03-17 13:14:04.887: E/AndroidRuntime(2688): Caused by: java.lang.NoSuchMethodException: readInInput [class android.view.View]
...

我在以下 SSCCE 中遇到此错误。我最初认为片段中控件的 onClick 侦听器可能是在主机 Activity 中定义的,但是 this SO answer 说它们是在片段中定义的。 那么为什么我会得到这个异常?

SSCCE:

MainActivity.java:

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void startInitialConfiguration(View view) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        ChooseFragment chooseFragment = new ChooseFragment();
        fragmentTransaction.add(R.id.mainActivity_frameLayout, chooseFragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }
}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FA5858"
    android:orientation="vertical" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/mainActivity_button"
        android:onClick="startInitialConfiguration" />

    <FrameLayout android:id="@+id/mainActivity_frameLayout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

</LinearLayout>

选择片段:

public class ChooseFragment extends Fragment {

    @Override 
    public View onCreateView(LayoutInflater layoutInflater, ViewGroup container, Bundle savedInstanceState) {
        return layoutInflater.inflate(R.layout.fragment_choose, container, false); 
    }

    public void readInInput(View view) {
        view.setBackgroundColor(Color.parseColor("#FFBF00")); 
    }

}

fragment_choose:

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

    <TextView android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/dummyText" />

    <Button android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/dummyText"
        android:onClick="readInInput" />

</LinearLayout>

res/values/strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Testee</string>
    <string name="title_activity_main">MainActivity</string>
    <string name="hello_world">Hello world!</string>
    <string name="mainActivity_button">Start</string>
    <string name="dummyText">Dummy Text</string>

</resources>

如果您在 xml 本身中定义点击侦听器,则回调将转到包含该片段的 Activity。要在 Fragment 中获得回调,您必须在 Java 中分配点击侦听器。

首先使用 button = expandedLayout.findViewById(R.id.viewId) 在 Fragment 的 onCreateView() 方法中获取对按钮的引用,然后使用 button.setOnClickListener().

在其上设置点击监听器