onclick方法

kqhtkvqz  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(427)

当我将片段作为两个场景的根容器时。显示第一个场景1。当用户选择特定选项时,应用程序将进入第二个场景。scene2是scene1的副本,但只做了很少的修改,新的textview和button。
当用户点击按钮,我希望应用程序返回到以前的布局。我写的方法是:

public void exit(View v) {scene2.exit();}

并在按钮的onclick属性中将其名称作为参数传递。
不幸的是,我不能切换到以前的布局。系统引发以下异常:

java.lang.IllegalStateException: Could not find method exit(View) in a parent or ancestor Context for android:onClick attribute defined on view class com.google.android.material.button.MaterialButton with id 'button'

片段的java代码

public class SecondFragment extends Fragment {

    private ViewGroup rootContainer;
    private Scene scene1, scene2;
    private Transition transitionMgr;
    private SecondViewModel mViewModel;
    private TextView valueView, rateView;

    public static SecondFragment newInstance() {
        return new SecondFragment();
    }

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

        setHasOptionsMenu(true);

        View v = inflater.inflate(R.layout.root_container_fragment, container, false);

        transitionMgr = TransitionInflater.from(getActivity()).inflateTransition(R.transition.transition);
        rootContainer = (ViewGroup) v;
        scene2 = Scene.getSceneForLayout(rootContainer, R.layout.scene2, getActivity());
        scene1 = Scene.getSceneForLayout(rootContainer, R.layout.scene1, getActivity());
        scene1.enter();

        return v;
    }

    @RequiresApi(api = Build.VERSION_CODES.Q)
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        mViewModel = new ViewModelProvider(this).get(SecondViewModel.class);
        mViewModel.getValueObject().observe(getViewLifecycleOwner(), valueObserver);
        valueView = rootContainer.findViewById(R.id.valueView);

    }

 [....]

[....]

[.....]

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.one:
                layoutPrepare("Dollar");
                return true;
            case R.id.two:
                layoutPrepare("Euro");
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private void layoutPrepare(String currency) {
        scene2.setEnterAction(new Runnable() {
            @Override
            public void run() {
                setHasOptionsMenu(false);
                rateView = rootContainer.findViewById(R.id.rateView);
                rateView.setText(String.valueOf(mViewModel.getRate(currency)));
                valueView = rootContainer.findViewById(R.id.valueView);
                valueView.setText(String.valueOf(mViewModel.conversion(currency)));

            }
        });

        TransitionManager.go(scene2, transitionMgr);
    }

public void exit(View v)
{
    scene2.exit();
}
}

场景1和场景2的xml:
场景1:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SecondFragment">

    <TextView
        android:id="@+id/valueView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="228dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

场景2:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SecondFragment">

    <TextView
        android:id="@+id/valueView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="464dp"
        android:text="test"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/rateView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="59dp"
        android:layout_marginTop="93dp"
        android:text="test"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="exit"
        android:text="Return"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.89" />

</androidx.constraintlayout.widget.ConstraintLayout>
xn1cxnb4

xn1cxnb41#

不确定这是否能解决你的问题,但根据tools:context
在定义哪个活动或片段类示例化了正在定义的布局时,tools:context起着主要作用。
两个xml都有工具:指向secondfragment的上下文。
因此,请尝试将布局更改为指向相应的片段。

相关问题