以前曾经问过这样的问题,但是我仍然无法理解它,所以我再问一遍
我尝试向我的活动中添加一个新的标签片段作为所有其他片段的主机时,挂了几秒(好吧不只是几个,大约10秒)。我添加了几个片段而没有遇到任何问题,而且有一个片段给我提到了上述问题。
每次创建新选项卡并添加此片段时,堆栈跟踪都会生成以下结果,
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
这是我要添加的片段的布局:
<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>
片段根本不会执行任务。我只是在片段类上的 onCreateView 方法上对其进行充气。但是你可以看到它包含一个沉重的UI(有很多视图和子视图)
只是想知道是否有更好的方法添加片段而不牺牲UI响应性。感谢您花时间阅读问题
答案 0 :(得分:1)
<强>解决方案:强> 首先,您要开发片段活动类,在其中创建一个函数并添加一些片段。在片段管理器之后,您可以更改片段。
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;
}