开发者

Dynamic Appending of Views and crashing

开发者 https://www.devze.com 2023-01-11 17:29 出处:网络
Having another issue, and this one is BAFFLING! Here we go. We have a layout... <?xml version=\"1.0\" encoding=\"utf-8\"?>

Having another issue, and this one is BAFFLING!

Here we go.

We have a layout...

<?xml version="1.0" encoding="utf-8"?>
<!-- Template for creating a result in MediaUpload -->
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <LinearLayout
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal"
      android:isScrollContainer="true"
      android:focusableInTouchMode="false"
      android:focusable="false">
      <ImageView android:id="@+id/imgView" 
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:maxWidth="110dp"
      android:maxHeight="110dp"
      android:minWidth="110dp"
      android:minHeight="110dp"
      android:layout_gravity="center_vertical"
      android:padding="3dp"
      android:scaleType="center"
      android:clickable="true"
      android:focusable="true"
      android:focusableInTouchMode="true"
      android:saveEnabled="true"/>
      <LinearLayout 
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical"
      android:gravity="center_horizontal">
        <TextView android:id="@+id/img_file_name" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/LinkText"
        android:textStyle="bold"
        android:paddingBottom="3dp"/>
        <EditText android:id="@+id/img_title" 
        android:hint="Title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:minWidth="150dp"
        android:maxHeight="50dp"
        android:inputType="textVisiblePassword"
        android:maxLength="255"/>
        <EditText android:id="@+id/img_desc" 
        android:hint="Description"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:minWidth="150dp"
        android:maxHeight="50dp"
        android:maxLength="255"/>
      </LinearLayout>
    </LinearLayout>
    <ImageView
      android:src="@drawable/divider"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:scaleType="fitXY"
      android:paddingTop="2dp"
      android:paddingBottom="2dp">
    </ImageView>
</LinearLayout>

We're using this here as a template for an object that a user can upload to our site. (This is in a massive layout, so just posting what we're appending it to).

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

Soo...Here's the actual issue. When a user selects an image from their gallery, it adds correctly to our LinearLayout. Now, one of our workers has low memory issues all the time, and the app is closed while he takes pictures or browses. Our app is set up to save all the data (data being the Uri, Title text, Description text, etc) immediately before the user leaves the app into the savedInstanceState method.

/*
     * The Activity is being recycled and needs to have its instance
     * saved before we lost all of 开发者_如何学编程the image URis!
     */
    protected void onSaveInstanceState(Bundle outState)
    {
        super.onSaveInstanceState(outState);
        helper.saveEnteredData();
        outState.putParcelableArray(IMAGE_URI_BUNDLE_KEY, helper.getUploadObjectArray());
        outState.putParcelable(CAPTURE_IMAGE_URI_BUNDLE_KEY, currentFileURI);

        outState.putInt(CURRENT_JOB_BUNDLE_KEY, reportSpinner.getSelectedItemPosition());

        ArrayList<ReportInfoParcelable> alReps = new ArrayList<ReportInfoParcelable>();
        for(int i = 0; i < reportList.length; ++i)
            alReps.add(new ReportInfoParcelable(reportList[i]));

        outState.putParcelableArray(JOB_LIST_BUNDLE_KEY, alReps.toArray(new ReportInfoParcelable[alReps.size()]));
    }

Here is the helper.saveEnteredData()...

public void saveEnteredData()
    {
        ViewGroup vg = null;
        UploadObject object = null;
        String title, desc;
        for(int i = 0; i < mLinearLayout.getChildCount(); i++)
        {
            vg = (ViewGroup) mLinearLayout.getChildAt(i);
            object = alUploadObjects.get(i);
            title = ((TextView) vg.findViewById(R.id.img_title)).getText().toString();
            desc = ((TextView) vg.findViewById(R.id.img_desc)).getText().toString();
            object.setTitle(title);
            object.setDesc(desc);
        }
    }

When the app returns from a crash after the user left the activity, when we debug through the app on return, the array of UploadObjects that return contain all the correct Uri's, titles, and descriptions, but when the app finally displays, ALL editTexts on the LinearLayout we append to, all suddenly contain the title and description of the last UploadObject in the Parcelable array. Again, we debug through each appending of the objects, and the objects contain the correct data. We are baffled and need some help here!


We got it!

Not totally sure what is going on here, but we overrode(sp?) the method onPostCreate(Bundle) and parsed out the bundle here instead and our problem is fixed. There must be something about inflating an xml file during an onCreate() which messes something up.

Anyway, if you have to inflate an XML (other than your regular layout), do it onPostCreate()!

0

精彩评论

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