首页 >> 数码 >

androidui框架的使用教程交流 你怎么解决Android开发中更新UI报错的异常吗

2022-07-06 20:35:12 来源: 用户: 

大家好,我是云百科的客服小柏,我来为大家解答以上问题。androidui框架的使用教程交流,你怎么解决Android开发中更新UI报错的异常吗很多人还不知道,现在让我们一起来看看吧!

解答:

1、 要实现的功能:

2、 在应用上点击"变透明"的按钮后,应用程序的背景色先变为透明,等等10秒后,

3、 再把背景色改为半透明(阿尔法值为0.5)

4、 显示代码:

5、 AndroidManifest.xml:

6、 线性布局

7、 Android:layout_width='fill_parent'

8、 Android:layout_height='wrap_content'

9、 安卓:重力重力='中心'

10、 android:orientation='水平'

11、 纽扣

12、 Android:id='@id/点击透明'

13、 Android:layout_width='wrap_content'

14、 Android:layout_height='wrap_content'

15、 Android:onclick='旋转透明循环处理程序'

16、 android:text='变透明'/

17、 MainActivity.java

18、 公共voidturn透明循环处理程序(视图视图){

19、 changeBackgroundAlphaTo(0.0f);

20、 }私有voidchangeBackgroundAlphaTo(最终浮点字母值){

21、 新线程(新Runnable(){

22、 @Overridepublicvoidrun(){

23、 最终窗口管理器布局参数属性=获取窗口().getAttributes();

24、 attributes.alpha=alphavalue//0.0全透明.1.0不透明。

25、 getWindow().设置属性(属性);

26、 }

27、 }).start();

28、 }

29、 执行上述代码。

30、 在应用中点击"变透明"的按钮后,应用程序就退出了。

31、 日志工具中打印了这个错:

32、 03-2014:47:31.32611640-11796/com。举例。赛。我的应用程序E/androidruntime:致命异常:线程-15651

33、 03-2014:47:31.32611640-11796/com。举例。赛。我的应用E/Android运行时:进程:com。举例。赛。我的申请,PID:11640

34、 03-2014:47:31.32611640-11796/com。举例。赛。我的应用E/androidruntime:Android。查看。viewrootimpl$calledfromworkthreadexception:只有创建视图层次结构的原始线程才能触及其视图。

35、 03-2014:47:31.32611640-11796/com。举例。赛。我在Android上的应用E/androidruntime:查看。viewrootimpl。检查线程(viewrootimpl。Java33606090)

36、 03-2014:47:31.32611640-11796/com。举例。赛。我在Android上的应用E/androidruntime:查看。viewrootimpl。请求布局(viewrootimpl。Java3360879)

37、 03-2014:47:31.32611640-11796/com。举例。赛。我的应用E/Android运行时:在Android。查看。查看。请求布局(视图。Java:16463)

38、 03-2014:47:31.32611640-11796/com。举例。赛。我的申请

39、03-2014:47:31.32611640-11796/com.example.cy.myapplicationE/AndroidRuntime:atandroid.view.WindowManagerGlobal.updateViewLayout(WindowManagerGlobal.java:292)

40、03-2014:47:31.32611640-11796/com.example.cy.myapplicationE/AndroidRuntime:atandroid.view.WindowManagerImpl.updateViewLayout(WindowManagerImpl.java:74)

41、03-2014:47:31.32611640-11796/com.example.cy.myapplicationE/AndroidRuntime:atandroid.app.Activity.onWindowAttributesChanged(Activity.java:2347)

42、03-2014:47:31.32611640-11796/com.example.cy.myapplicationE/AndroidRuntime:atandroid.support.v7.internal.view.WindowCallbackWrapper.onWindowAttributesChanged(WindowCallbackWrapper.java:105)

43、03-2014:47:31.32611640-11796/com.example.cy.myapplicationE/AndroidRuntime:atandroid.view.Window.setAttributes(Window.java:847)

44、03-2014:47:31.32611640-11796/com.example.cy.myapplicationE/AndroidRuntime:atcom.example.cy.myapplication.MainActivity$1.run(MainActivity.java:78)

45、03-2014:47:31.32611640-11796/com.example.cy.myapplicationE/AndroidRuntime:atjava.lang.Thread.run(Thread.java:841)

46、和Swing中的用法还不一样啊。

47、哪就在UI线程中更新吧。

48、Android中有个组件android.os.Handler的postDelayed可以解决这个问题

49、源码:

50、/**

51、*CausestheRunnablertobeaddedtothemessagequeue,toberun

52、*afterthespecifiedamountoftimeelapses.

53、*Therunnablewillberunonthethreadtowhichthishandler

54、*isattached.

55、*<b>Thetime-baseis{@linkandroid.os.SystemClock#uptimeMillis}.</b>

56、*Timespentindeepsleepwilladdanadditionaldelaytoexecution.

57、*

58、*@paramrTheRunnablethatwillbeexecuted.

59、*@paramdelayMillisThedelay(inmilliseconds)untiltheRunnable

60、*willbeexecuted.

61、*

62、*@returnReturnstrueiftheRunnablewassuccessfullyplacedintothe

63、*messagequeue.Returnsfalseonfailure,usuallybecausethe

64、*looperprocessingthemessagequeueisexiting.Notethata

65、*resultoftruedoesnotmeantheRunnablewillbeprocessed--

66、*ifthelooperisquitbeforethedeliverytimeofthemessage

67、*occursthenthemessagewillbedropped.

68、*/

69、publicfinalbooleanpostDelayed(Runnabler,longdelayMillis)

70、{

71、returnsendMessageDelayed(getPostMessage(r),delayMillis);

72、}

73、改改刚才报错的代码

74、Code:

75、MainActivity.java:

76、定义一个字段

77、privateHandlerhandler=newHandler();

78、publicvoidturnTransparencyClickHandler(Viewview){

79、changeBackgroundAlphaTo(0.0f);

80、handler.postDelayed(newRunnable(){

81、@Override

82、publicvoidrun(){

83、Log.i("turnTransparency","begintochangealphato0.5");

84、changeBackgroundAlphaTo(0.5f);

85、Log.i("turnTransparency","endtochangealphato0.5");

86、}

87、},10*1000);

88、}

89、privatevoidchangeBackgroundAlphaTo(floatalphaValue){

90、WindowManager.LayoutParamsattributes=getWindow().getAttributes();

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

92、getWindow().setAttributes(attributes);

93、}

94、执行下,看看是否达到预期效果

95、达到了,Success!

96、从截图看不出这个变化。

97、在执行变成半透明的代码中,加有日志打印,在logcat中看看能否找到

98、日志:

99、03-2015:16:14.98616571-16571/com.example.cy.myapplicationI/turnTransparency:

99、begintochangealphato0.5

100、03-2015:16:14.98616571-16571/com.example.cy.myapplicationI/turnTransparency:

100、endtochangealphato0.5

本文到此讲解完毕了,希望对大家有帮助。

  免责声明:本文由用户上传,与本网站立场无关。财经信息仅供读者参考,并不构成投资建议。投资者据此操作,风险自担。 如有侵权请联系删除!

 
分享:
最新文章
站长推荐