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

时间:2016-03-17 17:41:41

标签: android android-fragments android-activity onclick onclicklistener

首先来自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侦听器可能在host 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>

ChooseFragment:

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 /值/ 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>

1 个答案:

答案 0 :(得分:2)

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

首先使用<?php function isSubstring($s1, $s2) { if(strlen($s1) != strlen($s2)) { return false; } if(WHAT TO PUT HERE) { echo "it is!"; } else { echo "nope"; } } isSubstring("hello", "helol"); ?> 获取片段的onCreateView()方法中的按钮,然后使用button = expandedLayout.findViewById(R.id.viewId)在其上设置点击侦听器。