安卓开发的一些备忘问题

来源:互联网 发布:帝国cms二次开发 编辑:程序博客网 时间:2024/05/18 01:02

1、关于java内部类、内部静态类的一些问题

http://android.yaohuiji.com/archives/3247

2、在组件或者activity中引用handler时eclipse警告This Handler class should be static or leaks might occur, 警告原因参考StackOverflow中老外的解释

Issue: Ensures that Handler classes do not hold on to a reference to an outer class.
In Android, Handler classes should be static or leaks might occur. Messages enqueued on the application thread's MessageQueue also retain their target Handler. If the Handler is an inner class, its outer class will be retained as well. To avoid leaking the outer class, declare the Handler as a static nested class with a WeakReference to its outer class.

解决方法很简单,定义一个内部静态类继承Handler即可,参考代码

        MyHandler handler = new MyHandler(this);static class MyHandler extends Handler {WeakReference<Switcher> weakReference;MyHandler(Switcher switcher) {// TODO Auto-generated constructor stubweakReference = new WeakReference<Switcher>(switcher);}@Overridepublic void handleMessage(Message msg) {// TODO Auto-generated method stubSwitcher switcher = weakReference.get();if (msg.what == DISMISS_CONTROLS) {switcher.mDecreasePopup.dismiss();switcher.mIncreasePopup.dismiss();return;}switcher.mScroller.computeScrollOffset();int currX = switcher.mScroller.getCurrX();int delta = switcher.mPosition - currX;switcher.mPosition = currX;int packed = switcher.getPackedViews(switcher.mPosition);switcher.manageViews(packed);switcher.scroll(delta);if (!switcher.mScroller.isFinished()) {switcher.handler.sendEmptyMessage(msg.what);} else {if (msg.what == SCROLL) {switcher.justify();} else {switcher.mIndex = switcher.mPosition / switcher.mSize;switcher.setupButtons();}}}}


3、安卓低版本SDK项目重建为高版本SDK,因为有些主题因为安或者性能问题被移除而报错的问题

error: Error retrieving parent for item: No resource found that matches the given name 'android:XXXXXX'.

解决方法参考http://bbs.csdn.net/topics/370126223#post-382319519

具体是先安装低版本的SDK,然后找到%SDK_HOME%\platforms\android-{SDK_version}\data\res\values对应的找到缺失的style,加到你自己的项目中,示例如下

<!-- 解决低版本的SDK引用WindowTitleBackground报资源not found异常 -->    <!-- solution:http://bbs.csdn.net/topics/370126223#post-382319519 -->    <style name="WindowTitleBackground">        <item name="android:background">@android:drawable/title_bar</item>    </style>    <style name="iWindowTitleBackground" parent="@style/WindowTitleBackground">        <item name="android:background">@drawable/title_bar</item>    </style>

4、AIDL跨进程调用

首先,必须保证你的几个AIDL文件在所有项目中的包名都必须一致,比如你的service项目里面定义了aidl,那么你的其他调用的activity copy过来的aidl文件的包名就应该与server保持一致。其次service必须有android:exported="true"保证能被外部引用

5、其他一些性能警告问题,收集了http://blog.csdn.net/time_hunter/article/details/8664665


1.ObsoleteLayoutParam不起作用的标签Invalid layout param in a LinearLayout: layout_centerVerticalIssue: Looks for layout params that are not valid for the given parent layoutId: ObsoleteLayoutParamThe given layout_param is not defined for the given layout, meaning it has no effect. This usually happens when you change the parent layout or move view code around without updating the layout params. This will cause useless attribute processing at runtime, and is misleading for others reading the layout so the parameter should be removed.2.HandlerLeak 非静态的内部类HandlerThis Handler class should be static or leaks might occur (com.borqs.appinstaller.AppMoveFragment.LoadAppHandler)Issue: Ensures that Handler classes do not hold on to a reference to an outer classId: HandlerLeakIn Android, Handler classes should be static or leaks might occur. Messages enqueued on the application thread's MessageQueue also retain their target Handler. If the Handler is an inner class, its outer class will be retained as well. To avoid leaking the outer class, declare the Handler as a static nested class with a WeakReference to its outer class.3.FloatMath 有时候Android有更好的API实现相比JavaUse android.util.FloatMath#ceil() instead of java.lang.Math#ceil to avoid argument float to double conversionIssue: Suggests replacing java.lang.Math calls with android.util.FloatMath to avoid conversionsId: FloatMathOn modern hardware, "double" is just as fast as "float" though of course it takes more memory. However, if you are using floats and you need to compute the sine, cosine or square root, then it is better to use the android.util.FloatMath class instead of java.lang.Math since you can call methods written to operate on floats, so you avoid conversions back and forth to double.http://developer.android.com/guide/practices/design/performance.html#avoidfloat4.InefficientWeight Use a layout_width of 0dip instead of wrap_content for better performanceIssue: Looks for inefficient weight declarations in LinearLayoutsId: InefficientWeightWhen only a single widget in a LinearLayout defines a weight, it is more efficient to assign a width/height of 0dp to it since it will absorb all the remaining space anyway. With a declared width/height of 0dp it does not have to measure its own size first.5.NestedWeights Nested weights are bad for performanceIssue: Looks for nested layout weights, which are costlyId: NestedWeightsLayout weights require a widget to be measured twice. When a LinearLayout with non-zero weights is nested inside another LinearLayout with non-zero weights, then the number of measurements increase exponentially.6.UnusedResources The resource R.drawable.ic_action_search appears to be unusedIssue: Looks for unused resourcesId: UnusedResourcesUnused resources make applications larger and slow down builds.7.DrawAllocation 避免在draw和layout的时候创建对象,频繁的调用,垃圾回收操作会影响一些UI的体验Avoid object allocations during draw/layout operations (preallocate and reuse instead)Issue: Looks for memory allocations within drawing codeId: DrawAllocationYou should avoid allocating objects during a drawing or layout operation. These are called frequently, so a smooth UI can be interrupted by garbage collection pauses caused by the object allocations.The way this is generally handled is to allocate the needed objects up front and to reuse them for each drawing operation.Some methods allocate memory on your behalf (such as Bitmap.create), and these should be handled in the same way.8.UseCompoundDrawablesThis tag and its children can be replaced by one <TextView/> and a compound drawableIssue: Checks whether the current node can be replaced by a TextView using compound drawables.Id: UseCompoundDrawablesA LinearLayout which contains an ImageView and a TextView can be more efficiently handled as a compound drawable.There's a lint quickfix to perform this conversion in the Eclipse plugin.9.MergeRootFrameThis <FrameLayout> can be replaced with a <merge> tagIssue: Checks whether a root <FrameLayout> can be replaced with a <merge> tagId: MergeRootFrameIf a <FrameLayout> is the root of a layout and does not provide background or padding etc, it can often be replaced with a <merge> tag which is slightly more efficient. Note that this depends on context, so make sure you understand how the <merge> tag works before proceeding.http://android-developers.blogspot.com/2009/03/android-layout-tricks-3-optimize-by.html10.UselessParent 有些Layout可以省略This LinearLayout layout or its FrameLayout parent is uselessIssue: Checks whether a parent layout can be removed.Id: UselessParentA layout with children that has no siblings, is not a scrollview or a root layout, and does not have a background, can be removed and have its children moved directly into the parent for a flatter and more efficient layout hierarchy.


原创粉丝点击