0

I'm creating an application in which I face error. I want to add OnclickListner on a button. this button is on a fragment class. and from this fragment class i want to move on another class. the code is below:

fragment class code(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 class 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" />

and the error is:

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

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Osama Billah
  • 101
  • 10

1 Answers1

0

you need to add getActivity() in the end of ModeFragment.this

like this > ModeFragment.this.getActivity()

or

simply calling -> getActivity()

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

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

NOT_A_PROGRAMMER
  • 1,794
  • 2
  • 20
  • 31