【问题标题】:unable to instantiate activity ComponentInfo(Fragment) - Android无法实例化活动 ComponentInfo(Fragment) - Android
【发布时间】:2020-01-11 13:45:49
【问题描述】:

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

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

【问题讨论】:

  • 不要尝试将 Fragment 作为 Activity 打开,因为它是 Fragment 而不是 Activity

标签: android


【解决方案1】:

【讨论】:

  • Start activity 顾名思义是用来启动activity而不是fragment 希望有帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-11-19
  • 2014-11-22
  • 1970-01-01
  • 1970-01-01
  • 2011-06-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多