你怎麼解決Android開發中更新UI報錯的異常嗎?

想實現一個sleep 一會再更新UI的操作,結果報錯了。

最後使用android.os.Handler來實現這個功能

本文就分享一下解決這個報錯的辦法

工具/原料

Android Studio
Java
Android真機

方法/步驟

要實現的功能:

在App上點擊」變透明「的按鈕後,App的背景色先變為透明,等等10秒後,

再把背景色改為半透明(alpha值為0.5)

Show the code:

AndroidManifest.xml:

<LinearLayout

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:gravity="center"

android:orientation="horizontal">

<Button

android:id="@+id/clickTransparency"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:onClick="turnTransparencyClickHandler"

android:text="變透明" />

MainActivity.java

public void turnTransparencyClickHandler(View view) {

changeBackgroundAlphaTo(0.0f);

}private void changeBackgroundAlphaTo(final float alphaValue) {

new Thread(new Runnable() {

@Override        public void run() {

final WindowManager.LayoutParams attributes = getWindow().getAttributes();

attributes.alpha = alphaValue;//0.0全透明.1.0不透明.

getWindow().setAttributes(attributes);

}

}).start();

}

執行上述代碼。

在App中點擊」變透明「的按鈕後,App就退出了。

logcat中列印了這個錯:

03-20 14:47:31.326 11640-11796/com.example.cy.myapplication E/AndroidRuntime: FATAL EXCEPTION: Thread-15651

03-20 14:47:31.326 11640-11796/com.example.cy.myapplication E/AndroidRuntime: Process: com.example.cy.myapplication, PID: 11640

03-20 14:47:31.326 11640-11796/com.example.cy.myapplication E/AndroidRuntime: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

03-20 14:47:31.326 11640-11796/com.example.cy.myapplication E/AndroidRuntime:     at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6090)

03-20 14:47:31.326 11640-11796/com.example.cy.myapplication E/AndroidRuntime:     at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:879)

03-20 14:47:31.326 11640-11796/com.example.cy.myapplication E/AndroidRuntime:     at android.view.View.requestLayout(View.java:16463)

03-20 14:47:31.326 11640-11796/com.example.cy.myapplication E/AndroidRuntime:     at android.view.View.setLayoutParams(View.java:10593)

03-20 14:47:31.326 11640-11796/com.example.cy.myapplication E/AndroidRuntime:     at android.view.WindowManagerGlobal.updateViewLayout(WindowManagerGlobal.java:292)

03-20 14:47:31.326 11640-11796/com.example.cy.myapplication E/AndroidRuntime:     at android.view.WindowManagerImpl.updateViewLayout(WindowManagerImpl.java:74)

03-20 14:47:31.326 11640-11796/com.example.cy.myapplication E/AndroidRuntime:     at android.app.Activity.onWindowAttributesChanged(Activity.java:2347)

03-20 14:47:31.326 11640-11796/com.example.cy.myapplication E/AndroidRuntime:     at android.support.v7.internal.view.WindowCallbackWrapper.onWindowAttributesChanged(WindowCallbackWrapper.java:105)

03-20 14:47:31.326 11640-11796/com.example.cy.myapplication E/AndroidRuntime:     at android.view.Window.setAttributes(Window.java:847)

03-20 14:47:31.326 11640-11796/com.example.cy.myapplication E/AndroidRuntime:     at com.example.cy.myapplication.MainActivity$1.run(MainActivity.java:78)

03-20 14:47:31.326 11640-11796/com.example.cy.myapplication E/AndroidRuntime:     at java.lang.Thread.run(Thread.java:841)

和Swing中的用法還不一樣啊。

哪就在UI線程中更新吧。

Android中有個組件android.os.Handler的postDelayed可以解決這個問題

源碼:

/**

* Causes the Runnable r to be added to the message queue, to be run

* after the specified amount of time elapses.

* The runnable will be run on the thread to which this handler

* is attached.

* <b>The time-base is {@link android.os.SystemClock#uptimeMillis}.</b>

* Time spent in deep sleep will add an additional delay to execution.

*

* @param r The Runnable that will be executed.

* @param delayMillis The delay (in milliseconds) until the Runnable

*        will be executed.

*

* @return Returns true if the Runnable was successfully placed in to the

*         message queue.  Returns false on failure, usually because the

*         looper processing the message queue is exiting.  Note that a

*         result of true does not mean the Runnable will be processed --

*         if the looper is quit before the delivery time of the message

*         occurs then the message will be dropped.

*/

public final boolean postDelayed(Runnable r, long delayMillis)

{

return sendMessageDelayed(getPostMessage(r), delayMillis);

}

改改剛才報錯的代碼

Code:

MainActivity.java:

定義一個欄位

private Handler handler = new Handler();

public void turnTransparencyClickHandler(View view) {

changeBackgroundAlphaTo(0.0f);

handler.postDelayed(new Runnable() {

@Override

public void run() {

Log.i("turnTransparency", "begin to change alpha to 0.5");

changeBackgroundAlphaTo(0.5f);

Log.i("turnTransparency", "end to change alpha to 0.5");

}

}, 10 * 1000);

}

private void changeBackgroundAlphaTo(float alphaValue) {

WindowManager.LayoutParams attributes = getWindow().getAttributes();

attributes.alpha = alphaValue;//0.0全透明.1.0不透明.

getWindow().setAttributes(attributes);

}

執行下,看看是否達到預期效果

達到了,Success!

從截圖看不出這個變化。

在執行變成半透明的代碼中,加有日誌列印,在logcat中看看能否找到

日誌:

03-20 15:16:14.986 16571-16571/com.example.cy.myapplication I/turnTransparency: begin to change alpha to 0.5

03-20 15:16:14.986 16571-16571/com.example.cy.myapplication I/turnTransparency: end to change alpha to 0.5

本文內容整理自網絡, 文中所有觀點看法不代表淘大白的立場