[Android JAVA] How to float a new window of Activity in Fragment

Asked 2 years ago, Updated 2 years ago, 89 views

I want to open a new activity window in Fragment on Android Java Pressing the button will shut down the app at the same time as the error.

f0Binding.ibBk.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(getContext(), Bk.class)); } }

package com.example.up;

import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment;

import android.content.Intent; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button;

import com.example.up.databinding.ActivityFrag0Binding; import com.example.up.databinding.ActivityMainBinding;

public class Frag0 extends Fragment {

private ActivityFrag0Binding f0Binding; // fragment binding
private View view;
Private Activity Main Binding; // Main
private Frag0 frag0;
private Frag1 frag1;
private Frag2 frag2;
private Button ib_bk;



@Nullable
@Override
Public View on CreateView (@NonNullLayoutInflator, @NullableViewGroupContainer, @NullableBundleSavedInstanceState) { //Fragment starts with onCreateView #25 14:16
    f0Binding = ActivityFrag0Binding.inflate(inflater,container,false);
    view = inflater.inflate(R.layout.activity_frag0,container,false);


    f0Binding.ibBk.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(getContext(), Bk.class));


          Intent intent = new Intent(getActivity(), bk.class);
            startActivity(intent);


        }

    });

    return view;
}

}

java fragment activity android

2022-09-20 17:29

1 Answers

I think I do layout inflate twice in the fragment.

f0Binding = ActivityFrag0Binding.inflate(inflater,container,false);
//view = inflater.inflate(R.layout.activity_frag0,container,false);

return f0Binding.getRoot();

Two ibBk buttons were created, inflated in data binding and inflated directly in view variables, but the onCreateView method returned the view, but it seems to be an error because there is no designated findById.

Please also attach an error message next time.


2022-09-20 17:29

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.