Android中DialogFragment中的ClassCastException
我不熟悉Android中的Android中DialogFragment中的ClassCastException,android,android-fragments,android-dialogfragment,Android,Android Fragments,Android Dialogfragment,我不熟悉Android中的片段。刚刚试着了解一下DialogFragment。但是上面写着classcastException public class FragmentDialog extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(
片段。刚刚试着了解一下DialogFragment
。但是上面写着classcastException
public class FragmentDialog extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_dialog);
}
void showDialog() {
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment prev = getFragmentManager().findFragmentByTag("dialog");
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);
DialogFragment newFragment = MyDialogFragment.newInstance(0);
newFragment.show(getFragmentManager(), "dialog");
}
public static class MyDialogFragment extends DialogFragment {
static MyDialogFragment newInstance(int num) {
MyDialogFragment f = new MyDialogFragment();
return f;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int style = DialogFragment.STYLE_NORMAL, theme = 0;
setStyle(style, theme);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_dialog, container,
false);
Button button = (Button) v.findViewById(R.id.show);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
((FragmentDialog)getActivity()).showDialog(); // Error is in this line.
}
});
return v;
}
}
}
LogCat错误为:
07-12 15:22:25.241: E/AndroidRuntime(6419): java.lang.ClassCastException: com.example.fragmentexample.FragmentTabs cannot be cast to com.example.fragmentexample.FragmentDialog
07-12 15:22:25.241: E/AndroidRuntime(6419): at com.example.fragmentexample.FragmentDialog$MyDialogFragment$1.onClick(FragmentDialog.java:74)
编辑1#
此碎片对话框
是碎片选项卡
的一个选项卡
public class FragmentTabs extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final ActionBar bar = getActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
...
...
bar.addTab(bar.newTab()
.setText("Dialog")
.setTabListener(new TabListener<FragmentDialog.MyDialogFragment>(
this, "Dialog", FragmentDialog.MyDialogFragment.class)));
...
...
}
公共类碎片选项卡扩展活动{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
最终ActionBar=getActionBar();
设置导航模式(操作栏导航模式选项卡);
设置显示选项(0,ActionBar.DISPLAY\u SHOW\u TITLE);
...
...
bar.addTab(bar.newTab()
.setText(“对话框”)
.setTabListener(新的TabListener(
这是“Dialog”,FragmentDialog.MyDialogFragment.class);
...
...
}
这就是为什么((FragmentDialog)getActivity()).showDialog();
此行返回com.example.fragmentexample.FragmentTabs不能强制转换为com.example.fragmentexample.FragmentDialog
。如何将活动转换为MyDialogFragment您在遵循构造函数时遇到问题
static MyDialogFragment newInstance(int num)
{
MyDialogFragment f = new MyDialogFragment();
return f;
}
应该是这样,
static MyDialogFragment newInstance(int num)
{
MyDialogFragment f = new DialogFragment(); // Change is here.
return f;
}
您想在片段中单击按钮时显示对话框。我的代码当前正在使用
自定义对话框:
public class CustomDialog extends DialogFragment {
static CustomDialog newInstance() {
return new CustomDialog();
}
private ProgressDialog mProgressDialog;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
mProgressDialog = new ProgressDialog(getActivity());
mProgressDialog.setView(new View(getActivity()));
mProgressDialog.getWindow().setLayout(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
mProgressDialog.setCancelable(true);
mProgressDialog.setTitle(getResources().getString(R.string.title));
mProgressDialog.setMessage(getResources().getString(R.string.message));
mProgressDialog
.setProgressStyle(android.R.style.Theme_DeviceDefault_Light);
mProgressDialog.getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.TRANSPARENT));
return mProgressDialog;
}
}
片段:
public class MainFragment extends Fragment implements OnClickListener {
Button mButton;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_layout, container, false);
mButton = (Button) v.findViewById(R.id.button1);
mButton.setOnClickListener(MainFragment.this);
return v;
}
void showDialog() {
CustomDialog dialog= CustomDialog.newInstance();
dialog.show(getFragmentManager(), "dialog");
}
@Override
public void onClick(View v) {
showDialog();
}
}
否则,您可以删除import@Vigbyor:您确定存在名为FragmentDialog的biltin类吗?或者错放了DialogFragment
Opps,我想你是对的,内置的是DialogFragment
,我错了。@Gunaseelan,请再试一次。不。它只能是MyDialogFragment
。如果我更改名称,它会显示类型不匹配:无法像这样从DialogFragment转换为MyFragmentDialog.MyDialogFragment
。指出引发异常的行。@andranikazbekyan((FragmentDialog)getActivity())中的错误。showDialog()代码>。查看编辑是否还有其他活动片段选项卡?@Nizam是。。碎片选项卡实现操作栏
。这里的一个选项卡包含片段对话框
。没有朋友,我不需要进度对话框,我希望自定义对话框片段,因为我将执行一些操作。