gpt4 book ai didi

java - 尝试更改字体时出现 Digitalclock 运行时错误

转载 作者:行者123 更新时间:2023-11-30 04:16:40 25 4
gpt4 key购买 nike

我试图在 Activity 中显示数字时钟,但是当我添加更改字体的代码时,应用程序崩溃了:

注意:字体在asset文件夹中,名称正确

package asd.asasdx;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.DigitalClock;

public class ClockzActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DigitalClock digitalClock = (DigitalClock) findViewById(R.id.digitalClock);
setContentView(R.layout.main);
Typeface type = Typeface.createFromAsset(getAssets(), "DS_DIGI.TTF");
digitalClock.setTypeface(type);
}
}

和 XML:

<DigitalClock
android:id="@+id/digitalClock"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="DigitalClock"
android:textSize="70dp"
android:textColor="#00FF00"
android:gravity="center"
android:shadowColor="#00FF00"
android:shadowRadius="16" />

但是当我运行该应用程序时,它会因为运行时错误而崩溃:

03-26 19:05:49.242: E/AndroidRuntime(29310): FATAL EXCEPTION: main
03-26 19:05:49.242: E/AndroidRuntime(29310): java.lang.RuntimeException: Unable to start activity ComponentInfo{asd.asasdx/asd.asasdx.ClockzActivity}: java.lang.RuntimeException: native typeface cannot be made
03-26 19:05:49.242: E/AndroidRuntime(29310): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
03-26 19:05:49.242: E/AndroidRuntime(29310): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
03-26 19:05:49.242: E/AndroidRuntime(29310): at android.app.ActivityThread.access$600(ActivityThread.java:123)
03-26 19:05:49.242: E/AndroidRuntime(29310): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
03-26 19:05:49.242: E/AndroidRuntime(29310): at android.os.Handler.dispatchMessage(Handler.java:99)
03-26 19:05:49.242: E/AndroidRuntime(29310): at android.os.Looper.loop(Looper.java:137)
03-26 19:05:49.242: E/AndroidRuntime(29310): at android.app.ActivityThread.main(ActivityThread.java:4424)
03-26 19:05:49.242: E/AndroidRuntime(29310): at java.lang.reflect.Method.invokeNative(Native Method)
03-26 19:05:49.242: E/AndroidRuntime(29310): at java.lang.reflect.Method.invoke(Method.java:511)
03-26 19:05:49.242: E/AndroidRuntime(29310): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-26 19:05:49.242: E/AndroidRuntime(29310): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-26 19:05:49.242: E/AndroidRuntime(29310): at dalvik.system.NativeStart.main(Native Method)
03-26 19:05:49.242: E/AndroidRuntime(29310): Caused by: java.lang.RuntimeException: native typeface cannot be made
03-26 19:05:49.242: E/AndroidRuntime(29310): at android.graphics.Typeface.<init>(Typeface.java:147)
03-26 19:05:49.242: E/AndroidRuntime(29310): at android.graphics.Typeface.createFromAsset(Typeface.java:121)
03-26 19:05:49.242: E/AndroidRuntime(29310): at asd.asasdx.ClockzActivity.onCreate(ClockzActivity.java:15)
03-26 19:05:49.242: E/AndroidRuntime(29310): at android.app.Activity.performCreate(Activity.java:4465)
03-26 19:05:49.242: E/AndroidRuntime(29310): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
03-26 19:05:49.242: E/AndroidRuntime(29310): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
03-26 19:05:49.242: E/AndroidRuntime(29310): ... 11 more

有什么问题?

最佳答案

您的代码是正确的,但只有当我们遇到任何与字体相关的问题时才会出现此错误,因此请检查您的字体位置和字体名称可能不相同或字体中的任何其他问题。

关于java - 尝试更改字体时出现 Digitalclock 运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9876539/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com