最近我targetSdkVersion 26
从targetSdkVersion 21
. 这样做会出现几个问题。
我已经写了一个类并运行但开始抛出新的 RuntimeException(e)
代码如下
package com.xxxxxxxxxx.android.webapp.util; import android.content.Context; import android.telephony.TelephonyManager; import com.xxxxxxxxxx.android.webapp.BuildConfig; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.RandomAccessFile; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class Installation { private static String sID = null; private static final String INSTALLATION = "INSTALLATION"; public synchronized static String id(Context context) { if (sID == null) { File installation = new File(context.getFilesDir(), INSTALLATION); try { if (!installation.exists()) writeInstallationFile(installation,context); sID = readInstallationFile(installation,context); } catch (Exception e) { throw new RuntimeException(e); } } return sID; } private static String readInstallationFile(File installation,Context context) throws IOException { RandomAccessFile f = new RandomAccessFile(installation, "r"); byte[] bytes = new byte[(int) f.length()]; f.readFully(bytes); f.close(); return new String(bytes); } private static void writeInstallationFile(File installation,Context context) throws IOException { FileOutputStream out = new FileOutputStream(installation); String id = getUniqueID(context); //UUID.randomUUID().toString(); out.write(id.getBytes()); out.close(); } private static String getUniqueID(Context context){ String android_id = android.provider.Settings.Secure.getString( context.getContentResolver(), android.provider.Settings.Secure.ANDROID_ID); String eid = null; try { eid = Encryptor.encrypt("gignoGawaAppli", android_id ); if (BuildConfig.uidEncryptionMd5) { eid = md5(eid); } }catch (Exception e){}; return eid; } public static String md5(String s) { try { // Create MD5 Hash MessageDigest digest = java.security.MessageDigest.getInstance("MD5"); digest.update(s.getBytes()); byte messageDigest[] = digest.digest(); // Create Hex String StringBuffer hexString = new StringBuffer(); for (int i = 0; i < messageDigest.length; i++) { if ((0xff & messageDigest[i]) < 0x10) { hexString.append("0" + Integer.toHexString((0xFF & messageDigest[i]))); } else { hexString.append(Integer.toHexString(0xFF & messageDigest[i])); } } return hexString.toString(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return ""; } }
像这样出现在控制台上
原因:java.lang.RuntimeException:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法 byte[] java.lang.String.getBytes()
无法获取ANDROID_ID
当我的项目
targetSdkVersion's 21
一切正常时。
请帮忙,非常感谢。