clockbyte / admobadapter

It wraps your Adapter to display Admob native ads and banners in a ListView/RecyclerView data set. It based on the Yahoo fetchr project https://github.com/yahoo/fetchr
Apache License 2.0
236 stars 75 forks source link

The specified child already has a parent. You must call removeView() on the child's parent first. #77

Closed Sarun1001 closed 7 years ago

Sarun1001 commented 7 years ago

Log---- 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:4449) at android.view.ViewGroup.addView(ViewGroup.java:4285) at android.view.ViewGroup.addView(ViewGroup.java:4226) at android.view.ViewGroup.addView(ViewGroup.java:4199) at com.clockbyte.admobadapter.expressads.AdmobExpressAdapterWrapper.getView(AdmobExpressAdapterWrapper.java:335) at android.widget.AbsListView.obtainView(AbsListView.java:2474) at android.widget.ListView.measureHeightOfChildren(ListView.java:1301) at android.widget.ListView.onMeasure(ListView.java:1203) at android.view.View.measure(View.java:19169) at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:825) at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:511) at android.view.View.measure(View.java:19169) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6124) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1723) at android.widget.LinearLayout.measureVertical(LinearLayout.java:788) at android.widget.LinearLayout.onMeasure(LinearLayout.java:648) at android.view.View.measure(View.java:19169) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6124) at android.widget.FrameLayout.onMeasure(FrameLayout.java:223) at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139) at android.view.View.measure(View.java:19169) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6124) at android.widget.FrameLayout.onMeasure(FrameLayout.java:223) at android.view.View.measure(View.java:19169) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6124) at android.widget.FrameLayout.onMeasure(FrameLayout.java:223) at android.view.View.measure(View.java:19169) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6124) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1723) at android.widget.LinearLayout.measureVertical(LinearLayout.java:788) at android.widget.LinearLayout.onMeasure(LinearLayout.java:648) at android.view.View.measure(View.java:19169) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6124) at android.widget.FrameLayout.onMeasure(FrameLayout.java:223) at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2747) at android.view.View.measure(View.java:19169) at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2485) at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1444) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1698) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1325) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6741) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:912) at android.view.Choreographer.doCallbacks(Choreographer.java:714) at android.view.Choreographer.doFrame(Choreographer.java:649) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:898) at android.os.Handler.handleCallback(Handler.java:815) at android.os.Handler.dispatchMessage(Handler.java:104) at android.os.Looper.loop(Looper.java:207) at android.app.ActivityThread.main(ActivityThread.java:5769) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679) 05-11 14:41:10.292 604-612/com.globalaxiomlabs.malayalamtrolls I/System: FinalizerDaemon: finalize objects = 1582

Activity Code---------------

public class CommentsActivity extends AppCompatActivity {

ArrayList<Comment> comments;
ArrayAdapter<Comment> commentsAdapter;
int type;
String id;
AdmobExpressAdapterWrapper adapterWrapper;

private Toolbar mToolbar;

public static String DATA_PARSEABLE = "parseable";
public static String DATA_TYPE = "type";
public static String DATA_ID = "id";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_comments);
    mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
    setSupportActionBar(mToolbar);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    setTitle(getResources().getString(R.string.comments));

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); getWindow().setStatusBarColor((Color.parseColor("#1565C0")));//FF1744 }

    MobileAds.initialize(this, getString(R.string.admob_app_id));
    Bundle extras = getIntent().getExtras();
    String parseableString = extras.getString(DATA_PARSEABLE);
    type = extras.getInt(DATA_TYPE);
    id = extras.getString(DATA_ID);

    ListView lComments = (ListView) findViewById(R.id.listView);
    lComments.setEmptyView(findViewById(R.id.empty));
    comments = new ArrayList<>();
    commentsAdapter = new CommentsAdapter(this, comments, type);
    adapterWrapper = new AdmobExpressAdapterWrapper(this, getString(R.string.nativead_comment),new AdSize(AdSize.FULL_WIDTH, 150));
    adapterWrapper.setAdapter(commentsAdapter);
    adapterWrapper.setLimitOfAds(3);
    adapterWrapper.setNoOfDataBetweenAds(12);
    adapterWrapper.setFirstAdIndex(4);
    lComments.setAdapter(commentsAdapter);
    lComments.setAdapter(adapterWrapper);
    commentsAdapter.addAll(comments);
    commentsAdapter.notifyDataSetChanged();
    fetchComments(parseableString);
}

Adapter--------------

public class CommentsAdapter extends ArrayAdapter {

private Context context;
private int type;

public CommentsAdapter(Context context, List<Comment> objects, int type) {
    super(context, 0, objects);
    this.context = context;
    this.type = type;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final Comment comment = getItem(position);
    CommentViewHolder viewHolder;

    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.activity_comments_row, parent, false);
        viewHolder = new CommentViewHolder();
        viewHolder.ivProfilePhoto = (ImageView) convertView.findViewById(R.id.ivProfilePhoto);
        viewHolder.tvUsername = (TextView)convertView.findViewById(R.id.tvUsername);
        viewHolder.tvComment = (TextView) convertView.findViewById(R.id.tvComment);
       // viewHolder.Commentimage = (ImageView) convertView.findViewById(R.id.commentimage);
        convertView.setTag(viewHolder);
    }
    else {
        viewHolder = (CommentViewHolder)convertView.getTag();
    }

    viewHolder.ivProfilePhoto.setImageDrawable(null);
   // viewHolder.Commentimage.setImageDrawable(null);

    if (comment.profileUrl != null){
        viewHolder.ivProfilePhoto.setVisibility(View.VISIBLE);

        Picasso.with(context).load(comment.profileUrl).into(viewHolder.ivProfilePhoto);
    } else {
        viewHolder.ivProfilePhoto.setVisibility(View.GONE);
    }

    viewHolder.tvUsername.setText(comment.username);

    viewHolder.tvComment.setText(Html.fromHtml(comment.text.replaceAll("<img.+?>", "")));
    return convertView;
}

public class CommentViewHolder {
    ImageView ivProfilePhoto;
    TextView tvUsername;
    TextView tvComment;

}

activity_comments xml-------

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >

<include
    android:id="@+id/toolbar_actionbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    layout="@layout/toolbar_default" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView
        android:id="@+id/listView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:clipToPadding="false"
        />

    <TextView
        android:id="@+id/empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/listView"
        android:layout_centerInParent="true"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:text="@string/no_results" />
</RelativeLayout>

activity_comments_row ----------------

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:background="@color/white" android:layout_height="match_parent">

<LinearLayout 
android:id="@+id/lineView"
android:orientation="horizontal" 
android:layout_width="wrap_content"
android:layout_height="match_parent">

</LinearLayout>

<RelativeLayout 
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginBottom="20dp">
    <ImageView
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/ivProfilePhoto"
        android:layout_width="50dp"
        android:layout_height="50dp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:id="@+id/tvUsername"
        android:layout_alignTop="@+id/ivProfilePhoto"
        android:layout_toRightOf="@+id/ivProfilePhoto"
        android:layout_toEndOf="@+id/ivProfilePhoto"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="5dp"
        android:maxLines="1"
        android:gravity="start|center_vertical"
        android:textSize="14sp"
        android:textColor="#41739C" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tvComment"
        android:layout_toRightOf="@+id/ivProfilePhoto"
        android:layout_toEndOf="@+id/ivProfilePhoto"
        android:layout_below="@+id/tvUsername"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:gravity="start|center_vertical"
        android:textSize="14sp" />
    </RelativeLayout>

It crashes sometimes when opening comments activity, but works sometimes,

Crash frequency reduced when 'adapterWrapper.setFirstAdIndex(4);' set to an even number

Any idea ??

What actually makes it difficult for me is that crash is not pointing to any specific line of code. I need some expert advice Thank you

kot331107 commented 7 years ago

@Sarun1001 what version of lib or commit do you use?

Sarun1001 commented 7 years ago

@kot331107 - Version 1.4.1

kot331107 commented 7 years ago

I'll check it and return to you later

Sarun1001 commented 7 years ago

Thank you

Sarun1001 commented 7 years ago

hello, any idea guys

HaydenCampbell commented 7 years ago

I am having this same problem except mine crashes every time. The app opens, list loads with ads loading and then crashes.

HaydenCampbell commented 7 years ago

I fixed mine by changing the listview width and height from wrap_content to match_parent

<ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" />

kot331107 commented 7 years ago

@HaydenCampbell could you please reproduce that crash once more and post your LogCat here? thx in advance

HaydenCampbell commented 7 years ago

Sure can, just changed listview width and height back to wrap_content and it causes the crash:

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.package, PID: 2313 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:4659) at android.view.ViewGroup.addView(ViewGroup.java:4500) at android.view.ViewGroup.addView(ViewGroup.java:4440) at android.view.ViewGroup.addView(ViewGroup.java:4413) at com.clockbyte.admobadapter.expressads.AdmobExpressAdapterWrapper.getView(AdmobExpressAdapterWrapper.java:335) at android.widget.AbsListView.obtainView(AbsListView.java:3251) at android.widget.ListView.measureHeightOfChildren(ListView.java:1396) at android.widget.ListView.onMeasure(ListView.java:1303) at android.view.View.measure(View.java:21121) at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715) at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461) at android.view.View.measure(View.java:21121) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6462) at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:714) at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:90) at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1391) at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:784) at android.view.View.measure(View.java:21121) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6462) at android.widget.FrameLayout.onMeasure(FrameLayout.java:185) at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139) at android.view.View.measure(View.java:21121) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6462) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464) at android.widget.LinearLayout.measureVertical(LinearLayout.java:758) at android.widget.LinearLayout.onMeasure(LinearLayout.java:640) at android.view.View.measure(View.java:21121) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6462) at android.widget.FrameLayout.onMeasure(FrameLayout.java:185) at android.view.View.measure(View.java:21121) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6462) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464) at android.widget.LinearLayout.measureVertical(LinearLayout.java:758) at android.widget.LinearLayout.onMeasure(LinearLayout.java:640) at android.view.View.measure(View.java:21121) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6462) at android.widget.FrameLayout.onMeasure(FrameLayout.java:185) at com.android.internal.policy.DecorView.onMeasure(DecorView.java:897) at android.view.View.measure(View.java:21121) at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2625) at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1677) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1928) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1550) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7189) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:959) at android.view.Choreographer.doCallbacks(Choreographer.java:734) at android.view.Choreographer.doFrame(Choreographer.java:670) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:945) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6776) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

kot331107 commented 7 years ago

fixed