无法在android studio中加载任何外部字体

时间:2017-09-08 18:36:49

标签: android fonts

更新

我创建了一个资产文件夹,里面是一个century.ttf文件。首先我得到了错误的编码问题,但在File was loaded in the wrong encoding 'UTF-8' while pasting new font to Assets Folder in Android Studio之后我按照这些步骤操作后,错误消息消失了。 然后我写了我的MainActivity:

第四次更新:

Typeface typeface = Typeface.createFromAsset(getAssets(),"century.ttf");
Text.setTypeface(typrface);

然后我的应用突然停止了。

第四次更新:

09-08 20:17:54.742 1266-1301/? E/cutils: Failed to mkdir(/data/system/users/0): No such file or directory
09-08 20:17:54.742 1266-1301/? E/vold: Failed to prepare /data/system/users/0: No such file or directory
09-08 20:17:54.743 1266-1301/? E/vold: Failed to prepare user 0 storage
09-08 20:17:54.822 1266-1314/? E/Cryptfs: Bad magic for real block device /dev/block/vdd
09-08 20:17:54.742 1266-1301/? E/cutils: Failed to mkdir(/data/system/users/0): No such file or directory
09-08 20:17:54.742 1266-1301/? E/vold: Failed to prepare /data/system/users/0: No such file or directory
09-08 20:17:54.743 1266-1301/? E/vold: Failed to prepare user 0 storage
09-08 20:17:54.822 1266-1314/? E/Cryptfs: Bad magic for real block device /dev/block/vdd
09-08 20:17:56.997 1310-1310/? E/libEGL: load_driver(/system/lib/egl/libGLES_emulation.so): dlopen failed: library "/system/lib/egl/libGLES_emulation.so" not found
09-08 20:17:57.012 1266-1301/? E/vold: Failed to chmod /data/system_ce/0: No such file or directory
09-08 20:17:57.012 1266-1301/? E/vold: Failed to chmod /data/misc_ce/0: No such file or directory
09-08 20:17:57.012 1266-1301/? E/vold: Failed to chmod /data/media/0: No such file or directory
09-08 20:17:57.048 1310-1310/? E/SurfaceFlinger: hwcomposer module not found
09-08 20:17:57.851 1310-1326/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
09-08 20:17:57.853 1340-1345/? E/libEGL: load_driver(/system/lib/egl/libGLES_emulation.so): dlopen failed: library "/system/lib/egl/libGLES_emulation.so" not found
09-08 20:18:05.693 1266-1301/? E/vold: Failed to chmod /data/system_ce/0: No such file or directory
09-08 20:18:05.693 1266-1301/? E/vold: Failed to chmod /data/misc_ce/0: No such file or directory
09-08 20:18:05.693 1266-1301/? E/vold: Failed to chmod /data/media/0: No such file or directory
09-08 20:18:05.928 1405-1405/? E/cutils: Failed to read /data/.layout_version: No such file or directory

                                         [ 09-08 20:18:05.928  1405: 1405 D/         ]
                                         Assuming that device has multi-user storage layout; upgrade no longer supported

                                         [ 09-08 20:18:05.928  1405: 1405 D/         ]
                                         Upgrading to /data/misc/user directories
09-08 20:18:05.968 1416-1416/? E/perfprofd: unable to open configuration file /data/data/com.google.android.gms/files/perfprofd.conf
09-08 20:18:06.123 1403-1403/? E/EmulatedCamera_Factory: 2 cameras are being emulated. 0 of them are fake cameras.
09-08 20:18:06.123 1403-1403/? E/EmulatedCamera_HotplugThread: createFileIfNotExists: Could not create file '/data/misc/media/emulator.camera.hotplug.0', error: 'Permission denied' (13)
09-08 20:18:06.123 1403-1403/? E/EmulatedCamera_HotplugThread: createFileIfNotExists: Could not create file '/data/misc/media/emulator.camera.hotplug.1', error: 'Permission denied' (13)
09-08 20:18:06.123 1403-1403/? E/CameraService: setUpVendorTags: Vendor tag operations not fully defined. Ignoring definitions.
09-08 20:18:06.204 1402-1402/? E/RadioService: couldn't load radio module radio.primary (No such file or directory)
09-08 20:18:06.204 1402-1402/? E/SoundTriggerHwService: couldn't load sound trigger module sound_trigger.primary (No such file or directory)
09-08 20:18:07.021 1534-1534/? E/ipv6proxy: Failed to forward Neighbor Solicitation for fe80::5c01:45ff:fe1b:e65c from radio0-peer to eth0: Cannot assign requested address
09-08 20:18:07.039 1544-1544/? E/hostapd: Configuration file: /system/etc/simulated_hostapd.conf
09-08 20:18:07.308 1401-1401/? E/memtrack: Couldn't load memtrack module (No such file or directory)
09-08 20:18:07.308 1401-1401/? E/android.os.Debug: failed to load memtrack module: -2
09-08 20:18:07.695 1401-1401/? E/Typeface: Error mapping font file /system/fonts/DroidSansFallback.ttf
09-08 20:18:08.021 1534-1534/? E/ipv6proxy: Failed to forward Router Advertisement from eth0 to wlan1: Network is unreachable
09-08 20:18:08.021 1534-1534/? E/ipv6proxy: Failed to forward Router Advertisement from eth0 to radio0-peer: Cannot assign requested address
09-08 20:18:08.231 1411-1411/? E/Netd: cannot find interface dummy0
09-08 20:18:08.398 1401-1401/? E/libEGL: load_driver(/system/lib/egl/libGLES_emulation.so): dlopen failed: library "/system/lib/egl/libGLES_emulation.so" not found
09-08 20:18:08.423 1401-1401/? E/Hyphenator: error loading hyphenation /system/usr/hyphen-data/hyph-as.hyb
                                                                                            java.io.FileNotFoundException: /system/usr/hyphen-data/hyph-nb.hyb: open failed: ENOENT (No such file or directory)
                                                 at libcore.io.IoBridge.open(IoBridge.java:455)
                                                 at java.io.RandomAccessFile.<init>(RandomAccessFile.java:247)
                                                 at android.text.Hyphenator.loadHyphenator(Hyphenator.java:118)
                                                 at android.text.Hyphenator.init(Hyphenator.java:224)
                                                 at com.android.internal.os.ZygoteInit.preloadTextResources(ZygoteInit.java:252)
                                                 at com.android.internal.os.ZygoteInit.preload(ZygoteInit.java:206)
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:743)
                                              Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
                                                 at libcore.io.Posix.open(Native Method)
                                                 at libcore.io.BlockGuardOs.open(BlockGuardOs.java:187)
                                                 at libcore.io.IoBridge.open(IoBridge.java:441)
                                                 at java.io.RandomAccessFile.<init>(RandomAccessFile.java:247) 
                                                 at android.text.Hyphenator.loadHyphenator(Hyphenator.java:118) 
                                                 at android.text.Hyphenator.init(Hyphenator.java:224) 
                                                 at com.android.internal.os.ZygoteInit.preloadTextResources(ZygoteInit.java:252) 
                                                 at com.android.internal.os.ZygoteInit.preload(ZygoteInit.java:206) 
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:743) 
09-08 20:18:08.430 1401-1401/? E/Hyphenator: error loading hyphenation /system/usr/hyphen-data/hyph-nn.hyb
                                             java.io.FileNotFoundException: /system/usr/hyphen-data/hyph-nn.hyb: open failed: ENOENT (No such file or directory)
                                                 at libcore.io.IoBridge.open(IoBridge.java:455)
                                                 at java.io.RandomAccessFile.<init>(RandomAccessFile.java:247)
                                                 at android.text.Hyphenator.loadHyphenator(Hyphenator.java:118)
                                                 at android.text.Hyphenator.init(Hyphenator.java:224)
                                                 at com.android.internal.os.ZygoteInit.preloadTextResources(ZygoteInit.java:252)
                                                 at com.android.internal.os.ZygoteInit.preload(ZygoteInit.java:206)
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:743)
                                              Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
                                                 at libcore.io.Posix.open(Native Method)
                                                 at libcore.io.BlockGuardOs.open(BlockGuardOs.java:187)
                                                 at libcore.io.IoBridge.open(IoBridge.java:441)
                                                 at java.io.RandomAccessFile.<init>(RandomAccessFile.java:247) 
                                                 at android.text.Hyphenator.loadHyphenator(Hyphenator.java:118) 
                                                 at android.text.Hyphenator.init(Hyphenator.java:224) 
                                                 at com.android.internal.os.ZygoteInit.preloadTextResources(ZygoteInit.java:252) 
                                                 at com.android.internal.os.ZygoteInit.preload(ZygoteInit.java:206) 
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:743) 
09-08 20:18:08.430 1401-1401/? E/Hyphenator: error loading hyphenation /system/usr/hyphen-data/hyph-or.hyb
                                             java.io.FileNotFoundException: /system/usr/hyphen-data/hyph-or.hyb: open failed: ENOENT (No such file or directory)
                                                 at libcore.io.IoBridge.open(IoBridge.java:455)
                                                 at java.io.RandomAccessFile.<init>(RandomAccessFile.java:247)
                                                 at android.text.Hyphenator.loadHyphenator(Hyphenator.java:118)
                                                 at android.text.Hyphenator.init(Hyphenator.java:224)
                                                 at com.android.internal.os.ZygoteInit.preloadTextResources(ZygoteInit.java:252)
                                                 at com.android.internal.os.ZygoteInit.preload(ZygoteInit.java:206)
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:743)
                                              Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
                                                 at libcore.io.Posix.open(Native Method)
                                                 at libcore.io.BlockGuardOs.open(BlockGuardOs.java:187)
                                                 at libcore.io.IoBridge.open(IoBridge.java:441)
                                                 at java.io.RandomAccessFile.<init>(RandomAccessFile.java:247) 
                                                 at android.text.Hyphenator.loadHyphenator(Hyphenator.java:118) 
                                                 at android.text.Hyphenator.init(Hyphenator.java:224) 
                                                 at com.android.internal.os.ZygoteInit.preloadTextResources(ZygoteInit.java:252) 
                                                 at com.android.internal.os.ZygoteInit.preload(ZygoteInit.java:206) 
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:743) 
09-08 20:18:08.430 1401-1401/? E/Hyphenator: error loading hyphenation /system/usr/hyphen-data/hyph-pa.hyb
                                             java.io.FileNotFoundException: /system/usr/hyphen-data/hyph-pa.hyb: open failed: ENOENT (No such file or directory)
                                                 at libcore.io.IoBridge.open(IoBridge.java:455)
                                                 at java.io.RandomAccessFile.<init>(RandomAccessFile.java:247)
                                                 at android.text.Hyphenator.loadHyphenator(Hyphenator.java:118)
                                                 at android.text.Hyphenator.init(Hyphenator.java:224)
                                                 at com.android.internal.os.ZygoteInit.preloadTextResources(ZygoteInit.java:252)
                                                 at com.android.internal.os.ZygoteInit.preload(ZygoteInit.java:206)
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:743)
                                              Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
                                                 at libcore.io.Posix.open(Native Method)
                                                 at libcore.io.BlockGuardOs.open(BlockGuardOs.java:187)
                                                 at libcore.io.IoBridge.open(IoBridge.java:441)
                                                 at java.io.RandomAccessFile.<init>(RandomAccessFile.java:247) 
                                                 at android.text.Hyphenator.loadHyphenator(Hyphenator.java:118) 
                                                 at android.text.Hyphenator.init(Hyphenator.java:224) 
                                                 at com.android.internal.os.ZygoteInit.preloadTextResources(ZygoteInit.java:252) 
                                                 at com.android.internal.os.ZygoteInit.preload(ZygoteInit.java:206) 
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:743) 
09-08 20:18:08.431 1401-1401/? E/Hyphenator: error loading hyphenation /system/usr/hyphen-data/hyph-pt.hyb
                                                                               Process: com.example.fdai3744.t_online20, PID: 3050
                                                                               java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fdai3744.t_online20/com.example.fdai3744.t_online20.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setTypeface(android.graphics.Typeface)' on a null object reference
                                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
                                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                                                                                   at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                   at android.os.Looper.loop(Looper.java:154)
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                                                Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setTypeface(android.graphics.Typeface)' on a null object reference
                                                                                   at com.example.fdai3744.t_online20.MainActivity.onCreate(MainActivity.java:25)
                                                                                   at android.app.Activity.performCreate(Activity.java:6679)
                                                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
                                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
                                                                                   at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                   at android.os.Looper.loop(Looper.java:154) 
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:6119) 
                                                                                   at java.lang.reflect.Method.invoke(Native Method) 
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 

第四次更新:

enter image description here

4 个答案:

答案 0 :(得分:2)

按照以下链接中的说明进入清单文件并在应用程序xml标记下添加android:name =“the-below-implementation”

https://github.com/chrisjenx/Calligraphy

答案 1 :(得分:1)

考虑到你在资产文件夹中使用ttf字体,生成这样的字体。

Typeface typeface = Typeface.createFromAsset(getAssets(),"YourFontName.ttf"); 
Text.setTypeface(typrface);

答案 2 :(得分:1)

您的代码正在请求font/century(请参阅:Caused by: java.lang.RuntimeException: Font asset not found font/century)。实际资产是font/century.tff(根据您的评论),或者可能是font/century.ttf(这可能是TrueType字体更可能的文件扩展名)。

您提供的字体路径需要与assets/中的实际路径匹配,包括文件扩展名。

答案 3 :(得分:1)

尝试将ttf文件放入&#34;字体&#34; &#34;资产&#34;内的文件夹文件夹,然后更改源路径&#34; fonts / century.ttf&#34;。我的猜测是文件没有被识别为字体文件。

这就是我在其中一个项目中设置的方法:

enter image description here