android - 无法实例化活动 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 {
}
解决方案
推荐阅读
- regex - 使用 Ansible 正则表达式在文件中搜索字符串
- xaml - 如何设置 xamarin 表单输入字段的禁用文本颜色?
- sql - 获取每天的最小值和最大值之间的差异
- scala - 在 Spark 程序中访问 Oozie 配置
- python - 解析跳过元素
- php - PHP中不同语言的序数指标
- netsuite - NetSuite 公司信息页面(新手问题)
- python - 如何在 Pandas 中检索已删除 / Coulmns Rows 记录
- ubuntu - 使用 NGINX 作为反向代理为同一服务器上的网络服务器提供服务
- ios - Carthage Build 失败,使用 Xcode 11.3 的 Cuckoo 库