Reputation: 329
I am trying to make a Dialog with a custom listview inside. But I am getting this error: "The specified child already has a parent. You must call removeView() on the child's parent first".
ActivityCode:
public class ConfigActivity extends ActionBarActivity {
//... onCreate and blablabla
public void onClickUsoDados(View view) {
final ArrayList<CustomItem> data = init();//init() just get some data
CustomListAdapter adapter = new CustomListAdapter(this, data);
View v = ConfigActivity.this.getLayoutInflater().inflate(R.layout.layout_config_userdata_listview, null, true);
list = (ListView) v.findViewById(R.id.listView1);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String Slecteditem = data.get(position).nome;
Toast.makeText(ConfigActivity.this, Slecteditem, Toast.LENGTH_SHORT).show();
}
});
final Dialog dialogOut = new Dialog(ConfigActivity.this);
dialogOut.setContentView(list);
dialogOut.setCancelable(true);
dialogOut.setTitle("Dados");
dialogOut.buNeutralButton("Voltar",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialogOut.dismiss();
}
});
dialogOut.show();
}
}
CustomItem.java
public class CustomItem {
public String usuario;
public String nome;
public String curso;
}
CustomListAdapter.java
public class CustomListAdapter extends ArrayAdapter<CustomItem> {
private Activity context;
private ArrayList<CustomItem> data;
public CustomListAdapter(Activity context, ArrayList<CustomItem> item) {
super(context, R.layout.layout_config_userdata_row, item);
this.context = context;
this.data = item;
}
public View getView(int position, View convertView, ViewGroup parent) {
CustomItem user = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.layout_config_userdata_row, parent, true);
}
TextView uUsername = (TextView) convertView.findViewById(R.id.textViewListViewUsuario);
TextView uName = (TextView) convertView.findViewById(R.id.textViewListViewNome);
TextView uCurso = (TextView) convertView.findViewById(R.id.textViewListViewCurso);
uUsername.setText(user.usuario);
uName.setText(user.nome);
uCurso.setText(user.curso);
return convertView;
}
}
I am getting this error on "dialogOut.show();", or, if I use the builder, I get this error on "dialog.create()". Thoughts?
EDIT1: The xml files...
layout_config_userdata_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout //stuff>
<LinearLayout <!--stuff-->>
<LinearLayout <!--stuff-->>
<LinearLayout <!--stuff-->>
<TextView <!--stuff-->/>
<TextView
android:id="@+id/textViewListViewUsuario"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="userToLogin"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</LinearLayout>
<LinearLayout <!--stuff-->>
<LinearLayout <!--stuff-->>
<TextView <!--stuff-->/>
<TextView
android:id="@+id/textViewListViewNome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="nomeUser"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</LinearLayout>
<LinearLayout <!--stuff-->>
<LinearLayout <!--stuff-->>
<TextView <!--stuff-->/>
<TextView
android:id="@+id/textViewListViewCurso"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="cursoNome"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<FrameLayout <!--stuff-->>
<ImageView <!--stuff-->/>
</FrameLayout>
</LinearLayout>
And the layout_config_userdata_listview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
EDIT2: The stack trace.
09-29 12:50:24.073 19813-19813/com.ztesteconfig E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3680)
at android.view.View.performClick(View.java:4191)
at android.view.View$PerformClick.run(View.java:17229)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4960)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View$1.onClick(View.java:3675)
at android.view.View.performClick(View.java:4191)
at android.view.View$PerformClick.run(View.java:17229)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4960)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:3439)
at android.view.ViewGroup.addView(ViewGroup.java:3310)
at android.view.ViewGroup.addView(ViewGroup.java:3286)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:337)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:327)
at android.app.Dialog.setContentView(Dialog.java:480)
at com.ztesteconfig.ConfigActivity.onClickUsoDados(ConfigActivity.java:334)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View$1.onClick(View.java:3675)
at android.view.View.performClick(View.java:4191)
at android.view.View$PerformClick.run(View.java:17229)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4960)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
at com.ztesteconfig.ConfigActivity.onClickUsoDados(ConfigActivity.java:334) it is the exactly "dialogOut.show();"
Upvotes: 1
Views: 192
Reputation: 69
Try creating the list view programatically..
use
list = new ListView(getContext());
instead of
list = (ListView) v.findViewById(R.id.listView1);
OR
edit your xml as
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/base_view">
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
(added an id to LinearLayout)
and in your activity code
LinearLayout base=v.findViewById(R.id.base_view);
and set this view as content of dialog
dialogOut.setContentView(base);
Upvotes: 0
Reputation: 1007494
OK, so, let's review:
Your error message is "the specified view already has a parent"
Your layout has a LinearLayout
that you are not using
Your Java code is specifically ignoring that LinearLayout
So, get rid of the LinearLayout
from the layout resource. You are not using it. You do not need it. And, it is the source of your error, as the ListView
cannot both be in the LinearLayout
and in the content view of the dialog.
Alternatively, if you have a long-term vision in which you will need that LinearLayout
, put the LinearLayout
as the content view of the dialog, not the ListView
inside the LinearLayout
.
Upvotes: 1
Reputation:
Try this
if(view.getParent()!=null)
((ViewGroup)view.getParent()).removeView(v);
Upvotes: 0