【总结】常见的开发中错误

项目马上要完成了,在开发中遇到了很多问题,包括以前的问题,这里进行一个小的总结吧,学习交流用,如果有不正确的地方多多指出,你知道了也让我知道吧,哈哈,这里因为错误很多,只总结小部分。

 

1.String字符串值比较与字符串地址比较混淆

== 在字符串比较中为地址比较

.equals()方法为字符串值比较

 

public class StringDemo {
	public static void main(String[] args) {
		String string1 = "123";
		String string2 = "123";
		String string3 = new String("123");
		//----------------==---------------------
		System.out.println(string1 == string2);
		System.out.println(string2 == string3);
		System.out.println(string1 == string3);
		//----------------equals---------------------
		System.out.println(string1.equals(string2));
		System.out.println(string2.equals(string3));
		System.out.println(string1.equals(string3));

	}
}



 

运行结果:

true

false

false

true

true

true

之所以

System.out.println(string1 == string2);

System.out.println(string2 == string3);

System.out.println(string1 == string3);

是 t f f 这个结果是因为JavaString常量池,因为string1string2 是一般的赋值过程,所以当string1string2比较地址时候,Java会在常量池中寻找存储相同值的内存地址,同时string1string2同时指向这个地址,所以是true string3虽然值相同,但是使用new声明的时候,内存中指向的不是string1string2的地址,因为使用new关键字会在堆中开辟一个空间来存放,所以后面的都是 false  false

 

 

 

2.使用SharedPerference来存储的注意点

虽然使用sp来存储很方便,但是如果不注意的话,可能会使用sp存储大量数据,比如状态字段值,id值,字符串等等,如果少量使用的话或是使用合理的话,这样弊端还不是很明显,但是如果大量使用sp的话,那么弊端就显现出来了,以为sp是使用xml来存储数据的,所以在解析文件的时候也是一个IO操作,需要去读取文件解析节点等操作,这样会很消耗内存,使程序运行不流畅,所以建议是能用常量存储的就用常量存储,以减少内存的消耗。

 

3.IO流中的错误关闭或不标准关闭导致问题

先来看一段代码吧:

<span style="font-size:18px;">import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileDemo {
	public static void main(String[] args) {
		FileOutputStream fos = null;
		BufferedOutputStream bos = null;
		byte[] b = new byte[1024];
		try {
			fos = new FileOutputStream("1.txt");
			bos = new BufferedOutputStream(fos);
			bos.write(b, 0, b.length);
			bos.close();
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}</span>


 

很简单的IO操作的一小段代码,但是这个存在一些隐患,在IO流使用以后需要关闭,关闭的时候有时候为了简便所以直接在try-catch块中直接关闭,这时候就会存在隐患,当在关流之前的操作出现错误时候,这时候关闭的方法就不会再执行,所以这个关闭方法不是万全之策,所以需要改成这样,这样就保证了关闭的目的:

<span style="font-size:18px;">import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileDemo {
	public static void main(String[] args) {
		FileOutputStream fos = null;
		BufferedOutputStream bos = null;
		byte[] b = new byte[1024];
		try {
			fos = new FileOutputStream("1.txt");
			bos = new BufferedOutputStream(fos);
			bos.write(b, 0, b.length);

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 
		
		finally {
			if (bos != null) {
				try {
					bos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}</span>


4.使用第三方库时候需要注意点

1.使用第三方库的时候,首先需要注意有些第三方库或者框架在使用的时候需要你的签名,当使用SHA1签名的时候需要注意,在你未为你的APP打包发布的时候(即未签名打包状态)SHA1值与签名后的SHA1值有可能不一样,所以需要注意下,比如在使用百度地图API的时候,就有这个问题(不知道你们有没有这个问题,但是需要注意下)

2.当时用库文件的时候,有可能出现:明明导入到eclipse(我用的eclipse,所以以eclipse为准)中,然后正确导入库的时候,工程文件还是有问题,经检查后是库文件没有导入,导入框(properties of 你的项目 ---- Android路径)中显示导入的库是红叉,这个问题是由于你的库文件,和你的工程文件不在同一个路径下,所以才会出现这种问题,解决方法就是将库文件与工程文件放在同一个文件中,这样就能解决了。

3.有些开发者想引用库文件的jar包,这样比较方便,但是有些库文件需要注意,有时候可能做成jar包的时候,引入以后可能代码不会报错,但是库文件的jar包中有可能少些东西,在运行程序的时候就会发现,是那个库文件有问题。

 

5.为app打包时候出现下面的对话框


这是因为app打包的时候会检查你程序中的警告等等,所以会限制下,解决办法上面写的很清楚了,右击项目properties---Android Lint Preferences--Configure....----Run full error check...

Run full 勾选掉就ok

 

好了就说那么多吧,以后还会再次总结的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值