我是Java/Android开发新手(我昨晚开始学习),所以完全有可能我正在做一些可怕的愚蠢的事情。然而,经过一个多小时的谷歌搜索,我什么都没有得到。我使用Eclipse作为我的编辑器。AlertDialog示例给出的错误
我读的文档here为AlertDialog
,这给出了一个例子:
public static class MyAlertDialogFragment extends DialogFragment {
public static MyAlertDialogFragment newInstance(int title) {
MyAlertDialogFragment frag = new MyAlertDialogFragment();
Bundle args = new Bundle();
args.putInt("title", title);
frag.setArguments(args);
return frag;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
int title = getArguments().getInt("title");
return new AlertDialog.Builder(getActivity())
.setIcon(R.drawable.alert_dialog_icon)
.setTitle(title)
.setPositiveButton(R.string.alert_dialog_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
((FragmentAlertDialog)getActivity()).doPositiveClick();
}
}
)
.setNegativeButton(R.string.alert_dialog_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
((FragmentAlertDialog)getActivity()).doNegativeClick();
}
}
)
.create();
}
}
本来我重新写,所以我可以开始犯一些方法来记忆,却得到一个错误“ FragmentAlertDialog无法解析为“”类型。我打Ctrl + Shift + O确保我有适当的进口,但它仍然没有消失。
所以我复制/粘贴示例代码,并做了以下内容,顺序如下:
- 命中按Ctrl + 移 + Ø得到进口权(使用
android.app.DialogFragment
,不android.support.v4.app.DialogFragment
) - 顶部
- 与
android.R.string.ok
的替换R.string.alert_dialog_ok
和R.string.alert_dialog_cancel
宣布我的包dandroid.R.string.cancel
分别为 - 删除
setIcon()
,因为我没有一个图标放置在尚未
我仍然得到错误:
FragmentAlertDialog
不能被解析为一个类型(X4)- 该级别的非法修饰符
MyAlertDialogFragment
;只有public
,abstract
&final
允许
我做得不对,或者是有什么错示例代码?
这是否是单独的类或内部类FragmentAlertDialog的? – 2013-03-02 17:15:41
你能用你的解决方案更新吗?我也卡在这里... – Lion789 2014-02-10 23:23:13