Android 在后台使用do时以编程方式创建重复的详细布局

Android 在后台使用do时以编程方式创建重复的详细布局,android,android-layout,scrollview,repeat,Android,Android Layout,Scrollview,Repeat,我在这附近见过重复的布局示例。我甚至试过几次,但都没用。大多数并没有提供我在下面试图实现的目标的真实表现。我确实遇到了一个似乎可以工作,但由于我试图使用。添加到父布局,android不喜欢它,所以导致了一个错误。我需要的布局是动态的,这样我就可以在每个项目上使用不同的细节,而不会覆盖以前的重复 下面是重复布局的XML,它的布局与活动不同,我不相信这一点,我想知道是否有更好的方法。我确信我的结构有点超载,但我现在不担心这一点 <?xml version="1.0" encoding="utf

我在这附近见过重复的布局示例。我甚至试过几次,但都没用。大多数并没有提供我在下面试图实现的目标的真实表现。我确实遇到了一个似乎可以工作,但由于我试图使用。添加到父布局,android不喜欢它,所以导致了一个错误。我需要的布局是动态的,这样我就可以在每个项目上使用不同的细节,而不会覆盖以前的重复

下面是重复布局的XML,它的布局与活动不同,我不相信这一点,我想知道是否有更好的方法。我确信我的结构有点超载,但我现在不担心这一点

<?xml version="1.0" encoding="utf-8"?>

            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginBottom="5dp"
                android:layout_marginRight="5dp"
                android:background="@drawable/shop_item_banner"
                android:orientation="horizontal"
                android:padding="10dp" >

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal" >

                    <ImageView
                        android:id="@+id/ImageView01"
                        android:layout_width="55dp"
                        android:layout_height="55dp"
                        android:background="@drawable/shop_back_missing_image" />


                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="0.83"
                        android:orientation="vertical"
                        android:paddingLeft="5dp" >

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" >

                            <TextView
                                android:id="@+id/textView1"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="TextView" />
                            <TextView
                                android:id="@+id/textView3"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="TextView" />

                        </LinearLayout>

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

                    <ImageView
                        android:id="@+id/imageView1"
                        android:layout_width="wrap_content"
                        android:layout_height="fill_parent"
                        android:src="@drawable/shop_divider_section" />

                    <Button
                        android:id="@+id/button2"
                        android:layout_width="50dp"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="5dp"
                        android:background="@drawable/shop_big_button"
                        android:text="BUY"
                        android:typeface="normal" />


                </LinearLayout>

            </LinearLayout>

下面是更新页面结构的代码。它正在更新位于ScrollView中的LinearLayout,以便用户可以滚动项目。我开始收缩代码,以便调试问题。到目前为止,我只进行了一个while循环,将布局打印三次。一次可以很好地工作,但在我让它运行了不止一次之后,它就失败了,应用程序强制关闭。我收到一条警告,表示我无法再将项目添加到所选布局中

    public class LayoutStream extends AsyncTask<String, String, String>{

    String data = null;
    LinearLayout layoutList = (LinearLayout)findViewById(R.id.ScrollItem);

    protected void onPreExecute(){

    }

    @Override
    protected String doInBackground(String... params) {         
        return data;
    }

    protected void onPostExecute(String tpresult){
        int run = 5;
        View storeitems = getLayoutInflater().inflate(R.layout.shop_item, null);
        while (run > 3){

            layoutList.addView(storeitems);
            run--;
        }   
    }
}
公共类LayoutStream扩展异步任务{
字符串数据=null;
LinearLayout layoutList=(LinearLayout)findViewById(R.id.ScrollItem);
受保护的void onPreExecute(){
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
返回数据;
}
受保护的void onPostExecute(字符串tpresult){
int run=5;
View storeitems=GetLayoutFlater().充气(R.layout.shop_item,null);
while(运行>3){
layoutList.addView(storeitems);
跑--;
}   
}
}
任何帮助都是值得感谢的,这是一个很大的障碍,我花了很多时间试图弄清楚为什么会这样

错误代码在前面。错误是在postexecute()期间尝试添加元素时发生的

03-21 21:27:44.245:W/dalvikvm(30172):threadid=1:线程以未捕获异常退出(组=0x40020ac0) 03-21 21:27:44.255:E/AndroidRuntime(30172):致命异常:主 03-21 21:27:44.255:E/AndroidRuntime(30172):java.lang.IllegalStateException:指定的子级已具有父级。必须首先对子级的父级调用removeView()。 03-21 21:27:44.255:E/AndroidRuntime(30172):在android.view.ViewGroup.addViewInner(ViewGroup.java:1970) 03-21 21:27:44.255:E/AndroidRuntime(30172):在android.view.ViewGroup.addView(ViewGroup.java:1865)上 03-21 21:27:44.255:E/AndroidRuntime(30172):在android.view.ViewGroup.addView(ViewGroup.java:1822)上 03-21 21:27:44.255:E/AndroidRuntime(30172):在android.view.ViewGroup.addView(ViewGroup.java:1802)上 03-21 21:27:44.255:E/AndroidRuntime(30172):在com.example.myapp.app.myshop$TokenStream.onPostExecute(myshop.java:188) 03-21 21:27:44.255:E/AndroidRuntime(30172):在com.example.myapp.app.myshop$TokenStream.onPostExecute(myshop.java:1) 03-21 21:27:44.255:E/AndroidRuntime(30172):在android.os.AsyncTask.finish(AsyncTask.java:417) 03-21 21:27:44.255:E/AndroidRuntime(30172):位于android.os.AsyncTask.access$300(AsyncTask.java:127) 03-21 21:27:44.255:E/AndroidRuntime(30172):位于android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429) 03-21 21:27:44.255:E/AndroidRuntime(30172):在android.os.Handler.dispatchMessage(Handler.java:99)上 03-21 21:27:44.255:E/AndroidRuntime(30172):在android.os.Looper.loop(Looper.java:123)上 03-21 21:27:44.255:E/AndroidRuntime(30172):位于android.app.ActivityThread.main(ActivityThread.java:4627) 03-21 21:27:44.255:E/AndroidRuntime(30172):位于java.lang.reflect.Method.Invokenactive(本机方法) 03-21 21:27:44.255:E/AndroidRuntime(30172):在java.lang.reflect.Method.invoke(Method.java:521) 03-21 21:27:44.255:E/AndroidRuntime(30172):在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:885) 03-21 21:27:44.255:E/AndroidRuntime(30172):位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643) 03-21 21:27:44.255:E/AndroidRuntime(30172):在dalvik.system.NativeStart.main(本机方法)


谢谢,所以我找到了问题的答案。当我第一次使用它时,我已经接近解决方案了。这使我还可以针对正在创建的每个项目,在创建时对其进行更新

public class LayoutStream extends AsyncTask<String, String, String>{

    String data = null;
    LinearLayout layoutList = (LinearLayout)findViewById(R.id.ScrollItem);

    protected void onPreExecute(){

    }

    @Override
    protected String doInBackground(String... params) {         
        return data;
    }

    protected void onPostExecute(String tpresult){
        int run = 5;
        LinearLayout layoutList = (LinearLayout)findViewById(R.id.ScrollItem);
        LayoutInflater IInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        while (run >= 1){

            View storeitems = IInflater.inflate(R.layout.shop_item, null);
            layoutList.addView(storeitems, 0);
            run--;
        }   
    }
}
公共类LayoutStream扩展异步任务{
字符串数据=null;
LinearLayout layoutList=(LinearLayout)findViewById(R.id.ScrollItem);
受保护的void onPreExecute(){
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
返回数据;
}
受保护的void onPostExecute(字符串tpresult){
int run=5;
LinearLayout layoutList=(LinearLayout)findViewById(R.id.ScrollItem);
LayoutInflater IInflater=(LayoutInflater)getSystemService(Context.LAYOUT\u充气器\u SERVICE);
while(运行>=1){
查看storeitems=IInflater.充气(R.layout.shop_item,空);
layoutList.addView(storeitems,0);
跑--;
}   
}
}

发生了什么错误?发布错误日志我今天下午下班后会去。我没有时间获取错误日志。