首页 > 解决方案 > 无法实例化活动 ComponentInfo(Fragment) - Android

问题描述

我想单击按钮并打开另一个片段,但出现以下问题。我需要做什么才能切换到新片段?

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

第一个片段

  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);
            }
        });
}

第二个片段

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;
    }

视图模型

package ...yeniKayit;

import androidx.lifecycle.ViewModel;

public class yeniKayıtModel extends ViewModel {
}

标签: android

解决方案



推荐阅读