gpt4 book ai didi

java - fragment 点击

转载 作者:行者123 更新时间:2023-12-01 18:17:22 28 4
gpt4 key购买 nike

我正在创建一个应用程序,但在其中遇到错误。我想在按钮上添加 OnclickListner。该按钮位于 fragment 类上。从这个 fragment 类我想继续另一个类。代码如下:

fragment 类代码(JAVA)

public class ModeFragment extends Fragment implements TitledFragment {

private ModeViewModel viewModel;


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

try {
viewModel = BackAwareApplication.getContainer().getInstance(ModeViewModel.class);
} catch (Exception e) {
e.printStackTrace();
}


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

Button btn_monitoring = (Button) v.findViewById(R.id.btn_belt_monitoring);

btn_monitoring.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(ModeFragment.this , BluetoothActivity.class);
startActivity(intent);
}
});

fragment 类 XML:

 <Button
android:id="@+id/btn_belt_monitoring"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Belt Monitoring"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.96" />

错误是:

error: no suitable constructor found for Intent(ModeFragment,Class<BluetoothActivity>)
constructor Intent.Intent(String,Uri) is not applicable
(argument mismatch; ModeFragment cannot be converted to String)
constructor Intent.Intent(Context,Class<?>) is not applicable
(argument mismatch; ModeFragment cannot be converted to Context)

I read this link but did't solve the problem

最佳答案

您需要在ModeFragment.this末尾添加getActivity()

像这样> ModeFragment.this.getActivity()

只需调用 -> getActivity()

Intent intent = new Intent(ModeFragment.this.getActivity(), BluetoothActivity.class); 
ModeFragment.this.getActivity().startActivity(intent);

https://developer.android.com/guide/components/intents-filters

关于java - fragment 点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60342913/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com