1

This kind of question has been asked before, but I still can't get the hang of it so I ask again anyway

I got a hang for several seconds (well not just several, it's about 10 seconds) when trying to add a new tab fragment to my activity which serves as a host for all other fragments. I have added several fragments without encountering any problem, and there's this one fragment that gives me the mentioned problem.

Every time when I create a new tab and adding this fragment, the stack trace produces this following result,

11-19 20:54:30.403: E/Dynamiclayout(13299): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
11-19 20:54:30.403: E/Dynamiclayout(13299): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
11-19 20:54:30.403: E/Dynamiclayout(13299): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
11-19 20:54:30.403: E/Dynamiclayout(13299): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
11-19 20:54:30.411: E/Dynamiclayout(13299): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
11-19 20:54:30.411: E/Dynamiclayout(13299): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
11-19 20:54:30.411: E/Dynamiclayout(13299): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
11-19 20:54:30.411: E/Dynamiclayout(13299): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
11-19 20:54:30.411: E/Dynamiclayout(13299): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
11-19 20:54:30.411: E/Dynamiclayout(13299): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
11-19 20:54:30.411: E/Dynamiclayout(13299): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
11-19 20:54:30.411: E/Dynamiclayout(13299): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
11-19 20:54:30.411: E/Dynamiclayout(13299): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
11-19 20:54:30.411: E/Dynamiclayout(13299): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
11-19 20:54:30.411: E/Dynamiclayout(13299): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
11-19 20:54:30.411: E/Dynamiclayout(13299): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
11-19 20:54:30.419: E/Dynamiclayout(13299): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
11-19 20:54:30.419: E/Dynamiclayout(13299): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
11-19 20:54:30.419: E/Dynamiclayout(13299): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
11-19 20:54:30.419: E/Dynamiclayout(13299): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
11-19 20:54:30.419: E/Dynamiclayout(13299): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0

This is the layout of the fragment which I wanted to add :

<RelativeLayout
                android:id="@+id/description"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_toRightOf="@+id/menu"
                android:background="#FFFFFF" >
            <ScrollView 
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:fillViewport="true"
                >
            <RelativeLayout 
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_marginBottom="30dp">
                <LinearLayout 
                    android:id="@+id/simulate_desc"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >

                    <RelativeLayout 
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content" >
                        <RelativeLayout 
                            android:id="@+id/active_task"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:paddingLeft="@dimen/padding_description"
                            android:layout_marginTop="30dp" >
                            <TextView 
                                android:id="@+id/desc_title"
                                android:layout_width="fill_parent"
                                android:layout_height="wrap_content"
                                android:text="Location Incident"
                                android:textScaleX="0.8"
                                android:textSize="@dimen/desc_title"
                                android:textColor="#f47a5c"
                                android:textStyle="bold" /> 
                        </RelativeLayout>

                        <ImageView
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content" 
                            android:layout_below="@+id/active_task"
                            android:src="@drawable/stroke"
                            android:layout_marginRight="20dp"
                            android:layout_marginLeft="5dp"
                            />

                        <RelativeLayout
                            android:id="@+id/Address"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_below="@+id/active_task"
                            android:padding="@dimen/padding_description" >

                            <RelativeLayout
                                android:layout_width="fill_parent"
                                android:layout_marginRight="10dp"
                                android:layout_height="wrap_content" >

                                <TextView 
                                    android:id="@+id/address_incident_label"
                                    android:text="Address :"
                                    android:textSize="@dimen/desc_title"
                                    android:layout_height="wrap_content"
                                    android:layout_width="wrap_content"
                                    android:textScaleX="0.7"
                                    android:layout_marginLeft="2dp"
                                    android:textColor="#000"
                                    />
                                <EditText 
                                    android:layout_marginTop="5dp"
                                    android:id="@+id/address_incident"
                                    android:hint="address"
                                    android:paddingLeft="10dp"
                                    android:layout_height="40dp"
                                    android:layout_width="fill_parent"
                                    android:layout_below="@+id/address_incident_label"
                                    />


                            </RelativeLayout>
                        </RelativeLayout>



                        <RelativeLayout 
                            android:id="@+id/Incident"
                            android:layout_below="@+id/Address"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginRight="10dp"
                            android:padding="@dimen/padding_description"
                             >
                            <RelativeLayout
                                android:layout_width="fill_parent"
                                android:layout_height="wrap_content" >

                                <TextView 
                                    android:id="@+id/incident_report"
                                    android:text="Incident Report "
                                    android:textSize="@dimen/desc_title"
                                    android:layout_height="wrap_content"
                                    android:layout_width="wrap_content"
                                    android:textScaleX="0.7"
                                    android:layout_marginLeft="2dp"
                                    android:textColor="#000"
                                    />

                                <TextView
                                    android:id="@+id/incident_report2"
                                    android:text="Input the details of data from incident"
                                    android:textSize="12sp"
                                    android:textScaleX="0.8"
                                    android:layout_height="wrap_content"
                                    android:layout_width="wrap_content"
                                    android:layout_below="@+id/incident_report"
                                    android:layout_marginLeft="2dp"
                                    />
                                <!-- incident detail -->
                                <LinearLayout 
                                    android:layout_marginTop="20dp"
                                    android:layout_below="@+id/incident_report2"
                                    android:id="@+id/incident_linear_detail1"
                                    android:layout_width="fill_parent"
                                    android:layout_height="wrap_content"
                                    android:layout_marginRight="20dp"
                                    android:orientation="horizontal"
                                    >
                                    <TextView 
                                        android:layout_marginLeft="20dp"
                                        android:text="Date"
                                        android:textScaleX="0.8"
                                        android:layout_height="wrap_content"
                                        android:layout_width="wrap_content"
                                        android:textSize="20sp"
                                        android:layout_marginRight="48dp"
                                        android:textColor="#000"
                                        />

                                    <EditText
                                        android:id="@+id/incident_date"
                                        android:layout_height="40dp"
                                        android:layout_width="40dp"
                                        android:singleLine="true"
                                        />
                                    <EditText 
                                        android:id="@+id/incident_month"
                                        android:layout_width="40dp"
                                        android:layout_height="40dp"
                                        android:singleLine="true"
                                        />
                                    <EditText 
                                        android:id="@+id/incident_year"
                                        android:layout_width="80dp"
                                        android:layout_height="40dp"
                                        android:singleLine="true"
                                        />
                                    <TextView 
                                        android:id="@+id/label_time"
                                        android:layout_height="wrap_content"
                                        android:layout_width="wrap_content"
                                        android:textSize="20sp"
                                        android:scaleX="0.7"
                                        android:text="Time"
                                        />
                                    <EditText 
                                        android:id="@+id/incident_time"
                                        android:layout_height="40dp"
                                        android:layout_width="fill_parent"
                                        android:singleLine="true"
                                        android:editable="false"
                                        />

                                </LinearLayout>

                                <!-- 2 -->
                                <LinearLayout 
                                    android:layout_marginTop="5dp"
                                    android:layout_below="@+id/incident_linear_detail1"
                                    android:id="@+id/incident_linear_detail2"
                                    android:layout_width="fill_parent"
                                    android:layout_height="wrap_content"
                                    android:layout_marginRight="20dp"
                                    android:orientation="horizontal"
                                    >

                                    <TextView 
                                        android:layout_marginLeft="20dp"
                                        android:text="Location"
                                        android:textScaleX="0.8"
                                        android:layout_height="wrap_content"
                                        android:layout_width="wrap_content"
                                        android:textSize="20sp"
                                        android:layout_marginRight="20dp"
                                        android:textColor="#000"
                                        />
                                    <EditText
                                        android:id="@+id/incident_location"
                                        android:layout_height="40dp"
                                        android:layout_width="fill_parent"
                                        android:hint="Incident Location"
                                        android:paddingLeft="10dp"
                                        android:singleLine="true"
                                        />

                                </LinearLayout>

                               <!-- 3 --> 

                                <LinearLayout 
                                    android:layout_marginTop="5dp"
                                    android:layout_below="@+id/incident_linear_detail2"
                                    android:id="@+id/incident_linear_detail3"
                                    android:layout_width="fill_parent"
                                    android:layout_height="wrap_content"
                                    android:layout_marginRight="20dp"
                                    android:orientation="horizontal"
                                    >

                                    <TextView 
                                        android:layout_marginLeft="20dp"
                                        android:text="Speed"
                                        android:textScaleX="0.8"
                                        android:layout_height="wrap_content"
                                        android:layout_width="wrap_content"
                                        android:textSize="20sp"
                                        android:layout_marginRight="20dp"
                                        android:textColor="#000"
                                        />
                                    <EditText
                                        android:id="@+id/incident_speed"
                                        android:layout_height="40dp"
                                        android:layout_width="fill_parent"
                                        android:layout_marginLeft="17dp"
                                        android:hint="Speed of your car"
                                        android:singleLine="true"
                                        />

                                </LinearLayout>





                            </RelativeLayout>
                        </RelativeLayout>


                        <RelativeLayout 
                            android:id="@+id/Describe"
                            android:layout_below="@+id/Incident"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:padding="@dimen/padding_description" >
                            <RelativeLayout
                                android:layout_width="fill_parent"
                                android:layout_height="wrap_content" 
                                android:layout_marginRight="20dp"
                                >

                                <TextView 
                                    android:id="@+id/incident_description_label"
                                    android:text="Describe the Incident"
                                    android:textSize="@dimen/desc_title"
                                    android:layout_height="wrap_content"
                                    android:layout_width="wrap_content"
                                    android:textScaleX="0.7"
                                    android:layout_marginLeft="2dp"
                                    android:textColor="#000"
                                    />
                                <EditText 
                                    android:layout_below="@+id/incident_description_label"
                                    android:id="@+id/incident_description"
                                    android:layout_height="40dp"
                                    android:layout_width="fill_parent"
                                    android:hint="Description"
                                    />


                            </RelativeLayout>    
                        </RelativeLayout>



                        <RelativeLayout 
                            android:id="@+id/Upload"
                            android:layout_below="@+id/Describe"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            >
                            <RelativeLayout
                                android:layout_width="fill_parent"
                                android:layout_height="wrap_content"
                                android:layout_marginRight="20dp"
                                android:paddingLeft="10dp"
                                 >

                                 <TextView 

                                    android:id="@+id/upload_car_label"
                                    android:text="Upload Car Picture"
                                    android:textSize="@dimen/desc_title"
                                    android:layout_height="wrap_content"
                                    android:layout_width="wrap_content"
                                    android:textScaleX="0.7"
                                    android:layout_marginTop="5dp"
                                    android:layout_marginLeft="2dp"
                                    android:textColor="#000"
                                    />

                                 <TextView
                                    android:layout_below="@+id/upload_car_label"
                                    android:id="@+id/upload_car_label2"
                                    android:text="Input the details of data from incident"
                                    android:textSize="12sp"
                                    android:textScaleX="0.8"
                                    android:layout_height="wrap_content"
                                    android:layout_width="wrap_content"
                                    android:layout_marginLeft="2dp"

                                    />

                                 <ImageView
                                     android:id="@+id/incident_photo_car"
                                     android:layout_height="80dp"
                                     android:layout_width="80dp"
                                     android:src="@drawable/upload"
                                     android:layout_alignParentRight="true"
                                     />

                            </RelativeLayout>    
                        </RelativeLayout>


                        <RelativeLayout 
                            android:id="@+id/Foto"
                            android:layout_below="@+id/Upload"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                             >
                            <RelativeLayout
                                android:layout_width="fill_parent"
                                android:layout_height="wrap_content"
                                android:layout_marginRight="20dp"
                                android:paddingLeft="10dp"
                                 >

                               <TextView 
                                    android:id="@+id/license_label"
                                    android:text="License Photo"
                                    android:textSize="@dimen/desc_title"
                                    android:layout_height="wrap_content"
                                    android:layout_width="wrap_content"
                                    android:textScaleX="0.7"
                                    android:layout_marginLeft="2dp"
                                    android:textColor="#000"
                                    />

                                 <TextView
                                    android:layout_below="@+id/license_label"
                                    android:id="@+id/license_label2"
                                    android:text="Input the details of data from incident"
                                    android:textSize="12sp"
                                    android:textScaleX="0.8"
                                    android:layout_height="wrap_content"
                                    android:layout_width="wrap_content"
                                    android:layout_marginLeft="2dp"
                                    />

                                 <ImageView
                                     android:id="@+id/incident_photo_license"
                                     android:layout_height="80dp"
                                     android:layout_width="80dp"
                                     android:src="@drawable/upload"
                                     android:layout_alignParentRight="true"
                                     /> 


                            </RelativeLayout>    
                        </RelativeLayout>


                        <RelativeLayout 
                            android:id="@+id/Tampak"
                            android:layout_below="@+id/Foto"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                             >
                            <RelativeLayout
                                android:layout_width="fill_parent"
                                android:layout_height="wrap_content"
                                android:layout_marginRight="20dp"
                                android:paddingLeft="10dp"
                                 >

                               <TextView 
                                    android:id="@+id/tampak_label"
                                    android:text="Front/Back View"
                                    android:textSize="@dimen/desc_title"
                                    android:layout_height="wrap_content"
                                    android:layout_width="wrap_content"
                                    android:textScaleX="0.7"
                                    android:layout_marginLeft="2dp"
                                    android:textColor="#000"
                                    />

                                 <TextView
                                    android:layout_below="@+id/tampak_label"
                                    android:id="@+id/tampak_label2"
                                    android:text="Foto yang diambil dari tampak depan dan belakang"
                                    android:textSize="12sp"
                                    android:textScaleX="0.8"
                                    android:layout_height="wrap_content"
                                    android:layout_width="wrap_content"
                                    android:layout_marginLeft="2dp"
                                    />

                                 <ImageView
                                     android:id="@+id/incident_photo_frontback"
                                     android:layout_height="80dp"
                                     android:layout_width="80dp"
                                     android:src="@drawable/upload"
                                     android:layout_alignParentRight="true"
                                     /> 


                            </RelativeLayout>    
                        </RelativeLayout>

                    </RelativeLayout>
                </LinearLayout>

                <Button 
                    android:id="@+id/btn_next_claim"
                    android:layout_alignParentRight="true"
                    android:layout_below="@+id/simulate_desc"
                    android:layout_width="180dp"
                    android:layout_height="50dp"
                    android:text="next"
                    android:background="@drawable/btn_red"
                    android:textSize="@dimen/text_profile_size"
                    android:textColor="#FFFFFF"
                    android:layout_marginTop="20dp"
                    android:layout_marginRight="10dp"
                    android:onClick="nextClaim"
                    />


                </RelativeLayout>
                </ScrollView>
            </RelativeLayout>

The fragment does not perform a task at all. I simply just inflate it on my onCreateView method on the fragment class. But as you can see it contains a heavy UI (has so many views and child views)

Just wondering if there's a better method adding a fragment without sacrificing the UI responsiveness. Thank you for taking your time reading the question

Karate_Dog
  • 1,265
  • 5
  • 20
  • 37
  • You should really try to reduce the current view hierarchy. The fragment's layout that you posted is 8 levels deep and more levels will be added due to the fragments framework + activity layout which it is bad. – user Nov 19 '13 at 08:34
  • Hmm yeah, I've been thinking about reducing the views and separating it to a new fragment to lighten the UI load per fragment. But just wondering if I did some mistake in my code that makes it load so much longer than usual.. – Karate_Dog Nov 19 '13 at 09:13

1 Answers1

1

Solution: first of all you develop fragment activity class in which you create a function and add some fragments. After fragment manager you can change fragments.

private List<Fragment> getFragments() {
        List<Fragment> fList = new ArrayList<Fragment>();

        // TODO Put here your Fragments
        Category_fragments f3 =new Category_fragments();
        Popular_fragments2 f2 = new Popular_fragments2();
        Random_fragments f4 =  new Random_fragments();
        Recent_fragments2 f1 =  new Recent_fragments2(); 
        fList.add(f1);
        fList.add(f2);
        fList.add(f3);
        fList.add(f4);
        return fList;
    }