在 TextView 中使用 phone 数字时违反 StrictMode

StrictMode violation when using a phone number in a TextView

在使用参数 android:autoLink="phone" 创建包含编号为 phone 的 TextView 的布局时,存在 StrictModeDiskRead 冲突。

这是使用 com.android.support:appcompat-v7:22.2.0

删除 autoLink 参数会导致违规行为不再发生。

有办法解决这个问题吗?

堆栈跟踪:

D/StrictMode﹕ StrictMode policy violation; ~duration=108 ms: android.os.StrictMode$StrictModeDiskReadViolation: policy=31 violation=2 at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1135) at libcore.io.BlockGuardOs.open(BlockGuardOs.java:106) at libcore.io.IoBridge.open(IoBridge.java:393) at java.io.RandomAccessFile.(RandomAccessFile.java:118) at java.io.RandomAccessFile.(RandomAccessFile.java:150) at java.util.zip.ZipFile.(ZipFile.java:149) at java.util.jar.JarFile.(JarFile.java:187) at libcore.net.url.JarURLConnectionImpl.openJarFile(JarURLConnectionImpl.java:136) at libcore.net.url.JarURLConnectionImpl.findJarFile(JarURLConnectionImpl.java:113) at libcore.net.url.JarURLConnectionImpl.connect(JarURLConnectionImpl.java:82) at libcore.net.url.JarURLConnectionImpl.getInputStream(JarURLConnectionImpl.java:215) at java.net.URL.openStream(URL.java:470) at java.lang.ClassLoader.getResourceAsStream(ClassLoader.java:432) at java.lang.Class.getResourceAsStream(Class.java:1037) at com.android.i18n.phonenumbers.PhoneNumberUtil.loadMetadataFromFile(PhoneNumberUtil.java:585) at com.android.i18n.phonenumbers.PhoneNumberUtil.getMetadataForRegion(PhoneNumberUtil.java:1925) at com.android.i18n.phonenumbers.PhoneNumberUtil.parseHelper(PhoneNumberUtil.java:2766) at com.android.i18n.phonenumbers.PhoneNumberUtil.parseAndKeepRawInput(PhoneNumberUtil.java:2679) at com.android.i18n.phonenumbers.PhoneNumberUtil.parseAndKeepRawInput(PhoneNumberUtil.java:2668) at com.android.i18n.phonenumbers.PhoneNumberMatcher.parseAndVerify(PhoneNumberMatcher.java:418) at com.android.i18n.phonenumbers.PhoneNumberMatcher.extractMatch(PhoneNumberMatcher.java:310) at com.android.i18n.phonenumbers.PhoneNumberMatcher.find(PhoneNumberMatcher.java:240) at com.android.i18n.phonenumbers.PhoneNumberMatcher.hasNext(PhoneNumberMatcher.java:652) at android.text.util.Linkify.gatherTelLinks(Linkify.java:454) at android.text.util.Linkify.addLinks(Linkify.java:230) at android.widget.TextView.setText(TextView.java:3749) at android.widget.TextView.setText(TextView.java:3671) at android.widget.TextView.(TextView.java:1270) at android.support.v7.widget.AppCompatTextView.(AppCompatTextView.java:44) at android.support.v7.widget.AppCompatTextView.(AppCompatTextView.java:40) at android.support.v7.internal.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:103) at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:802) at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:832) at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44) at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:172) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:684) at android.view.LayoutInflater.rInflate(LayoutInflater.java:755) at android.view.LayoutInflater.rInflate(LayoutInflater.java:758) at android.view.LayoutInflater.parseInclude(LayoutInflater.java:839) at android.view.LayoutInflater.rInflate(LayoutInflater.java:745) at android.view.LayoutInflater.rInflate(LayoutInflater.java:758) at android.view.LayoutInflater.inflate(LayoutInflater.java:492) at android.view.LayoutInflater.inflate(LayoutInflater.java:397)

编辑:我现在使用 ViewStub 按需加载此视图,因此它不会减慢应用程序的正常加载速度

堆栈跟踪看起来确实很正常。至少,它不是你的代码,它是系统。

来自the docs

But don't feel compelled to fix everything that StrictMode finds. In particular, many cases of disk access are often necessary during the normal activity lifecycle. Use StrictMode to find things you did by accident.