Android 程序在使用 getAssets() 时崩溃

发布于 2024-10-21 07:17:47 字数 2632 浏览 2 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

┊风居住的梦幻卍 2024-10-28 07:17:47

您好,

请检查字体名称大小写(大写字符)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.

紫罗兰の梦幻 2024-10-28 07:17:47

我认为您在模块的 AndroidManifest.xml 文件和项目的 AndroidManifest.xml 中使用了冲突的 Android API。如果您的文件位于模块中,请确保它们保持不变。

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mrwujay.cascade"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="23" />
    <application/>
</manifest>

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.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mrwujay.cascade"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="23" />
    <application/>
</manifest>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文