我再次发现,我发现我的应用程序出现了很多麻烦,有人预料到,我还没有在Android上编程 我不熟悉视图,活动,上下文以及所有这些东西,今天,我在片段上创建AlertDialog时出现问题,我使用Fragment 因为我使用的是DrawerLayout。
错误说:
"致命的例外:主要 java.lang.IllegalStateException:指定的子级已有父级。您必须先在孩子的父母身上调用removeView()。"
这是我的代码
我宣布了一个ViewGroup" context"变量来设置rootview值(OnCreateView方法)
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final ViewGroup root = (ViewGroup) inflater.inflate(R.layout.ventas_fragment, null);
context = root;
我尝试创建AlertDialog的方法是:
private void createDialog(int position){
LayoutInflater inflater = LayoutInflater.from(context.getContext());
View dialog_layout = inflater.inflate(R.layout.ventas_dialog,(ViewGroup) context.findViewById(R.id.ventaFragment));
AlertDialog.Builder db = new AlertDialog.Builder(getActivity());
db.setView(dialog_layout);
db.create();
ImageView imageView = (ImageView)dialog_layout.findViewById(R.id.imageDialog);
switch (position){
case 0:
db.setTitle("Venta de Ponques - Cantidad");
imageView.setImageResource(R.drawable.ic_lanzador);
db.setPositiveButton(R.string.aceptar, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(context.getContext(),"Prueba superada",Toast.LENGTH_LONG).show();
}
});
db.show();
break;
}
}
我打电话给:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
.....
gridView.setChoiceMode(GridView.CHOICE_MODE_SINGLE);
gridView.setOnItemClickListener(new GridView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
createDialog(position);
}
});
在线上检测到错误
db.show();
我已尝试在LayoutInflater.from()和AlerDialog.Builder()上更改这些上下文参数但它没有工作, 也许这个问题并不复杂,但我真的没有看到它,我不理解这个错误,你们中的任何人都可以 伙计们伸出援手帮我解决这个问题?
事先非常感谢你。