Android 程序在使用 getAssets() 时崩溃
我有一个 MyActivity 类,它扩展了活动。这个类本质上只是为了保存我需要的一些额外方法,它还保存我将在整个应用程序中使用的字体。例如,我有一个扩展 MyActivity 的活动“HomeScreen”。但是,当我尝试使用以下方法实例化字体时:
lavi = Typeface.createFromAsset(getAssets(), "fonts/Lavi.ttf");
由于 getAssets() 方法中似乎出现空指针,程序崩溃了。有谁知道为什么会发生这种情况以及如何解决这个问题?
编辑: Logcat:
03-07 23:47:16.389:错误/AndroidRuntime(9432):致命异常:主要 03-07 23:47:16.389:错误/AndroidRuntime(9432):java.lang.RuntimeException:无法实例化活动ComponentInfo {android.PhysicsEngine4 / android.PhysicsEngine4.HomeScreen}:java.lang.NullPointerException 03-07 23:47:16.389:错误/AndroidRuntime(9432):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585) 03-07 23:47:16.389:错误/AndroidRuntime(9432):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 03-07 23:47:16.389:错误/AndroidRuntime(9432):在android.app.ActivityThread.access $ 2300(ActivityThread.java:125) 03-07 23:47:16.389:错误/AndroidRuntime(9432):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2033) 03-07 23:47:16.389:错误/AndroidRuntime(9432):在android.os.Handler.dispatchMessage(Handler.java:99) 03-07 23:47:16.389:错误/AndroidRuntime(9432):在android.os.Looper.loop(Looper.java:123) 03-07 23:47:16.389:错误/AndroidRuntime(9432):在android.app.ActivityThread.main(ActivityThread.java:4627) 03-07 23:47:16.389:错误/AndroidRuntime(9432):在java.lang.reflect.Method.invokeNative(本机方法) 03-07 23:47:16.389:错误/AndroidRuntime(9432):在java.lang.reflect.Method.invoke(Method.java:521) 03-07 23:47:16.389:错误/AndroidRuntime(9432):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868) 03-07 23:47:16.389:错误/AndroidRuntime(9432):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 03-07 23:47:16.389:错误/AndroidRuntime(9432):在dalvik.system.NativeStart.main(本机方法) 03-07 23:47:16.389:错误/AndroidRuntime(9432):引起:java.lang.NullPointerException 03-07 23:47:16.389:错误/AndroidRuntime(9432):在android.content.ContextWrapper.getAssets(ContextWrapper.java:74) 03-07 23:47:16.389:错误/AndroidRuntime(9432):在android.PhysicsEngine4.MyActivity。(MyActivity.java:19) 03-07 23:47:16.389:错误/AndroidRuntime(9432):在android.PhysicsEngine4.HomeScreen。(HomeScreen.java:12) 03-07 23:47:16.389:错误/AndroidRuntime(9432):在java.lang.Class.newInstanceImpl(本机方法) 03-07 23:47:16.389:错误/AndroidRuntime(9432):在java.lang.Class.newInstance(Class.java:1429) 03-07 23:47:16.389:错误/AndroidRuntime(9432):在android.app.Instrumentation.newActivity(Instrumentation.java:1021) 03-07 23:47:16.389:错误/AndroidRuntime(9432):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577) 03-07 23:47:16.389: 错误/AndroidRuntime(9432): ... 11 更多
I have a class MyActivity which extends activity. This class is essentially present only to hold a few extra methods that I will need and it also holds the typeface that i will be using for the entire app. So, for instance, I have an activity "HomeScreen" that extends MyActivity. However, when I try to instantiate the typeface using:
lavi = Typeface.createFromAsset(getAssets(), "fonts/Lavi.ttf");
the program crashes due to a nullpointer that seems to be occurring at the getAssets() method. Does anyone know why this is happening and how to fix the problem?
EDIT:
Logcat:
03-07 23:47:16.389: ERROR/AndroidRuntime(9432): FATAL EXCEPTION: main
03-07 23:47:16.389: ERROR/AndroidRuntime(9432): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{android.PhysicsEngine4/android.PhysicsEngine4.HomeScreen}: java.lang.NullPointerException
03-07 23:47:16.389: ERROR/AndroidRuntime(9432): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
03-07 23:47:16.389: ERROR/AndroidRuntime(9432): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-07 23:47:16.389: ERROR/AndroidRuntime(9432): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-07 23:47:16.389: ERROR/AndroidRuntime(9432): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-07 23:47:16.389: ERROR/AndroidRuntime(9432): at android.os.Handler.dispatchMessage(Handler.java:99)
03-07 23:47:16.389: ERROR/AndroidRuntime(9432): at android.os.Looper.loop(Looper.java:123)
03-07 23:47:16.389: ERROR/AndroidRuntime(9432): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-07 23:47:16.389: ERROR/AndroidRuntime(9432): at java.lang.reflect.Method.invokeNative(Native Method)
03-07 23:47:16.389: ERROR/AndroidRuntime(9432): at java.lang.reflect.Method.invoke(Method.java:521)
03-07 23:47:16.389: ERROR/AndroidRuntime(9432): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-07 23:47:16.389: ERROR/AndroidRuntime(9432): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-07 23:47:16.389: ERROR/AndroidRuntime(9432): at dalvik.system.NativeStart.main(Native Method)
03-07 23:47:16.389: ERROR/AndroidRuntime(9432): Caused by: java.lang.NullPointerException
03-07 23:47:16.389: ERROR/AndroidRuntime(9432): at android.content.ContextWrapper.getAssets(ContextWrapper.java:74)
03-07 23:47:16.389: ERROR/AndroidRuntime(9432): at android.PhysicsEngine4.MyActivity.(MyActivity.java:19)
03-07 23:47:16.389: ERROR/AndroidRuntime(9432): at android.PhysicsEngine4.HomeScreen.(HomeScreen.java:12)
03-07 23:47:16.389: ERROR/AndroidRuntime(9432): at java.lang.Class.newInstanceImpl(Native Method)
03-07 23:47:16.389: ERROR/AndroidRuntime(9432): at java.lang.Class.newInstance(Class.java:1429)
03-07 23:47:16.389: ERROR/AndroidRuntime(9432): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
03-07 23:47:16.389: ERROR/AndroidRuntime(9432): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
03-07 23:47:16.389: ERROR/AndroidRuntime(9432): ... 11 more
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您好,
请检查字体名称大小写(大写字符)Lavi.ttf 或 lavi.ttf 并尝试在代码中使用与放入资产文件夹中相同的代码。
HI,
plz check font name case (capitalized characters) Lavi.ttf or lavi.ttf and try to use same in code as you have put in assets folder.
我认为您在模块的 AndroidManifest.xml 文件和项目的 AndroidManifest.xml 中使用了冲突的 Android API。如果您的文件位于模块中,请确保它们保持不变。
I think you use the conflicting Android API in the module's AndroidManifest.xml file and project's AndroidManifest.xml. make sure them stay the same if you have the file in modules.