-1

I want to click the button and open another fragment, but I get the following problem. What do I need to do to switch to a new fragment?

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{...yeniKayıtFragment}: java.lang.ClassCastException: ...yeniKayıtFragment cannot be cast to android.app.Activity

Firs Fragment

  public class GirisFragmentNew extends Fragment implements View.OnClickListener{

 btnKAyit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i=new Intent(getActivity(), yeniKayıtFragment.class);
                startActivity(i);
            }
        });
}

Second Fragment

public class yeniKayıtFragment extends  Fragment {
    private yeniKayıtModel yeniKayıtModel;
   public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        yeniKayıtModel =
                ViewModelProviders.of(this).get(yeniKayıtModel.class);

        View root = inflater.inflate(R.layout.fragment_yenikayit, container, false);
        Window window=getActivity().getWindow();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.setNavigationBarColor(getResources().getColor(R.color.colorPrimary));}
  return root;
    }

ViewModel

package ...yeniKayit;

import androidx.lifecycle.ViewModel;

public class yeniKayıtModel extends ViewModel {
}
stacksoru
  • 23
  • 4

1 Answers1

0

Use fragment manager instead of intent for more info you can check this link

https://www.google.com/url?sa=t&source=web&rct=j&url=https://stackoverflow.com/questions/32700818/how-to-open-a-fragment-on-button-click-from-a-fragment-in-android&ved=2ahUKEwiEt8iQ2fvmAhU0olwKHduzAeQQFjAAegQIAhAB&usg=AOvVaw1_58tL3yoNjYApzG4cB1cj&cshid=1578750857220

Clinton
  • 21
  • 4