指定的子级已有父级错误

时间:2014-04-17 10:09:45

标签: android android-layout

是的,我知道,这里有很多解决方案..但是我的代码中找不到问题..要使这些代码有效,必须做些什么?我需要在某处编写SetContentView()吗? 代码:

private void setPic() {


//Get image name

        /* There isn't enough memory to open up more than a couple camera photos */
        /* So pre-scale the target bitmap into which the file is decoded */
        /* reviewLayout */


        OnePhotoLayout = (LinearLayout) findViewById(R.id.one_photo_layout);



        reviewEntryTopLayout = (LinearLayout) findViewById(R.id.top_layout);

        /* reviewsLayout */
         AllPhotosLayout = (LinearLayout) findViewById(R.id.photoslayout);
        setContentView(AllPhotosLayout);
         AllPhotosLayout.addView(OnePhotoLayout);

        mImageView = (ImageView) findViewById(R.id.photo);

        /* image   (entry number)*/
        /* Get the size of the ImageView */
        int targetW = 90;   //cc
        int targetH = 120;

        /* Get the size of the image */
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        bmOptions.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
        int photoW = bmOptions.outWidth;
        int photoH = bmOptions.outHeight;

        /* Figure out which way needs to be reduced less */
        int scaleFactor = 1;
        if ((targetW > 0) || (targetH > 0)) {
            scaleFactor = Math.min(photoW/targetW, photoH/targetH); 
        }

        /* Set bitmap options to scale the image decode target */
        bmOptions.inJustDecodeBounds = false;
        bmOptions.inSampleSize = scaleFactor;
        bmOptions.inPurgeable = true;

        /* Decode the JPEG file into a Bitmap */
        Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);

        /* Associate the Bitmap to the ImageView */
        mImageView.setImageBitmap(bitmap);
        mImageView.setVisibility(View.VISIBLE);
         // sutvarkyt photo i maza
        OnePhotoLayout.addView(reviewEntryTopLayout);
        reviewEntryTopLayout.addView(mImageView);
        /////////////////////////////////////////////////
        /* table layout */

        TableLayout tl = new TableLayout(this);
        tl.setLayoutParams(new TableLayout.LayoutParams(
                TableLayout.LayoutParams.MATCH_PARENT,
                TableLayout.LayoutParams.WRAP_CONTENT, 1.0f));
        tl.setPadding(densityToPixels(20), 0, 0, 0);//palikti
        reviewEntryTopLayout.addView(tl);

        /* tableRow1 w */
        TableRow tableRow1 = new TableRow(this);
        tableRow1.setLayoutParams(new TableRow.LayoutParams(
                TableRow.LayoutParams.WRAP_CONTENT,
                TableRow.LayoutParams.WRAP_CONTENT));

        tl.addView(tableRow1);

        /* textView1 w */
        TextView textView1 = new TextView(this);
        textView1.setLayoutParams(new TableRow.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        textView1.setText("nuotraukos datas ");

        tableRow1.addView(textView1);







    }

XML:  

            <LinearLayout
                android:id="@+id/lightSeperator"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="#d5d5d5"
                android:orientation="vertical" />

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

                <!-- Review -->

                <LinearLayout
                    android:id="@+id/one_photo_layout"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:background="@drawable/menu_button_bg"
                    android:orientation="vertical" >

                    <LinearLayout
                        android:id="@+id/top_layout"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center_vertical"
                        android:orientation="horizontal" >

                        <ImageView
                            android:id="@+id/photo"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:visibility="visible" />

                        <TableLayout
                             android:id="@+id/table"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" >

                            <TableRow
                                 android:id="@+id/row"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content" >

                                <TextView
                                     android:id="@+id/text"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content" />
                            </TableRow>
                        </TableLayout>
                    </LinearLayout>
                </LinearLayout>

                <!-- Photos -->

            </LinearLayout>

LogCat:

04-17 13:03:19.950: E/AndroidRuntime(10702): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.project.simplify/com.project.simplify.PhotoUploadActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

0 个答案:

没有答案