本文来自投稿,不代表长河网立场,转载请注明出处: http://www.changhe99.com/a/qzdnqggwEB.html
你怎么解决Android开发中更新UI报错的异常吗,想实现一个lee一会再更新UI的操作,结果报错了。最后使用adroid.o.Hadler来实现这个功能本文就分享一下解决这个报错的办法
想实现一个sleep一会再更新UI的操作,结果报错了。最后使用android.os.Handler来实现这个功能本文就分享一下解决这个报错的办法工具/原料AndroidStudioJavaAndroid真机方法/步骤1要实现的功能:在App上点击”变透明“的按钮后,App的背景色先变为透明,等等10秒后,再把背景色改为半透明(alpha值为0.5)Showthecode:AndroidManifest.xml: MainActivity.javapublicvoidturnTransparencyClickHandler(Viewview){ changeBackgroundAlphaTo(0.0f);}privatevoidchangeBackgroundAlphaTo(finalfloatalphaValue){ newThread(newRunnable(){ @Override publicvoidrun(){ finalWindowManager.LayoutParamsattributes=getWindow().getAttributes(); attributes.alpha=alphaValue;//0.0全透明.1.0不透明. getWindow().setAttributes(attributes); } }).start();}步骤阅读步骤阅读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)步骤阅读3和Swing中的用法还不一样啊。哪就在UI线程中更新吧。Android中有个组件android.os.Handler的postDelayed可以解决这个问题源码:/***CausestheRunnablertobeaddedtothemessagequeue,toberun*afterthespecifiedamountoftimeelapses.*Therunnablewillberunonthethreadtowhichthishandler*isattached.*Thetime-baseis{@linkandroid.os.SystemClock#uptimeMillis}.*Timespentindeepsleepwilladdanadditionaldelaytoexecution.* *@paramrTheRunnablethatwillbeexecuted.*@paramdelayMillisThedelay(inmilliseconds)untiltheRunnable* willbeexecuted.* *@returnReturnstrueiftheRunnablewassuccessfullyplacedintothe * messagequeue. Returnsfalseonfailure,usuallybecausethe* looperprocessingthemessagequeueisexiting. Notethata* resultoftruedoesnotmeantheRunnablewillbeprocessed--* ifthelooperisquitbeforethedeliverytimeofthemessage* occursthenthemessagewillbedropped.*/publicfinalbooleanpostDelayed(Runnabler,longdelayMillis){ returnsendMessageDelayed(getPostMessage(r),delayMillis);}步骤阅读4改改刚才报错的代码Code:MainActivity.java:定义一个字段privateHandlerhandler=newHandler();publicvoidturnTransparencyClickHandler(Viewview){ changeBackgroundAlphaTo(0.0f); handler.postDelayed(newRunnable(){ @Override publicvoidrun(){ 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);}步骤阅读5执行下,看看是否达到预期效果达到了,Success!步骤阅读步骤阅读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步骤阅读END
赞
(0)