Android基础操作-----SuppressLint和SuppressWarnings

一、SuppressLint
1)SuppressLint
Lint是一个静态检查器,它围绕Android项目的正确性、安全性、性能、可用性以及可访问性进行分析。
它检查的对象包括XML资源、位图、ProGuard配置文件、源文件甚至编译后的字节码。
Lint包含了API版本检查、性能检查以及其他诸多特性。
可以使用@SuppressLint标注忽略指定的警告。
如果想去掉的话,可以右键点工程,然后在android tools 中,选择 clear lint marker 就没有这个错误了

其实,既然程序会报出这样的警告,肯定就会有他的不合理之处,只是并不会导致程序发生错误无法运行,我们写的代码平时也不多,所以一般对我们的程序不会有多大的影响,但是他会影响整个程序的安全性及一些其他性能,所以我们还是尽量去避免这写不合理之处。

2)@SuppressLint(“NewApi”)
我们有时会使用比我们在AndroidManifest中设置的android:minSdkVersion版本更高的方法,此时编译器会提示警告,解决方法是在方法上加上@SuppressLint(“NewApi”)或者@TargetApi()。
@SuppressLint(“NewApi”)屏蔽一切新api中才能使用的方法报的android lint错误
@TargetApi() 只屏蔽某一新api中才能使用的方法报的android lint错误
举个例子,某个方法中使用了api9新加入的方法,而项目设置的android:minSdkVersion=8,此时在方法上加@SuppressLint(“NewApi”)和@TargetApi(Build.VERSION_CODES.GINGERBREAD)都可以,以上是通用的情况。
而当你在此方法中又引用了一个api11才加入的方法时,@TargetApi(Build.VERSION_CODES.GINGERBREAD)注解的方法又报错了,而@SuppressLint(“NewApi”)不会报错,这就是区别。

注意,不管你使用了哪个注解,作用仅仅是屏蔽android lint错误,所以在方法中还要根据api的版本进行判断版本做不同的操作。

3)@SuppressLint(“DrawAllocation”)
我们从警告的提示来看,Avoid object allocations during draw/layout operations (preallocate and reuse instead),意为避免在绘制/布局中去实例化对象,解决办法:将这些对象改为类的成员变量。
因为在View及其子类的onDraw(Canvas canvas)方法,会实时调用以更新界面,会频繁的创建对象和进行垃圾回收等,这明显就会影响UI的显示性能,这样一个显示很顺畅的用户界面就会因对象分配引起的一些垃圾回收机制进行短暂的停滞。

4)@SuppressLint(“HandlerLeak”)
在主线程用Handler处理消息出现时会有警告,提示你,这块有内存泄露的危险,handler最好声明为static的

5)@SuppressLint(“CommitPrefEdits”)
sharedPreferences.edit()时,获取到的Editor必须使用commit或apply来完成操作。所以再没有写commit或apply之前或提示@SuppressLint(“CommitPrefEdits”);
Editor的commit和apply相比,apply效率跟高,commit方法返回boolean类型,判断是否存储成功。
简略解释:apply先将数据存储到内存,再异步操作存储到磁盘,而commit:写数据时就同步写到磁盘中了,所以效率会低一些。

6)@SuppressLint(“RestrictedApi”)
受限制的API,那应该还存在其他的替代方式,就尽量不要使用。

7)@SuppressLint(“SimpleDateFormat”)
不规范写法:SimpleDateFormat format = new SimpleDateFormat(pattern);
正确写法:SimpleDateFormat format = new SimpleDateFormat(pattern, Locale.getDefault());

8)@SuppressLint(“DefaultLocale”)
不规范写法:String lower = string.toLowerCase();
boolean b = “String”.toUpperCase().equals(“STRING”);
正确写法:String lower = string.toLowerCase(Locale.getDefault());
boolean b = “String”.toUpperCase().equals(“STRING”);
二、@SuppressWarnings

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
dom4j,jsoup,jdom,w3cdom,xstream使用代码工程 package ivyy.taobao.com.dom4j; import ivyy.taobao.com.entity.Address; import ivyy.taobao.com.entity.Location; import ivyy.taobao.com.entity.Point; import ivyy.taobao.com.entity.Pois; import ivyy.taobao.com.utils.IoUtils; import ivyy.taobao.com.utils.UrlUtils; import ivyy.taobao.com.utils.Dom4jUtils; import java.io.File; import java.net.URL; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.SAXReader; import org.jsoup.Jsoup; import org.jsoup.select.Elements; /** *@Date:2015-1-6 *@Author:liangjilong *@Email:[email protected] *@Version:1.0 *@Description: */ @SuppressWarnings("all") public class Dom4jTest2 { public static void main(String[] args)throws Exception { //String filepath="D:/"+System.currentTimeMillis()+".xml"; String filepath="D:/test/map1.xml"; File f=new File(filepath); if(!f.exists()){ f.createNewFile(); } //List<Pois> list=getReaderXml("URL"); List<Pois> list=getReaderXml("FILE"); org.dom4j.Document doc=createAsXML(list); IoUtils.write(doc.asXML(),filepath); //格式化 Dom4jUtils.formatAsXml(doc); } /**** * 组装成一个xml * @param list * @return * @throws Exception */ private static org.dom4j.Document createAsXML(List<Pois> list) throws Exception{ org.dom4j.Document doc=DocumentHelper.createDocument(); Element root=doc.addElement("GeocoderSearchResponse");//根 root.addElement("status").setText("0");//status for (Iterator iterator = list.iterator(); iterator.hasNext();) { Pois pois = (Pois) iterator.next(); Element result=root.addElement("result");//result List<Location> listLoc=pois.getLocations(); Element location=result.addElement("location");//location for (Iterator iterator2 = listLoc.iterator(); iterator2.hasNext();) { Location locObj = (Location) iterator2.next(); location.addElement("lat").setText(locObj.getLat()+"");//lat location.addElement("lng").setText(locObj.getLng()+"");//lat result.addElement("formatted_address").setText(locObj.getFormattedAddress()+"");//formatted_address result.addElement("business").setText(locObj.getBusiness()+"");//business } List<Address> listAdd=pois.getAddress(); Element comp=result.addElement("addressComponent");//addressComponent for (Iterator iterator3 = listAdd.iterator(); iterator3.hasNext();) { Address address = (Address) iterator3.next(); comp.addElement("streetNumber").setText(address.getStreetNumber()+"");//streetNumber comp.addElement("street").setText(address.getStreet()+"");//street comp.addElement("district").setText(address.getDistrict()+"");//district comp.addElement("city").setText(address.getCity()+"");//city comp.addElement("province").setText(address.getProvince()+"");//province comp.addElement("cityCode").setText(address.getCityCode()+"");//cityCode } Element poi=result.addElement("pois").addElement("poi"); poi.addElement("addr").setText(pois.getAddr());//addr poi.addElement("distance").setText(pois.getDistance());//distance poi.addElement("name").setText(pois.getName());//name poi.addElement("poiType").setText(pois.getPoiType());//poiType poi.addElement("tel").setText(pois.getTel());//tel poi.addElement("zip").setText(pois.getZip());//zip List<Point> listPoint=pois.getPoints(); Element point=poi.addElement("point"); for (Iterator iterator4 = listPoint.iterator(); iterator4.hasNext();) { Point p = (Point) iterator4.next(); point.addElement("x").setText(p.getX()+""); point.addElement("y").setText(p.getY()+""); } } return doc; } /** * Dom4j(SAX)读取xml数据(解析) * @param params * @throws Exception */ private static List<Pois> getReaderXml(String flg) throws Exception{ String fromRead=Dom4jTest2.class.getClassLoader().getResource("xml/map1.xml").getPath(); List<Pois> list=new ArrayList<Pois>(); SAXReader saxReader = new SAXReader(); org.dom4j.Document document=null; //从api上面解析 if(flg.equals("URL")){ String url = UrlUtils.getBaiduMapUrl("你的key", "39.983424,116.322987", "xml"); document = saxReader.read(url); //从文件上面的xml解析 }else if(flg.equals("FILE")){ document = saxReader.read(new File(fromRead)); } Element resultEl = (Element)document.getRootElement().element("result"); Element poisEl=resultEl.element("pois");//pois节点 Element locationEl=resultEl.element("location");//location节点 Element addressEl=resultEl.element("addressComponent");//addressComponent节点 /*******从pois节点下面遍历多个poi节点*******/ for (Iterator<Element> poIter = poisEl.elementIterator("poi"); poIter.hasNext();) { Element element = (Element) poIter.next(); String addr = element.elementText("addr"); String distance = element.elementText("distance"); String name = element.elementText("name"); String poiType = element.elementText("poiType"); String tel =(element.elementText("tel")==""?"":element.elementText("tel")); String zip =(element.elementText("zip")==""?"":element.elementText("zip")); Pois poi=new Pois(); poi.setAddr(addr); poi.setDistance(distance); poi.setName(name); poi.setPoiType(poiType); poi.setTel(tel); poi.setZip(zip); List<Location> listLoc=new ArrayList<Location>(); /************Location***************************/ String business=resultEl.elementText("business"); String formatted_address=resultEl.elementText("formatted_address"); String lat = locationEl.elementText("lat"); String lng = locationEl.elementText("lng"); Location location=new Location(); location.setBusiness(business); location.setFormattedAddress(formatted_address); location.setLat(lat); location.setLng(lng); listLoc.add(location); poi.setLocations(listLoc); List<Address> listAddr=new ArrayList<Address>(); /************Address***************************/ Address address=new Address(); String streetNumber=(addressEl.elementText("streetNumber")==""?"":addressEl.elementText("streetNumber")); String street=(addressEl.elementText("street")==""?"":addressEl.elementText("street")); String district=(addressEl.elementText("district")==""?"":addressEl.elementText("district")); String city=(addressEl.elementText("city")==""?"":addressEl.elementText("city")); String province=(addressEl.elementText("province")==""?"":addressEl.elementText("province")); String direction=(addressEl.elementText("direction")==""?"":addressEl.elementText("direction")); String distancez=(addressEl.elementText("distance")==""?"":addressEl.elementText("distance")); address.setStreetNumber(streetNumber); address.setStreet(street); address.setCity(city); address.setDistrict(district); address.setDirection(direction); address.setDistance(distancez); address.setProvince(province); listAddr.add(address); poi.setAddress(listAddr); list.add(poi); } return list; } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值