无法通过方法调用转换将实际参数android.app.FragmentManager转换为FragmentTransaction
2016-06-26 11:27
351 查看
今天在做一个简单的DialogFragment作为底部弹出框的例子的时候发生了一个bug
发现出现问题的位置很有可能是调用这个DialogFragment的Activity部分
public void showBottomDialog(View view) {
FragmentManager fm= getSupportFragmentManager();
BottomDialogFragment editNameDialog = new BottomDialogFragment();
editNameDialog.show(fm,"fragment_bottom_dialog");
}
此时的editNameDialog.show 方法下是有红色的线的,说明此处我的方法是有问题的。
再回来看问题本身,此处的问题 中文描述的已经非常明确了 就是类型转换错误,android.support.v4.app.FragmentManager 和 android.app.FragmentManager无法互相转换,于是我们就要找到问题出现的实际原因了,为什么我用getSupportFragmentManager 获取的v4包的FragmentManager无法给editNameDialog使用呢?
于是我进入editNameDialog进行查看
OK 看到我的继承的父类了吗?没错,此处继承的是android.app.DialogFragment,而不是android.support.v4.app.DialogFragment,所已问题的根源其实是出在Fragment这里了,将此处改为android.support.v4.app.DialogFragment 就可以和 主Activity中方法匹配上了,也就不会报类型转换错误了。
当然 这里顺便就说一下为什么不用android.app.Fragment相关的引用呢?
因为这个app下的fragment支持版本和support.v4.app下fragment支持的版本是不一样的。
android.app.Fragment 兼容的最低版本是android:minSdkVersion="11" 即3.0版
android.support.v4.app.Fragment 兼容的最低版本是android:minSdkVersion="4" 即1.6版
只是这个原因,相信大家也应该知道为什么不用app.Fragment了吧?
发现出现问题的位置很有可能是调用这个DialogFragment的Activity部分
public void showBottomDialog(View view) {
FragmentManager fm= getSupportFragmentManager();
BottomDialogFragment editNameDialog = new BottomDialogFragment();
editNameDialog.show(fm,"fragment_bottom_dialog");
}
此时的editNameDialog.show 方法下是有红色的线的,说明此处我的方法是有问题的。
再回来看问题本身,此处的问题 中文描述的已经非常明确了 就是类型转换错误,android.support.v4.app.FragmentManager 和 android.app.FragmentManager无法互相转换,于是我们就要找到问题出现的实际原因了,为什么我用getSupportFragmentManager 获取的v4包的FragmentManager无法给editNameDialog使用呢?
于是我进入editNameDialog进行查看
package com.example.jasoncool.test; import android.app.Dialog; import android.app.DialogFragment; import android.os.Bundle; import android.support.annotation.NonNull; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.view.WindowManager; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import butterknife.BindView; import butterknife.ButterKnife; /** * Created by jasoncool on 2016/6/26. */ public class BottomDialogFragment extends DialogFragment { @BindView(R.id.regards_tv_100_coins) TextView regardsTv100Coins; @BindView(R.id.regards_iv_100_coins) ImageView regardsIv100Coins; @BindView(R.id.regards_ll_first_container) LinearLayout regardsLlFirstContainer; @BindView(R.id.regards_tv_2_yuan) TextView regardsTv2Yuan; @BindView(R.id.regards_iv_2_yuan) ImageView regardsIv2Yuan; @BindView(R.id.regards_ll_second_container) LinearLayout regardsLlSecondContainer; @BindView(R.id.regards_tv_8_yuan) TextView regardsTv8Yuan; @BindView(R.id.regards_iv_8_yuan) ImageView regardsIv8Yuan; @BindView(R.id.regards_ll_third_container) LinearLayout regardsLlThirdContainer; @BindView(R.id.regards_tv_12_yuan) TextView regardsTv12Yuan; @BindView(R.id.regards_iv_12_yuan) ImageView regardsIv12Yuan; @BindView(R.id.regards_ll_forth_container) LinearLayout regardsLlForthContainer; @BindView(R.id.regards_tv_coin_count) TextView regardsTvCoinCount; @BindView(R.id.regards_tv_send) TextView regardsTvSend; @NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) { Dialog dialog = new Dialog(getActivity(), R.style.BottomDialog); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog.setContentView(R.layout.fragment_bottom); dialog.setCanceledOnTouchOutside(true); Window window = dialog.getWindow(); WindowManager.LayoutParams lp = window.getAttributes(); lp.gravity = Gravity.BOTTOM; lp.width = WindowManager.LayoutParams.MATCH_PARENT; window.setAttributes(lp); ButterKnife.bind(this, dialog); return dialog; } }
OK 看到我的继承的父类了吗?没错,此处继承的是android.app.DialogFragment,而不是android.support.v4.app.DialogFragment,所已问题的根源其实是出在Fragment这里了,将此处改为android.support.v4.app.DialogFragment 就可以和 主Activity中方法匹配上了,也就不会报类型转换错误了。
当然 这里顺便就说一下为什么不用android.app.Fragment相关的引用呢?
因为这个app下的fragment支持版本和support.v4.app下fragment支持的版本是不一样的。
android.app.Fragment 兼容的最低版本是android:minSdkVersion="11" 即3.0版
android.support.v4.app.Fragment 兼容的最低版本是android:minSdkVersion="4" 即1.6版
只是这个原因,相信大家也应该知道为什么不用app.Fragment了吧?
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories