养生 装修 购物 美食 感冒 便秘 营销 加盟 小吃 火锅 管理 创业 搭配 减肥 培训 旅游

你怎么解决Android开发中更新UI报错的异常吗

时间:2024-09-23 18:32:46

想实现一个sleep一会再更新UI的操作,结果报错了。最后使用android.os.Handler来实现这个功能本文就分享一下解决这个报错的办法

工具/原料

AndroidStudio

Java

Android真机

方法/步骤

1、要实现的功能:在App上点击”变透明“的按钮后,App的背景色先变为透明,等等10秒后,再把背景色改为半透明(alpha值为0.5)Showthecode:AndroidManifest.xml:<LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:gravity="center"android:orientation="horizontal"><Buttonandroid:id="@+id/clickTransparency"android:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="turnTransparencyClickHandler"android:text="变透明"/>MainActivity.javapublicvoidturnTransparencyClickHandler(Viewview){changeBackgroundAlphaTo(0.0f);}privatevoidchangeBackgroundAlphaTo(finalfloatalphaValue){newThread(newRunnable(){@Overridepublicvoidrun(){finalWindowManager.LayoutParamsattributes=getWindow().getAttributes();attributes.alpha=alphaValue;//0.0全透明.1.0不透明.getWindow().setAttributes(attributes);}}).start();}

你怎么解决Android开发中更新UI报错的异常吗

你怎么解决Android开发中更新UI报错的异常吗

2、执行上述代码。在App中点击”变透明“的按钮后,App就退出了。logcat中打印了这个错:03-2014:47:31.32611640-11796/com.example.cy.myapplicationE/AndroidRuntime:FATALEXCEPTION:Thread-1565103-2014:47:31.32611640-11796/com.example.cy.myapplicationE/AndroidRuntime:Process:com.example.cy.myapplication,PID:1164003-2014:47:31.32611640-11796/com.example.cy.myapplicationE/AndroidRuntime:android.view.ViewRootImpl$CalledFromWrongThreadException:Onlytheoriginalthreadthatcreatedaviewhierarchycantouchitsviews.03-2014:47:31.32611640-11796/com.example.cy.myapplicationE/AndroidRuntime:atandroid.view.ViewRootImpl.checkThread(ViewRootImpl.java:6090)03-2014:47:31.32611640-11796/com.example.cy.myapplicationE/AndroidRuntime:atandroid.view.ViewRootImpl.requestLayout(ViewRootImpl.java:879)03-2014:47:31.32611640-11796/com.example.cy.myapplicationE/AndroidRuntime:atandroid.view.View.requestLayout(View.java:16463)03-2014:47:31.32611640-11796/com.example.cy.myapplicationE/AndroidRuntime:atandroid.view.View.setLayoutParams(View.java:10593)03-2014:47:31.32611640-11796/com.example.cy.myapplicationE/AndroidRuntime:atandroid.view.WindowManagerGlobal.updateViewLayout(WindowManagerGlobal.java:292)03-2014:47:31.32611640-11796/com.example.cy.myapplicationE/AndroidRuntime:atandroid.view.WindowManagerImpl.updateViewLayout(WindowManagerImpl.java:74)03-2014:47:31.32611640-11796/com.example.cy.myapplicationE/AndroidRuntime:atandroid.app.Activity.onWindowAttributesChanged(Activity.java:2347)03-2014:47:31.32611640-11796/com.example.cy.myapplicationE/AndroidRuntime:atandroid.support.v7.internal.view.WindowCallbackWrapper.onWindowAttributesChanged(WindowCallbackWrapper.java:105)03-2014:47:31.32611640-11796/com.example.cy.myapplicationE/AndroidRuntime:atandroid.view.Window.setAttributes(Window.java:847)03-2014:47:31.32611640-11796/com.example.cy.myapplicationE/AndroidRuntime:atcom.example.cy.myapplication.MainActivity$1.run(MainActivity.java:78)03-2014:47:31.32611640-11796/com.example.cy.myapplicationE/AndroidRuntime:atjava.lang.Thread.run(Thread.java:841)

你怎么解决Android开发中更新UI报错的异常吗

3、和Swing中的用法还不一样啊。哪就在UI线程中更新吧。Android中有个组件android.os.Handler的postDelayed可以解决这个问题源码:/***CausestheRunnablertobeaddedtothemessagequeue,toberun*afterthespecifiedamountoftimeelapses.*Therunnablewillberunonthethreadtowhichthishandler*isattached.*<b>Thetime-baseis{@linkandroid.os.SystemClock#uptimeMillis}.</b>*Timespentindeepsleepwilladdanadditionaldelaytoexecution.**@paramrTheRunnablethatwillbeexecuted.*@paramdelayMillisThedelay(inmilliseconds)untiltheRunnable*willbeexecuted.**@returnReturnstrueiftheRunnablewassuccessfullyplacedintothe*messagequeue.Returnsfalseonfailure,usuallybecausethe*looperprocessingthemessagequeueisexiting.Notethata*resultoftruedoesnotmeantheRunnablewillbeprocessed--*ifthelooperisquitbeforethedeliverytimeofthemessage*occursthenthemessagewillbedropped.*/publicfinalbooleanpostDelayed(Runnabler,longdelayMillis){returnsendMessageDelayed(getPostMessage(r),delayMillis);}

你怎么解决Android开发中更新UI报错的异常吗

4、改改刚才报错的代码Code:MainActivity.java:定义一个字段privateHandlerhandler=newHandler();publicvoidturnTransparencyClickHandler(Viewview){changeBackgroundAlphaTo(0.0f);handler.postDelayed(newRunnable(){@Overridepublicvoidrun(){Log.i("turnTransparency","begintochangealphato0.5");changeBackgroundAlphaTo(0.5f);Log.i("turnTransparency","endtochangealphato0.5");}},10*1000);}privatevoidchangeBackgroundAlphaTo(floatalphaValue){WindowManager.LayoutParamsattributes=getWindow().getAttributes();attributes.alpha=alphaValue;//0.0全透明.1.0不透明.getWindow().setAttributes(attributes);}

你怎么解决Android开发中更新UI报错的异常吗

5、执行下,看看是否达到预期效果达到了,Success!

你怎么解决Android开发中更新UI报错的异常吗

你怎么解决Android开发中更新UI报错的异常吗

6、从截图看不出这个变化。在执行变成半透明的代码中,加有日志打印,在logcat中看看能否找到日志:03-2015:16:14.98616571-16571/com.example.cy.myapplicationI/turnTransparency:begintochangealphato0.503-2015:16:14.98616571-16571/com.example.cy.myapplicationI/turnTransparency:endtochangealphato0.5

你怎么解决Android开发中更新UI报错的异常吗

© 一点知识