无法实例化活动组件信息(片段) - 安卓



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

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

使用片段管理器而不是意图以获取更多信息,您可以查看此链接

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

最新更新