gpt4 book ai didi

android - 在 android TabFragment 上添加 fragment 会导致挂起

转载 作者:行者123 更新时间:2023-11-30 03:16:14 25 4
gpt4 key购买 nike

这种问题以前有人问过,但还是搞不明白所以再问一次

当我尝试向我的 Activity 添加一个新的选项卡 fragment 作为所有其他 fragment 的宿主时,我挂了几秒钟(好吧,不只是几秒钟,大约 10 秒)。我添加了几个 fragment 而没有遇到任何问题,这个 fragment 给了我提到的问题。

每次我创建一个新选项卡并添加此 fragment 时,堆栈跟踪都会产生以下结果,

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

这是我想添加的 fragment 的布局:

<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>

该 fragment 根本不执行任何任务。我只是在 fragment 类的 onCreateView 方法上对其进行膨胀。但是正如您所看到的,它包含一个沉重的 UI(有很多 View 和 subview )

只是想知道是否有更好的方法在不牺牲 UI 响应能力的情况下添加 fragment 。感谢您花时间阅读问题

最佳答案

解决方案:首先,您开发 fragment Activity 类,在其中创建一个函数并添加一些 fragment 。在 fragment 管理器之后,您可以更改 fragment 。

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;
}

关于android - 在 android TabFragment 上添加 fragment 会导致挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20066465/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com