开发者

Android application reloading with corrupted UI

开发者 https://www.devze.com 2023-02-07 07:27 出处:网络
I wrote an Android application with a UI based on the tab host, consisting of 3 tabs. Each tab consists of some UI elements, mainly text & image views.

I wrote an Android application with a UI based on the tab host, consisting of 3 tabs. Each tab consists of some UI elements, mainly text & image views.

The problem I'm experiencing is as follows: When running the application the first time it works fine. If I don't switch tabs, just click the "back" button to close the application and then run it again it works fine. If I switch to a different tab & back to the first one, and then click "back" to close the application - when I run it again the first tab view appears corrupted. Meaning, one of the images I'm using as a background of the view doesn't appear and instead I get a few gray horizontal lines with a black background along the whole view. All other tabs' views (that use the same image as background btw) appear fine. All following executions of the application remain corrupted.

This is also an inconsistent behavior, and not related to the specific background image I'm using (it still happens even when I remove it completely from the application). Another thing I've tried is to switch between the first (problematic) tab view and the second one. When I did that the problem did not repeat itself.

It smells to me like a problem in the code generation of the R.java file, but I have no idea what's wrong.

Does anyone have any idea what's the problem and how to solve it?

Thanks.

This is the XML of the main tab activity:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:myapp="http://schemas.android.com/apk/res/com.mycompany.myapp"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:background="@drawable/main_bg">

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"/>

</LinearLayout>

This is the XML of the corrupted view:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_horizontal">   

    <TextView
    android:id="@+id/TextTitle"
    android:text="@string/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:paddingTop="5sp"
    android:paddingBottom="5sp"
    android:textColor="@color/pink"
    android:textStyle="bold"
    android:textSize="18sp"
    />      

    <ImageView
    android:src="@drawable/silver_line"
    android:scaleType="fitXY"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <ImageView
        android:src="@drawable/star_bg"
        android:scaleType="fitCenter"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center_horizontal"
        />

        <ImageView
        android:src="@drawable/pink_lights"
        android:scaleType="fitXY"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:gravity="center_horizontal"
            android:background="@color/transparent"&开发者_如何学JAVAgt;   

            <FrameLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">

                <LinearLayout
                    android:id="@+id/LayoutSelectImage"
                    android:orientation="vertical"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center_horizontal">       

                    <TextView
                    android:text="@string/select_image"
                    android:id="@+id/TextSelectImage"
                        android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:minHeight="28sp"
                    android:textColor="@color/white"
                    android:textSize="19sp"
                    />      

                    <LinearLayout
                        android:orientation="horizontal"
                        android:layout_width="wrap_content"
                        android:layout_height="fill_parent"
                        android:layout_marginTop="10sp"
                        android:gravity="center_vertical">     

                        <LinearLayout
                            android:orientation="vertical"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginRight="20sp"
                            android:gravity="center_horizontal">       

                            <ImageView
                            android:id="@+id/ImageCamera"
                        android:src="@drawable/icon_camera_reg"
                            android:scaleType="centerInside"
                            android:layout_width="wrap_content"
                            android:layout_height="fill_parent"
                            />

                            <TextView
                            android:text="@string/camera"
                            android:id="@+id/TextCamera"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textColor="@color/gray"
                            android:textStyle="bold"
                            android:textSize="13sp"
                            />      

                        </LinearLayout>

                        <LinearLayout
                            android:orientation="vertical"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="20sp"
                            android:gravity="center_horizontal">       

                            <ImageView
                            android:id="@+id/ImageGallery"
                        android:src="@drawable/icon_gallery_reg"
                            android:scaleType="centerInside"
                            android:layout_width="wrap_content"
                            android:layout_height="fill_parent"
                            />

                            <TextView                   
                            android:text="@string/gallery"
                            android:id="@+id/TextGallery"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textColor="@color/gray"
                            android:textStyle="bold"
                            android:textSize="13sp"                 
                            />      

                        </LinearLayout>

                    </LinearLayout>

                </LinearLayout>

                <LinearLayout
                    android:id="@+id/LayoutSelectGender"
                    android:orientation="vertical"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center_horizontal"
                    android:visibility="gone">     

                    <TextView
                    android:text="@string/select_gender"
                    android:id="@+id/TextSelectGender"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:minHeight="28sp"
                    android:textColor="@color/white"
                    android:textSize="19sp"
                    />      
                    <!--android:layout_weight="10"-->

                    <LinearLayout
                        android:orientation="horizontal"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="10sp"
                        android:gravity="center_vertical">     
                        <!--android:layout_weight="60"-->

                        <LinearLayout
                            android:orientation="vertical"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginRight="20sp"
                            android:gravity="center_horizontal">       

                            <ImageView
                            android:id="@+id/ImageFemale"
                        android:src="@drawable/icon_female_reg"
                            android:scaleType="centerInside"
                            android:layout_width="wrap_content"
                            android:layout_height="fill_parent"
                            />

                            <TextView
                            android:text="@string/female"
                            android:id="@+id/TextFemale"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textColor="@color/gray"
                            android:textStyle="bold"
                            android:textSize="13sp"
                            />      

                        </LinearLayout>

                        <LinearLayout
                            android:orientation="vertical"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="20sp"
                            android:gravity="center_horizontal">       

                            <ImageView
                            android:id="@+id/ImageMale"
                        android:src="@drawable/icon_male_reg"
                            android:scaleType="centerInside"
                            android:layout_width="wrap_content"
                            android:layout_height="fill_parent"
                            />

                            <TextView
                            android:text="@string/male"
                            android:id="@+id/TextMale"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textColor="@color/gray"
                            android:textStyle="bold"
                            android:textSize="13sp"
                            />      

                        </LinearLayout>

                    </LinearLayout>

                    <ImageView
                    android:id="@+id/ImageSelected"
                    android:background="@drawable/frame_pink_large"
                    android:scaleType="centerCrop"
                    android:layout_width="wrap_content"
                    android:layout_height="0px"
                    android:layout_weight="1"
                    android:layout_margin="7sp"
                    android:padding="6px"
                    />

                </LinearLayout>

                <LinearLayout
                    android:id="@+id/LayoutLoading"
                    android:orientation="vertical"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center_horizontal"
                    android:visibility="gone">     

                    <TextView
                    android:text="@string/loading"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:minHeight="28sp"
                    android:textColor="@color/white"
                    android:textSize="19sp"
                    />      

                    <ImageView
                    android:id="@+id/ImageLoading"
                    android:scaleType="center"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="15sp"
                    android:layout_marginBottom="25sp"
                    />

                    <ImageView
                    android:id="@+id/ImageSelected2"
                    android:background="@drawable/frame_pink_large"
                    android:scaleType="centerCrop"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:layout_margin="7sp"
                    android:padding="6px"
                    />

                </LinearLayout>

                <LinearLayout
                    android:id="@+id/LayoutShow"
                    android:orientation="vertical"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center_horizontal"
                    android:visibility="gone">     

                    <AbsoluteLayout
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="3sp">

                        <ImageView
                        android:id="@+id/ImageStripLeft"
                        android:src="@drawable/strip_left"
                        android:scaleType="fitCenter"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        />                      

                        <LinearLayout
                            android:id="@+id/LayoutOriginal"
                            android:orientation="vertical"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:gravity="center_horizontal">

                            <TextView
                            android:text="@string/original"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="6sp"          
                            android:layout_marginBottom="3sp"
                            android:gravity="center"
                            android:textColor="@color/white"
                            android:textStyle="bold"
                            android:textSize="14sp"
                            android:shadowColor="@color/black"
                            android:shadowDy="1.2"
                            android:shadowRadius="1"
                            />      

                            <ImageView
                            android:id="@+id/ImageOriginal"
                    android:background="@drawable/frame_gray_large"
                            android:scaleType="fitXY"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_margin="10sp"
                            android:padding="6px"
                            />

                        </LinearLayout>

                        <LinearLayout
                            android:id="@+id/LayoutOther"
                            android:orientation="vertical"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:gravity="center_horizontal">

                            <TextView
                            android:text="@string/other"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="6sp"          
                            android:layout_marginBottom="3sp"
                            android:gravity="center"
                            android:textColor="@color/white"
                            android:textStyle="bold"
                            android:textSize="14sp"
                            android:shadowColor="@color/black"
                            android:shadowDy="1.2"
                            android:shadowRadius="1"
                            />      

                            <ImageView
                            android:id="@+id/ImageOther"
                    android:background="@drawable/frame_pink_large"
                            android:scaleType="fitCenter"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_margin="10sp"
                            android:padding="6px"
                            />

                        </LinearLayout>

                        <ImageView
                        android:id="@+id/ImageStripRight"
                        android:src="@drawable/strip_right"
                        android:scaleType="fitCenter"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        />

                        <TextView
                        android:id="@+id/TextPercentage"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:gravity="center_horizontal|top"
                        android:background="@drawable/strip_center"
                        android:paddingTop="2sp"
                        android:textColor="@color/pink"
                        android:textStyle="bold"
                        android:textSize="26sp"
                        android:shadowColor="@color/white"
                        android:shadowDy="1.2"
                        android:shadowRadius="1"
                        />

                    </AbsoluteLayout>

                    <TextView
                    android:id="@+id/TextOther"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:textColor="@color/pink"
                    android:textStyle="bold"
                    android:textSize="16sp"
                    />      

                    <ImageView
                    android:id="@+id/ImageShare"
                    android:src="@drawable/icon_share_reg"
                    android:scaleType="centerInside"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:layout_margin="10sp"
                    />

                    <FrameLayout
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content">

                        <ImageView
                        android:id="@+id/ImageResultsBackground"
                        android:src="@drawable/results_bg"
                        android:scaleType="fitXY"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        />

                        <TextView
                        android:text="@string/more_results"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:layout_marginTop="15sp"
                        android:gravity="center"
                        android:textColor="@color/white"
                        android:textSize="16sp"
                        android:visibility="gone"
                        />      

                        <com.mycompany.myapp.Results.ResultsGallery
                        android:id="@+id/Gallery"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="bottom"
                        android:spacing="20sp"
                        />

                    </FrameLayout>

                </LinearLayout>

            </FrameLayout>

        </LinearLayout>

    </FrameLayout>

</LinearLayout>

EDIT: Soooo weird! The solution was to remove the two places in the corrupted view XML where I defined a background of "@color/transparent" where "transparent" is a color resource of value "#00000000".

My question now is - why???

This is the proper view: http://imgur.com/dEz1G.jpg

And this is the corrupted one: http://imgur.com/CF25i.jpg


I also had this issue and your post helped me a lot specially that transparent thing. I didn't used any color/transparent thing but I was using empty view for my ListView. My issue was that I was using "@id/android:empty" but it should be "@android:id/empty" strangely it worked for months and as soon as I changed the android:targetSdkVersion="18" from 14 to 18, screens with ListView inside Fragments got corrupted.

So it has to do something with how we provide the id to the views. I hope it will help somebody who has wasted lot of time on this. :)

0

精彩评论

暂无评论...
验证码 换一张
取 消