困境UI线程或新线程

时间:2017-01-18 18:11:12

标签: android ui-thread android-anr-dialog

我有一堆textview和按钮最初在.xml中设置为GONE,我设置代码以便在短时间内逐个显示它们(让它们变得可见)。如果我使用UI线程,我冒险"应用程序没有响应" (或" ANR")如果间隔时间长于约5秒,但如果我创建新线程则必须使用

.getHandler().post(new Runnable() {
                    public void run() {
                        .setVisibility(View.VISIBLE);
                    }

继续回到UI线程(在我的理解中),所以......拥有新线程的重点是什么??

有人可以解释并详细说明:)

XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/sv"

        >

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

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/textview_background"
                android:text="Linija 1"
                android:id="@+id/tv1"
                android:visibility="gone"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/textview_background"
                android:text="Linija 2"
                android:id="@+id/tv2"
                android:visibility="gone"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/textview_background"
                android:text="Linija 3"
                android:id="@+id/tv3"
                android:visibility="gone"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/textview_background"
                android:text="Linija 4"
                android:id="@+id/tv4"
                android:visibility="gone"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/textview_background"
                android:text="Linija 5"
                android:id="@+id/tv5"
                android:visibility="gone"/>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/LL1"
                android:visibility="gone">

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:id="@+id/btn1"
                    android:text="Btn1"/>

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:id="@+id/btn2"
                    android:text="Btn2"/>

            </LinearLayout>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/my_textview_background"
                android:text="Linija 6"
                android:id="@+id/tv6"
                android:layout_gravity="end"
                android:visibility="gone"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/my_textview_background"
                android:text="Linija 7"
                android:id="@+id/tv7"
                android:layout_gravity="end"
                android:visibility="gone"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/textview_background"
                android:text="Linija 8"
                android:id="@+id/tv8"
                android:visibility="gone"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/textview_background"
                android:text="Linija 9"
                android:id="@+id/tv9"
                android:visibility="gone"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/textview_background"
                android:text="Linija 10"
                android:id="@+id/tv10"
                android:visibility="gone"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/textview_background"
                android:text="Linija 11"
                android:id="@+id/tv11"
                android:visibility="gone"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/textview_background"
                android:text="Linija 12"
                android:id="@+id/tv12"
                android:visibility="gone"/>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/LL2"
                android:visibility="gone">

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:id="@+id/btn3"
                    android:text="Btn3"/>

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:id="@+id/btn4"
                    android:text="Btn4"/>

            </LinearLayout>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/my_textview_background"
                android:text="Linija 13"
                android:id="@+id/tv13"
                android:layout_gravity="end"
                android:visibility="gone"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/my_textview_background"
                android:text="Linija 14"
                android:id="@+id/tv14"
                android:layout_gravity="end"
                android:visibility="gone"/>

</LinearLayout>



    </ScrollView>

</RelativeLayout>

测试活动

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_layout);
        Thread mainThread = new Thread(
                new Runnable() {
                    @Override
                    public void run() {
                        start_game();
                    }
                }
        );
        mainThread.start();
    }

public void start_game () {

Button btn1 = (Button) findViewById(R.id.btn1);
        ... btn4

TextView tv1 = (TextView) findViewById(R.id.tv1);
        ... tv14

LinearLayout LL1 = (LinearLayout) findViewById(R.id.LL1);
        LinearLayout LL2 = (LinearLayout) findViewById(R.id.LL2);

 show_line(tv1, 1000);
        show_line(tv2, 1000);
        show_line(tv3, 1000);
        show_line(tv4, 1000);
        show_line(tv5, 1000);

        btnLL(LL1, btn1, btn2, tv6, tv7);

if (btn1.isPressed())        show_line(tv8, 1000);
        if (btn2.isPressed())        show_line(tv9, 1000);
        show_line(tv10, 1000);
        show_line(tv11, 1000);
        show_line(tv12, 1000);

public void show_line(final TextView tv, int duration) {
        if (x) x=false;
        viewArray[viewIndex++]= tv;
        try {
            Thread.sleep(duration);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        tv.getHandler().post(new Runnable() {
            public void run() {
                tv.setVisibility(View.VISIBLE);
            }
        });
    }

public void btnLL (final LinearLayout LL, Button btnLeft, Button btnRight, final TextView tvLeft, final TextView tvRight) {
        linearArray[llIndex++]=LL;      
        LL.getHandler().post(new Runnable() {
            public void run() {
                LL.setVisibility(View.VISIBLE);
            }
        });

        btnLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {                
                LL.getHandler().post(new Runnable() {
                    public void run() {
                        LL.setVisibility(View.GONE);
                    }
                });
                tvLeft.getHandler().post(new Runnable() {
                    public void run() {
                        tvLeft.setVisibility(View.VISIBLE);
                    }
                });
                x = true;
                myViewArray[myViewIndex++]=tvLeft;
            }
        });

        btnRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LL.getHandler().post(new Runnable() {
                    public void run() {
                        LL.setVisibility(View.GONE);
                    }
                });
                tvRight.getHandler().post(new Runnable() {
                    public void run() {
                        tvRight.setVisibility(View.VISIBLE);
                    }
                });
                x = true;
                myViewArray[myViewIndex++]=tvRight;
            }
        });
        while (!x){}
    }

1 个答案:

答案 0 :(得分:1)

这显然是一个奇怪/糟糕的设计案例。你只有1个UI线程。如果您需要立即更新UI上的大量数据,可能需要间隔刷新屏幕? IE有一个可以自行调用的post runnable,让它延迟2秒,读取你的状态并进行UI更改。如果你觉得它很有用,我可以写出来......

更新发布更多信息..

看着这个。显然这对你来说是错误的教程。老实说,我打算投票来结束这个问题,因为我认为它对别人没用。那说你的结构应该看起来更接近..虽然你可能对如何做到这一点感到困惑...如果你只是想让它们在用户按下按钮时显示和隐藏......摆脱线程。您根本不需要线程来执行此操作。对我来说,这个线程看起来像你正在写一个游戏,游戏正在不断循环。如果您正在等待用户按下按钮,则该按钮由收听者支持。写一个帖子无济于事......

  //HERE IS WHERE YOU DECLARE THE BUTTONS
    Button btn1; //... btn4
    TextView tv1; //... tv14
    LinearLayout LL1;
    LinearLayout LL2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_layout);
        //HERE IS WHERE YOU SET YOUR BUTTONS
        btn1 = (Button) findViewById(R.id.btn1);
         tv1 = (TextView) findViewById(R.id.tv1);
         LL1 = (LinearLayout) findViewById(R.id.LL1);
         LL2 = (LinearLayout) findViewById(R.id.LL2);

        //SET CLICK HANDLERS HERE
        btnRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LL.setVisibility(View.GONE);
                tvRight.setVisibility(View.VISIBLE);
                x = true;
                myViewArray[myViewIndex++] = tvRight;
            } );

            btnLeft.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    LL.getHandler().post(new Runnable() {
                        public void run() {
                            LL.setVisibility(View.GONE);
                        }
                    });
                    tvLeft.getHandler().post(new Runnable() {
                        public void run() {
                            tvLeft.setVisibility(View.VISIBLE);
                        }
                    });
                    x = true;
                    myViewArray[myViewIndex++]=tvLeft;
                }
            });

        Thread mainThread = new Thread(
                new Runnable() {
                    @Override
                    public void run() {
                        start_game();
                    }
                }
        );
        mainThread.start();
    }

    public void start_game () {
// guessing you would have a do while loop here?
        show_line(tv1, 1000);
        show_line(tv2, 1000);
        show_line(tv3, 1000);
        show_line(tv4, 1000);
        show_line(tv5, 1000);

        btnLL(LL1, btn1, btn2, tv6, tv7);

        if (btn1.isPressed()) show_line(tv8, 1000);
        if (btn2.isPressed()) show_line(tv9, 1000);
        show_line(tv10, 1000);
        show_line(tv11, 1000);
        show_line(tv12, 1000);
    }

    public void show_line(final TextView tv, int duration) {
        if (x) x=false;
        viewArray[viewIndex++]= tv;
        try {
            Thread.sleep(duration); // look into post delay runnables.... thread sleep no.
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        runOnUiThread(new Runnable() { // we are on background thread. use runOnUiThread
            @Override
            public void run() {
                tv.setVisibility(View.VISIBLE);
            }
        });

    }

    public void btnLL (final LinearLayout LL, Button btnLeft, Button btnRight, final TextView tvLeft, final TextView tvRight) {
        linearArray[llIndex++]=LL;
        runOnUiThread(new Runnable() { // we are on background thread. use runOnUiThread
            @Override
            public void run() {
                LL.setVisibility(View.VISIBLE);
            }
        });

    }