Trying to add selected image views by(OnItemClickListener), between fragments, on the second ImageView the app crashes










-1















I'm trying to add and show a multiple image views existing in a fragment to another in the same activity by OnItemClickListener on GridView.



I was able to add the first image view but when I add the second one the app crashes.
By the way, I'm adding the image views to a linear layout inside Horizontal Scroll View.



Here is the layout of the first fragment:






<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/content_fragment_id"
>

<GridView
android:id="@+id/items_id_gridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:columnWidth="100dp"
android:minHeight="40dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:numColumns="auto_fit"
android:horizontalSpacing="10.0dip"
android:verticalSpacing="10.0dip"
android:cacheColorHint="#00000000"
android:gravity="center_horizontal"
android:requiresFadingEdge="vertical"
/>

</RelativeLayout>





The layout of the second fragment:






<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageButton
android:id="@+id/play_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/play_36dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:contentDescription="@string/play_button" />


<HorizontalScrollView
android:id="@+id/sentenceBarScrollView"
android:layout_width="fill_parent"
android:layout_height="104dp"
android:layout_toLeftOf="@id/play_button"
android:layout_toStartOf="@id/play_button"
android:padding="1dp"
>
<LinearLayout
android:id="@+id/sentence_bar"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:padding="2dp"
android:orientation="horizontal"
>

</LinearLayout>


</HorizontalScrollView>

</RelativeLayout>





The layout of the main activity:






<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Home"

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

<android.support.v7.widget.CardView
android:id="@+id/sentence_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical">
<fragment
android:id="@+id/sentenceBarFragment"
android:name="ye.com.ebra.contentfragments.SentenceBarFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"

/>
</LinearLayout>
</android.support.v7.widget.CardView>

<RelativeLayout
android:id="@+id/content_Layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/sentence_layout"
android:padding="5dp"
>
<!-- put contents fragments here :) -->
</RelativeLayout>
</RelativeLayout>


</android.support.constraint.ConstraintLayout>





the class of the first fragment.



sendItemToSentenceBar connector;

//for retrieving the values of the image stored in database
private Item item;

static ArrayList<Integer> back;

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState)
super.onActivityCreated(savedInstanceState);

back=new ArrayList<>();
DBConnection dbConnection= new DBConnection(getActivity());
GridView gridView=getActivity().findViewById(R.id.items_id_gridView);

final ArrayList<Item> items2=new ArrayList<>(dbConnection.getAll(Cat_id));

ContentAdapter dataAdapter=new ContentAdapter(getActivity(),items2);
gridView.setAdapter(dataAdapter);

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener()

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)

item=items2.get(position);
if(item.getType().equals("Category"))

back.add(Cat_id);
changeFragment(item.getID());
AddNewDialogFragment.Cat_id=item.getID();

else
speak(item.getName());
connector.sendData(item);


);



and I'm using an interface to pass the data from OnItemClickListener to the other fragment by "connector.sendData(item);"



Here is the interface:



public interface sendItemToSentenceBar 
// send item to the sentence bar fragment :)
void sendData(Item item);



Then in the class of the other fragment
There is the method AddItem(Item item) that should add the image view to the second fragment, the first image view can be added be on the second one the app just crashes :



private ArrayList<Item> items;

private LinearLayout sentenceLayout;
private View itemView;

private ArrayList<View> itemsViews;
View view;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState)
View sentenceBarFragment=inflater.inflate(R.layout.activity_sentence_bar_fragment,container,false);
sentenceLayout = sentenceBarFragment.findViewById(R.id.sentence_bar);
itemView = inflater.inflate(R.layout.activity_item,sentenceLayout,false);


return sentenceBarFragment;


public void AddItem(Item item)

TextView name_id = itemView.findViewById(R.id.name_id);
name_id.setText(item.getName());

ImageView image_id = itemView.findViewById(R.id.image_id);
image_id.setImageBitmap(convertByteToBitmap(item.getImage()));
itemsViews.add(itemView);
sentenceLayout.addView(itemsViews.get(0));

items.add(item);



Finally the method AddItem(Item item) is used in the class of the main activity that contain both of the fragments:



@Override
public void sendData(Item item)
FragmentManager fragmentManager=getFragmentManager();
SentenceBarFragment s= (SentenceBarFragment) fragmentManager.findFragmentById(R.id.sentenceBarFragment);
s.AddItem(item);



Here the Logcat:



E/AndroidRuntime: FATAL EXCEPTION: main
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:3378)
at android.view.ViewGroup.addView(ViewGroup.java:3249)
at android.view.ViewGroup.addView(ViewGroup.java:3194)
at android.view.ViewGroup.addView(ViewGroup.java:3170)
at ye.com.ebra.contentfragments.SentenceBarFragment.AddItem(SentenceBarFragment.java:95)
at ye.com.ebra.contentfragments.Home.sendData(Home.java:129)
at ye.com.ebra.contentfragments.ContentFragment$2.onItemClick(ContentFragment.java:83)
at android.widget.AdapterView.performItemClick(AdapterView.java:298)
at android.widget.AbsListView.performItemClick(AbsListView.java:1086)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:2855)
at android.widget.AbsListView$1.run(AbsListView.java:3529)
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:4745)
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:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)


of the Logcat, I could identify those errors:



at ye.com.ebra.contentfragments.SentenceBarFragment.AddItem(SentenceBarFragment.java:95)


which is at the class of the second fragment, in the method AddItem(Item item)



sentenceLayout.addView(itemsViews.get(0));


.



at ye.com.ebra.contentfragments.Home.sendData(Home.java:129)


which is at the class of the main activity, in the used method
sendData(Item item)



s.AddItem(item);


.



at ye.com.ebra.contentfragments.ContentFragment$2.onItemClick(ContentFragment.java:83)


which is in the class of the first fragment, in OnItemClickListener:
connector.sendData(item);



Sorry for the long post, but I'm really stuck here










share|improve this question




























    -1















    I'm trying to add and show a multiple image views existing in a fragment to another in the same activity by OnItemClickListener on GridView.



    I was able to add the first image view but when I add the second one the app crashes.
    By the way, I'm adding the image views to a linear layout inside Horizontal Scroll View.



    Here is the layout of the first fragment:






    <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/content_fragment_id"
    >

    <GridView
    android:id="@+id/items_id_gridView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerInParent="true"
    android:columnWidth="100dp"
    android:minHeight="40dp"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    android:numColumns="auto_fit"
    android:horizontalSpacing="10.0dip"
    android:verticalSpacing="10.0dip"
    android:cacheColorHint="#00000000"
    android:gravity="center_horizontal"
    android:requiresFadingEdge="vertical"
    />

    </RelativeLayout>





    The layout of the second fragment:






    <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >
    <ImageButton
    android:id="@+id/play_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/play_36dp"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:contentDescription="@string/play_button" />


    <HorizontalScrollView
    android:id="@+id/sentenceBarScrollView"
    android:layout_width="fill_parent"
    android:layout_height="104dp"
    android:layout_toLeftOf="@id/play_button"
    android:layout_toStartOf="@id/play_button"
    android:padding="1dp"
    >
    <LinearLayout
    android:id="@+id/sentence_bar"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:padding="2dp"
    android:orientation="horizontal"
    >

    </LinearLayout>


    </HorizontalScrollView>

    </RelativeLayout>





    The layout of the main activity:






    <android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Home"

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

    <android.support.v7.widget.CardView
    android:id="@+id/sentence_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:orientation="vertical">
    <fragment
    android:id="@+id/sentenceBarFragment"
    android:name="ye.com.ebra.contentfragments.SentenceBarFragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    />
    </LinearLayout>
    </android.support.v7.widget.CardView>

    <RelativeLayout
    android:id="@+id/content_Layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/sentence_layout"
    android:padding="5dp"
    >
    <!-- put contents fragments here :) -->
    </RelativeLayout>
    </RelativeLayout>


    </android.support.constraint.ConstraintLayout>





    the class of the first fragment.



    sendItemToSentenceBar connector;

    //for retrieving the values of the image stored in database
    private Item item;

    static ArrayList<Integer> back;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState)
    super.onActivityCreated(savedInstanceState);

    back=new ArrayList<>();
    DBConnection dbConnection= new DBConnection(getActivity());
    GridView gridView=getActivity().findViewById(R.id.items_id_gridView);

    final ArrayList<Item> items2=new ArrayList<>(dbConnection.getAll(Cat_id));

    ContentAdapter dataAdapter=new ContentAdapter(getActivity(),items2);
    gridView.setAdapter(dataAdapter);

    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener()

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)

    item=items2.get(position);
    if(item.getType().equals("Category"))

    back.add(Cat_id);
    changeFragment(item.getID());
    AddNewDialogFragment.Cat_id=item.getID();

    else
    speak(item.getName());
    connector.sendData(item);


    );



    and I'm using an interface to pass the data from OnItemClickListener to the other fragment by "connector.sendData(item);"



    Here is the interface:



    public interface sendItemToSentenceBar 
    // send item to the sentence bar fragment :)
    void sendData(Item item);



    Then in the class of the other fragment
    There is the method AddItem(Item item) that should add the image view to the second fragment, the first image view can be added be on the second one the app just crashes :



    private ArrayList<Item> items;

    private LinearLayout sentenceLayout;
    private View itemView;

    private ArrayList<View> itemsViews;
    View view;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState)
    View sentenceBarFragment=inflater.inflate(R.layout.activity_sentence_bar_fragment,container,false);
    sentenceLayout = sentenceBarFragment.findViewById(R.id.sentence_bar);
    itemView = inflater.inflate(R.layout.activity_item,sentenceLayout,false);


    return sentenceBarFragment;


    public void AddItem(Item item)

    TextView name_id = itemView.findViewById(R.id.name_id);
    name_id.setText(item.getName());

    ImageView image_id = itemView.findViewById(R.id.image_id);
    image_id.setImageBitmap(convertByteToBitmap(item.getImage()));
    itemsViews.add(itemView);
    sentenceLayout.addView(itemsViews.get(0));

    items.add(item);



    Finally the method AddItem(Item item) is used in the class of the main activity that contain both of the fragments:



    @Override
    public void sendData(Item item)
    FragmentManager fragmentManager=getFragmentManager();
    SentenceBarFragment s= (SentenceBarFragment) fragmentManager.findFragmentById(R.id.sentenceBarFragment);
    s.AddItem(item);



    Here the Logcat:



    E/AndroidRuntime: FATAL EXCEPTION: main
    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:3378)
    at android.view.ViewGroup.addView(ViewGroup.java:3249)
    at android.view.ViewGroup.addView(ViewGroup.java:3194)
    at android.view.ViewGroup.addView(ViewGroup.java:3170)
    at ye.com.ebra.contentfragments.SentenceBarFragment.AddItem(SentenceBarFragment.java:95)
    at ye.com.ebra.contentfragments.Home.sendData(Home.java:129)
    at ye.com.ebra.contentfragments.ContentFragment$2.onItemClick(ContentFragment.java:83)
    at android.widget.AdapterView.performItemClick(AdapterView.java:298)
    at android.widget.AbsListView.performItemClick(AbsListView.java:1086)
    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2855)
    at android.widget.AbsListView$1.run(AbsListView.java:3529)
    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:4745)
    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:786)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    at dalvik.system.NativeStart.main(Native Method)


    of the Logcat, I could identify those errors:



    at ye.com.ebra.contentfragments.SentenceBarFragment.AddItem(SentenceBarFragment.java:95)


    which is at the class of the second fragment, in the method AddItem(Item item)



    sentenceLayout.addView(itemsViews.get(0));


    .



    at ye.com.ebra.contentfragments.Home.sendData(Home.java:129)


    which is at the class of the main activity, in the used method
    sendData(Item item)



    s.AddItem(item);


    .



    at ye.com.ebra.contentfragments.ContentFragment$2.onItemClick(ContentFragment.java:83)


    which is in the class of the first fragment, in OnItemClickListener:
    connector.sendData(item);



    Sorry for the long post, but I'm really stuck here










    share|improve this question


























      -1












      -1








      -1








      I'm trying to add and show a multiple image views existing in a fragment to another in the same activity by OnItemClickListener on GridView.



      I was able to add the first image view but when I add the second one the app crashes.
      By the way, I'm adding the image views to a linear layout inside Horizontal Scroll View.



      Here is the layout of the first fragment:






      <RelativeLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:id="@+id/content_fragment_id"
      >

      <GridView
      android:id="@+id/items_id_gridView"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_centerInParent="true"
      android:columnWidth="100dp"
      android:minHeight="40dp"
      android:paddingTop="5dp"
      android:paddingBottom="5dp"
      android:numColumns="auto_fit"
      android:horizontalSpacing="10.0dip"
      android:verticalSpacing="10.0dip"
      android:cacheColorHint="#00000000"
      android:gravity="center_horizontal"
      android:requiresFadingEdge="vertical"
      />

      </RelativeLayout>





      The layout of the second fragment:






      <RelativeLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal"
      >
      <ImageButton
      android:id="@+id/play_button"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:src="@drawable/play_36dp"
      android:layout_alignParentRight="true"
      android:layout_alignParentEnd="true"
      android:contentDescription="@string/play_button" />


      <HorizontalScrollView
      android:id="@+id/sentenceBarScrollView"
      android:layout_width="fill_parent"
      android:layout_height="104dp"
      android:layout_toLeftOf="@id/play_button"
      android:layout_toStartOf="@id/play_button"
      android:padding="1dp"
      >
      <LinearLayout
      android:id="@+id/sentence_bar"
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:padding="2dp"
      android:orientation="horizontal"
      >

      </LinearLayout>


      </HorizontalScrollView>

      </RelativeLayout>





      The layout of the main activity:






      <android.support.constraint.ConstraintLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context=".Home"

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

      <android.support.v7.widget.CardView
      android:id="@+id/sentence_layout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      >

      <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="100dp"
      android:orientation="vertical">
      <fragment
      android:id="@+id/sentenceBarFragment"
      android:name="ye.com.ebra.contentfragments.SentenceBarFragment"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"

      />
      </LinearLayout>
      </android.support.v7.widget.CardView>

      <RelativeLayout
      android:id="@+id/content_Layout"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_below="@+id/sentence_layout"
      android:padding="5dp"
      >
      <!-- put contents fragments here :) -->
      </RelativeLayout>
      </RelativeLayout>


      </android.support.constraint.ConstraintLayout>





      the class of the first fragment.



      sendItemToSentenceBar connector;

      //for retrieving the values of the image stored in database
      private Item item;

      static ArrayList<Integer> back;

      @Override
      public void onActivityCreated(@Nullable Bundle savedInstanceState)
      super.onActivityCreated(savedInstanceState);

      back=new ArrayList<>();
      DBConnection dbConnection= new DBConnection(getActivity());
      GridView gridView=getActivity().findViewById(R.id.items_id_gridView);

      final ArrayList<Item> items2=new ArrayList<>(dbConnection.getAll(Cat_id));

      ContentAdapter dataAdapter=new ContentAdapter(getActivity(),items2);
      gridView.setAdapter(dataAdapter);

      gridView.setOnItemClickListener(new AdapterView.OnItemClickListener()

      @Override
      public void onItemClick(AdapterView<?> parent, View view, int position, long id)

      item=items2.get(position);
      if(item.getType().equals("Category"))

      back.add(Cat_id);
      changeFragment(item.getID());
      AddNewDialogFragment.Cat_id=item.getID();

      else
      speak(item.getName());
      connector.sendData(item);


      );



      and I'm using an interface to pass the data from OnItemClickListener to the other fragment by "connector.sendData(item);"



      Here is the interface:



      public interface sendItemToSentenceBar 
      // send item to the sentence bar fragment :)
      void sendData(Item item);



      Then in the class of the other fragment
      There is the method AddItem(Item item) that should add the image view to the second fragment, the first image view can be added be on the second one the app just crashes :



      private ArrayList<Item> items;

      private LinearLayout sentenceLayout;
      private View itemView;

      private ArrayList<View> itemsViews;
      View view;

      @Nullable
      @Override
      public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState)
      View sentenceBarFragment=inflater.inflate(R.layout.activity_sentence_bar_fragment,container,false);
      sentenceLayout = sentenceBarFragment.findViewById(R.id.sentence_bar);
      itemView = inflater.inflate(R.layout.activity_item,sentenceLayout,false);


      return sentenceBarFragment;


      public void AddItem(Item item)

      TextView name_id = itemView.findViewById(R.id.name_id);
      name_id.setText(item.getName());

      ImageView image_id = itemView.findViewById(R.id.image_id);
      image_id.setImageBitmap(convertByteToBitmap(item.getImage()));
      itemsViews.add(itemView);
      sentenceLayout.addView(itemsViews.get(0));

      items.add(item);



      Finally the method AddItem(Item item) is used in the class of the main activity that contain both of the fragments:



      @Override
      public void sendData(Item item)
      FragmentManager fragmentManager=getFragmentManager();
      SentenceBarFragment s= (SentenceBarFragment) fragmentManager.findFragmentById(R.id.sentenceBarFragment);
      s.AddItem(item);



      Here the Logcat:



      E/AndroidRuntime: FATAL EXCEPTION: main
      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:3378)
      at android.view.ViewGroup.addView(ViewGroup.java:3249)
      at android.view.ViewGroup.addView(ViewGroup.java:3194)
      at android.view.ViewGroup.addView(ViewGroup.java:3170)
      at ye.com.ebra.contentfragments.SentenceBarFragment.AddItem(SentenceBarFragment.java:95)
      at ye.com.ebra.contentfragments.Home.sendData(Home.java:129)
      at ye.com.ebra.contentfragments.ContentFragment$2.onItemClick(ContentFragment.java:83)
      at android.widget.AdapterView.performItemClick(AdapterView.java:298)
      at android.widget.AbsListView.performItemClick(AbsListView.java:1086)
      at android.widget.AbsListView$PerformClick.run(AbsListView.java:2855)
      at android.widget.AbsListView$1.run(AbsListView.java:3529)
      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:4745)
      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:786)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
      at dalvik.system.NativeStart.main(Native Method)


      of the Logcat, I could identify those errors:



      at ye.com.ebra.contentfragments.SentenceBarFragment.AddItem(SentenceBarFragment.java:95)


      which is at the class of the second fragment, in the method AddItem(Item item)



      sentenceLayout.addView(itemsViews.get(0));


      .



      at ye.com.ebra.contentfragments.Home.sendData(Home.java:129)


      which is at the class of the main activity, in the used method
      sendData(Item item)



      s.AddItem(item);


      .



      at ye.com.ebra.contentfragments.ContentFragment$2.onItemClick(ContentFragment.java:83)


      which is in the class of the first fragment, in OnItemClickListener:
      connector.sendData(item);



      Sorry for the long post, but I'm really stuck here










      share|improve this question
















      I'm trying to add and show a multiple image views existing in a fragment to another in the same activity by OnItemClickListener on GridView.



      I was able to add the first image view but when I add the second one the app crashes.
      By the way, I'm adding the image views to a linear layout inside Horizontal Scroll View.



      Here is the layout of the first fragment:






      <RelativeLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:id="@+id/content_fragment_id"
      >

      <GridView
      android:id="@+id/items_id_gridView"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_centerInParent="true"
      android:columnWidth="100dp"
      android:minHeight="40dp"
      android:paddingTop="5dp"
      android:paddingBottom="5dp"
      android:numColumns="auto_fit"
      android:horizontalSpacing="10.0dip"
      android:verticalSpacing="10.0dip"
      android:cacheColorHint="#00000000"
      android:gravity="center_horizontal"
      android:requiresFadingEdge="vertical"
      />

      </RelativeLayout>





      The layout of the second fragment:






      <RelativeLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal"
      >
      <ImageButton
      android:id="@+id/play_button"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:src="@drawable/play_36dp"
      android:layout_alignParentRight="true"
      android:layout_alignParentEnd="true"
      android:contentDescription="@string/play_button" />


      <HorizontalScrollView
      android:id="@+id/sentenceBarScrollView"
      android:layout_width="fill_parent"
      android:layout_height="104dp"
      android:layout_toLeftOf="@id/play_button"
      android:layout_toStartOf="@id/play_button"
      android:padding="1dp"
      >
      <LinearLayout
      android:id="@+id/sentence_bar"
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:padding="2dp"
      android:orientation="horizontal"
      >

      </LinearLayout>


      </HorizontalScrollView>

      </RelativeLayout>





      The layout of the main activity:






      <android.support.constraint.ConstraintLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context=".Home"

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

      <android.support.v7.widget.CardView
      android:id="@+id/sentence_layout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      >

      <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="100dp"
      android:orientation="vertical">
      <fragment
      android:id="@+id/sentenceBarFragment"
      android:name="ye.com.ebra.contentfragments.SentenceBarFragment"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"

      />
      </LinearLayout>
      </android.support.v7.widget.CardView>

      <RelativeLayout
      android:id="@+id/content_Layout"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_below="@+id/sentence_layout"
      android:padding="5dp"
      >
      <!-- put contents fragments here :) -->
      </RelativeLayout>
      </RelativeLayout>


      </android.support.constraint.ConstraintLayout>





      the class of the first fragment.



      sendItemToSentenceBar connector;

      //for retrieving the values of the image stored in database
      private Item item;

      static ArrayList<Integer> back;

      @Override
      public void onActivityCreated(@Nullable Bundle savedInstanceState)
      super.onActivityCreated(savedInstanceState);

      back=new ArrayList<>();
      DBConnection dbConnection= new DBConnection(getActivity());
      GridView gridView=getActivity().findViewById(R.id.items_id_gridView);

      final ArrayList<Item> items2=new ArrayList<>(dbConnection.getAll(Cat_id));

      ContentAdapter dataAdapter=new ContentAdapter(getActivity(),items2);
      gridView.setAdapter(dataAdapter);

      gridView.setOnItemClickListener(new AdapterView.OnItemClickListener()

      @Override
      public void onItemClick(AdapterView<?> parent, View view, int position, long id)

      item=items2.get(position);
      if(item.getType().equals("Category"))

      back.add(Cat_id);
      changeFragment(item.getID());
      AddNewDialogFragment.Cat_id=item.getID();

      else
      speak(item.getName());
      connector.sendData(item);


      );



      and I'm using an interface to pass the data from OnItemClickListener to the other fragment by "connector.sendData(item);"



      Here is the interface:



      public interface sendItemToSentenceBar 
      // send item to the sentence bar fragment :)
      void sendData(Item item);



      Then in the class of the other fragment
      There is the method AddItem(Item item) that should add the image view to the second fragment, the first image view can be added be on the second one the app just crashes :



      private ArrayList<Item> items;

      private LinearLayout sentenceLayout;
      private View itemView;

      private ArrayList<View> itemsViews;
      View view;

      @Nullable
      @Override
      public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState)
      View sentenceBarFragment=inflater.inflate(R.layout.activity_sentence_bar_fragment,container,false);
      sentenceLayout = sentenceBarFragment.findViewById(R.id.sentence_bar);
      itemView = inflater.inflate(R.layout.activity_item,sentenceLayout,false);


      return sentenceBarFragment;


      public void AddItem(Item item)

      TextView name_id = itemView.findViewById(R.id.name_id);
      name_id.setText(item.getName());

      ImageView image_id = itemView.findViewById(R.id.image_id);
      image_id.setImageBitmap(convertByteToBitmap(item.getImage()));
      itemsViews.add(itemView);
      sentenceLayout.addView(itemsViews.get(0));

      items.add(item);



      Finally the method AddItem(Item item) is used in the class of the main activity that contain both of the fragments:



      @Override
      public void sendData(Item item)
      FragmentManager fragmentManager=getFragmentManager();
      SentenceBarFragment s= (SentenceBarFragment) fragmentManager.findFragmentById(R.id.sentenceBarFragment);
      s.AddItem(item);



      Here the Logcat:



      E/AndroidRuntime: FATAL EXCEPTION: main
      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:3378)
      at android.view.ViewGroup.addView(ViewGroup.java:3249)
      at android.view.ViewGroup.addView(ViewGroup.java:3194)
      at android.view.ViewGroup.addView(ViewGroup.java:3170)
      at ye.com.ebra.contentfragments.SentenceBarFragment.AddItem(SentenceBarFragment.java:95)
      at ye.com.ebra.contentfragments.Home.sendData(Home.java:129)
      at ye.com.ebra.contentfragments.ContentFragment$2.onItemClick(ContentFragment.java:83)
      at android.widget.AdapterView.performItemClick(AdapterView.java:298)
      at android.widget.AbsListView.performItemClick(AbsListView.java:1086)
      at android.widget.AbsListView$PerformClick.run(AbsListView.java:2855)
      at android.widget.AbsListView$1.run(AbsListView.java:3529)
      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:4745)
      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:786)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
      at dalvik.system.NativeStart.main(Native Method)


      of the Logcat, I could identify those errors:



      at ye.com.ebra.contentfragments.SentenceBarFragment.AddItem(SentenceBarFragment.java:95)


      which is at the class of the second fragment, in the method AddItem(Item item)



      sentenceLayout.addView(itemsViews.get(0));


      .



      at ye.com.ebra.contentfragments.Home.sendData(Home.java:129)


      which is at the class of the main activity, in the used method
      sendData(Item item)



      s.AddItem(item);


      .



      at ye.com.ebra.contentfragments.ContentFragment$2.onItemClick(ContentFragment.java:83)


      which is in the class of the first fragment, in OnItemClickListener:
      connector.sendData(item);



      Sorry for the long post, but I'm really stuck here






      <RelativeLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:id="@+id/content_fragment_id"
      >

      <GridView
      android:id="@+id/items_id_gridView"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_centerInParent="true"
      android:columnWidth="100dp"
      android:minHeight="40dp"
      android:paddingTop="5dp"
      android:paddingBottom="5dp"
      android:numColumns="auto_fit"
      android:horizontalSpacing="10.0dip"
      android:verticalSpacing="10.0dip"
      android:cacheColorHint="#00000000"
      android:gravity="center_horizontal"
      android:requiresFadingEdge="vertical"
      />

      </RelativeLayout>





      <RelativeLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:id="@+id/content_fragment_id"
      >

      <GridView
      android:id="@+id/items_id_gridView"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_centerInParent="true"
      android:columnWidth="100dp"
      android:minHeight="40dp"
      android:paddingTop="5dp"
      android:paddingBottom="5dp"
      android:numColumns="auto_fit"
      android:horizontalSpacing="10.0dip"
      android:verticalSpacing="10.0dip"
      android:cacheColorHint="#00000000"
      android:gravity="center_horizontal"
      android:requiresFadingEdge="vertical"
      />

      </RelativeLayout>





      <RelativeLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal"
      >
      <ImageButton
      android:id="@+id/play_button"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:src="@drawable/play_36dp"
      android:layout_alignParentRight="true"
      android:layout_alignParentEnd="true"
      android:contentDescription="@string/play_button" />


      <HorizontalScrollView
      android:id="@+id/sentenceBarScrollView"
      android:layout_width="fill_parent"
      android:layout_height="104dp"
      android:layout_toLeftOf="@id/play_button"
      android:layout_toStartOf="@id/play_button"
      android:padding="1dp"
      >
      <LinearLayout
      android:id="@+id/sentence_bar"
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:padding="2dp"
      android:orientation="horizontal"
      >

      </LinearLayout>


      </HorizontalScrollView>

      </RelativeLayout>





      <RelativeLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal"
      >
      <ImageButton
      android:id="@+id/play_button"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:src="@drawable/play_36dp"
      android:layout_alignParentRight="true"
      android:layout_alignParentEnd="true"
      android:contentDescription="@string/play_button" />


      <HorizontalScrollView
      android:id="@+id/sentenceBarScrollView"
      android:layout_width="fill_parent"
      android:layout_height="104dp"
      android:layout_toLeftOf="@id/play_button"
      android:layout_toStartOf="@id/play_button"
      android:padding="1dp"
      >
      <LinearLayout
      android:id="@+id/sentence_bar"
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:padding="2dp"
      android:orientation="horizontal"
      >

      </LinearLayout>


      </HorizontalScrollView>

      </RelativeLayout>





      <android.support.constraint.ConstraintLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context=".Home"

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

      <android.support.v7.widget.CardView
      android:id="@+id/sentence_layout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      >

      <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="100dp"
      android:orientation="vertical">
      <fragment
      android:id="@+id/sentenceBarFragment"
      android:name="ye.com.ebra.contentfragments.SentenceBarFragment"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"

      />
      </LinearLayout>
      </android.support.v7.widget.CardView>

      <RelativeLayout
      android:id="@+id/content_Layout"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_below="@+id/sentence_layout"
      android:padding="5dp"
      >
      <!-- put contents fragments here :) -->
      </RelativeLayout>
      </RelativeLayout>


      </android.support.constraint.ConstraintLayout>





      <android.support.constraint.ConstraintLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context=".Home"

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

      <android.support.v7.widget.CardView
      android:id="@+id/sentence_layout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      >

      <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="100dp"
      android:orientation="vertical">
      <fragment
      android:id="@+id/sentenceBarFragment"
      android:name="ye.com.ebra.contentfragments.SentenceBarFragment"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"

      />
      </LinearLayout>
      </android.support.v7.widget.CardView>

      <RelativeLayout
      android:id="@+id/content_Layout"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_below="@+id/sentence_layout"
      android:padding="5dp"
      >
      <!-- put contents fragments here :) -->
      </RelativeLayout>
      </RelativeLayout>


      </android.support.constraint.ConstraintLayout>






      android android-fragments






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 16:04







      Omar

















      asked Nov 14 '18 at 15:58









      OmarOmar

      14




      14






















          1 Answer
          1






          active

          oldest

          votes


















          0














          You appear to be repeatedly modifying and adding the same item_view object inflated in onCreateView(...) method. It strikes me that you need to inflate a new object each time you call "AddItem".



          There is also a possible issue with appending views to the itemViews list but repeatedly adding the first view in the list (ie. not the one you just added) .



          So SentenceBarFragment might look instead like this:



          private ArrayList<Item> items;

          private LinearLayout sentenceLayout;

          private ArrayList<View> itemsViews;

          @Nullable
          @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState)
          View sentenceBarFragment=inflater.inflate(R.layout.activity_sentence_bar_fragment,container,false);
          sentenceLayout = sentenceBarFragment.findViewById(R.id.sentence_bar);
          return sentenceBarFragment;


          public void AddItem(Item item)

          // inflate a new view to be added
          View itemView = LayoutInflater.from(getActivity()).inflate(R.layout.activity_item,sentenceLayout,false);

          TextView name_id = itemView.findViewById(R.id.name_id);
          name_id.setText(item.getName());

          ImageView image_id = itemView.findViewById(R.id.image_id);
          image_id.setImageBitmap(convertByteToBitmap(item.getImage()));

          // add the view you just inflated
          itemsViews.add(itemView);
          sentenceLayout.addView(itemView); // or addView(itemViews.get(itemViews.size()-1))
          items.add(item);






          share|improve this answer

























          • Your answer worked really well, the image does show in the second fragment and its text does get read by text to speech engine. It's just the text doesn't show and I have searched a lot about it, but nothing works. I'm using the same method that you corrected for me(addItem) to set the text for the image's TextView. Hope you could help me with this.

            – Omar
            Nov 24 '18 at 14:47











          • On further consideration, my suggestion would be to change your sentenceBar LinearLayout and its enclosing HorizontalScrollView with a horizontal RecyclerView and populate it using an adapter based on your items list. This a more standard approach than dynamically adding child views to XML-defined layouts.

            – AGDownie
            Nov 25 '18 at 0:22











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53304203%2ftrying-to-add-selected-image-views-byonitemclicklistener-between-fragments-o%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          You appear to be repeatedly modifying and adding the same item_view object inflated in onCreateView(...) method. It strikes me that you need to inflate a new object each time you call "AddItem".



          There is also a possible issue with appending views to the itemViews list but repeatedly adding the first view in the list (ie. not the one you just added) .



          So SentenceBarFragment might look instead like this:



          private ArrayList<Item> items;

          private LinearLayout sentenceLayout;

          private ArrayList<View> itemsViews;

          @Nullable
          @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState)
          View sentenceBarFragment=inflater.inflate(R.layout.activity_sentence_bar_fragment,container,false);
          sentenceLayout = sentenceBarFragment.findViewById(R.id.sentence_bar);
          return sentenceBarFragment;


          public void AddItem(Item item)

          // inflate a new view to be added
          View itemView = LayoutInflater.from(getActivity()).inflate(R.layout.activity_item,sentenceLayout,false);

          TextView name_id = itemView.findViewById(R.id.name_id);
          name_id.setText(item.getName());

          ImageView image_id = itemView.findViewById(R.id.image_id);
          image_id.setImageBitmap(convertByteToBitmap(item.getImage()));

          // add the view you just inflated
          itemsViews.add(itemView);
          sentenceLayout.addView(itemView); // or addView(itemViews.get(itemViews.size()-1))
          items.add(item);






          share|improve this answer

























          • Your answer worked really well, the image does show in the second fragment and its text does get read by text to speech engine. It's just the text doesn't show and I have searched a lot about it, but nothing works. I'm using the same method that you corrected for me(addItem) to set the text for the image's TextView. Hope you could help me with this.

            – Omar
            Nov 24 '18 at 14:47











          • On further consideration, my suggestion would be to change your sentenceBar LinearLayout and its enclosing HorizontalScrollView with a horizontal RecyclerView and populate it using an adapter based on your items list. This a more standard approach than dynamically adding child views to XML-defined layouts.

            – AGDownie
            Nov 25 '18 at 0:22
















          0














          You appear to be repeatedly modifying and adding the same item_view object inflated in onCreateView(...) method. It strikes me that you need to inflate a new object each time you call "AddItem".



          There is also a possible issue with appending views to the itemViews list but repeatedly adding the first view in the list (ie. not the one you just added) .



          So SentenceBarFragment might look instead like this:



          private ArrayList<Item> items;

          private LinearLayout sentenceLayout;

          private ArrayList<View> itemsViews;

          @Nullable
          @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState)
          View sentenceBarFragment=inflater.inflate(R.layout.activity_sentence_bar_fragment,container,false);
          sentenceLayout = sentenceBarFragment.findViewById(R.id.sentence_bar);
          return sentenceBarFragment;


          public void AddItem(Item item)

          // inflate a new view to be added
          View itemView = LayoutInflater.from(getActivity()).inflate(R.layout.activity_item,sentenceLayout,false);

          TextView name_id = itemView.findViewById(R.id.name_id);
          name_id.setText(item.getName());

          ImageView image_id = itemView.findViewById(R.id.image_id);
          image_id.setImageBitmap(convertByteToBitmap(item.getImage()));

          // add the view you just inflated
          itemsViews.add(itemView);
          sentenceLayout.addView(itemView); // or addView(itemViews.get(itemViews.size()-1))
          items.add(item);






          share|improve this answer

























          • Your answer worked really well, the image does show in the second fragment and its text does get read by text to speech engine. It's just the text doesn't show and I have searched a lot about it, but nothing works. I'm using the same method that you corrected for me(addItem) to set the text for the image's TextView. Hope you could help me with this.

            – Omar
            Nov 24 '18 at 14:47











          • On further consideration, my suggestion would be to change your sentenceBar LinearLayout and its enclosing HorizontalScrollView with a horizontal RecyclerView and populate it using an adapter based on your items list. This a more standard approach than dynamically adding child views to XML-defined layouts.

            – AGDownie
            Nov 25 '18 at 0:22














          0












          0








          0







          You appear to be repeatedly modifying and adding the same item_view object inflated in onCreateView(...) method. It strikes me that you need to inflate a new object each time you call "AddItem".



          There is also a possible issue with appending views to the itemViews list but repeatedly adding the first view in the list (ie. not the one you just added) .



          So SentenceBarFragment might look instead like this:



          private ArrayList<Item> items;

          private LinearLayout sentenceLayout;

          private ArrayList<View> itemsViews;

          @Nullable
          @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState)
          View sentenceBarFragment=inflater.inflate(R.layout.activity_sentence_bar_fragment,container,false);
          sentenceLayout = sentenceBarFragment.findViewById(R.id.sentence_bar);
          return sentenceBarFragment;


          public void AddItem(Item item)

          // inflate a new view to be added
          View itemView = LayoutInflater.from(getActivity()).inflate(R.layout.activity_item,sentenceLayout,false);

          TextView name_id = itemView.findViewById(R.id.name_id);
          name_id.setText(item.getName());

          ImageView image_id = itemView.findViewById(R.id.image_id);
          image_id.setImageBitmap(convertByteToBitmap(item.getImage()));

          // add the view you just inflated
          itemsViews.add(itemView);
          sentenceLayout.addView(itemView); // or addView(itemViews.get(itemViews.size()-1))
          items.add(item);






          share|improve this answer















          You appear to be repeatedly modifying and adding the same item_view object inflated in onCreateView(...) method. It strikes me that you need to inflate a new object each time you call "AddItem".



          There is also a possible issue with appending views to the itemViews list but repeatedly adding the first view in the list (ie. not the one you just added) .



          So SentenceBarFragment might look instead like this:



          private ArrayList<Item> items;

          private LinearLayout sentenceLayout;

          private ArrayList<View> itemsViews;

          @Nullable
          @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState)
          View sentenceBarFragment=inflater.inflate(R.layout.activity_sentence_bar_fragment,container,false);
          sentenceLayout = sentenceBarFragment.findViewById(R.id.sentence_bar);
          return sentenceBarFragment;


          public void AddItem(Item item)

          // inflate a new view to be added
          View itemView = LayoutInflater.from(getActivity()).inflate(R.layout.activity_item,sentenceLayout,false);

          TextView name_id = itemView.findViewById(R.id.name_id);
          name_id.setText(item.getName());

          ImageView image_id = itemView.findViewById(R.id.image_id);
          image_id.setImageBitmap(convertByteToBitmap(item.getImage()));

          // add the view you just inflated
          itemsViews.add(itemView);
          sentenceLayout.addView(itemView); // or addView(itemViews.get(itemViews.size()-1))
          items.add(item);







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 14 '18 at 17:05

























          answered Nov 14 '18 at 16:59









          AGDownieAGDownie

          1965




          1965












          • Your answer worked really well, the image does show in the second fragment and its text does get read by text to speech engine. It's just the text doesn't show and I have searched a lot about it, but nothing works. I'm using the same method that you corrected for me(addItem) to set the text for the image's TextView. Hope you could help me with this.

            – Omar
            Nov 24 '18 at 14:47











          • On further consideration, my suggestion would be to change your sentenceBar LinearLayout and its enclosing HorizontalScrollView with a horizontal RecyclerView and populate it using an adapter based on your items list. This a more standard approach than dynamically adding child views to XML-defined layouts.

            – AGDownie
            Nov 25 '18 at 0:22


















          • Your answer worked really well, the image does show in the second fragment and its text does get read by text to speech engine. It's just the text doesn't show and I have searched a lot about it, but nothing works. I'm using the same method that you corrected for me(addItem) to set the text for the image's TextView. Hope you could help me with this.

            – Omar
            Nov 24 '18 at 14:47











          • On further consideration, my suggestion would be to change your sentenceBar LinearLayout and its enclosing HorizontalScrollView with a horizontal RecyclerView and populate it using an adapter based on your items list. This a more standard approach than dynamically adding child views to XML-defined layouts.

            – AGDownie
            Nov 25 '18 at 0:22

















          Your answer worked really well, the image does show in the second fragment and its text does get read by text to speech engine. It's just the text doesn't show and I have searched a lot about it, but nothing works. I'm using the same method that you corrected for me(addItem) to set the text for the image's TextView. Hope you could help me with this.

          – Omar
          Nov 24 '18 at 14:47





          Your answer worked really well, the image does show in the second fragment and its text does get read by text to speech engine. It's just the text doesn't show and I have searched a lot about it, but nothing works. I'm using the same method that you corrected for me(addItem) to set the text for the image's TextView. Hope you could help me with this.

          – Omar
          Nov 24 '18 at 14:47













          On further consideration, my suggestion would be to change your sentenceBar LinearLayout and its enclosing HorizontalScrollView with a horizontal RecyclerView and populate it using an adapter based on your items list. This a more standard approach than dynamically adding child views to XML-defined layouts.

          – AGDownie
          Nov 25 '18 at 0:22






          On further consideration, my suggestion would be to change your sentenceBar LinearLayout and its enclosing HorizontalScrollView with a horizontal RecyclerView and populate it using an adapter based on your items list. This a more standard approach than dynamically adding child views to XML-defined layouts.

          – AGDownie
          Nov 25 '18 at 0:22




















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53304203%2ftrying-to-add-selected-image-views-byonitemclicklistener-between-fragments-o%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          政党

          天津地下鉄3号線