【发布时间】: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