gpt4 book ai didi

java - "Unfortunately (app name) hast stopped"的字体原因

转载 作者:行者123 更新时间:2023-12-01 06:13:38 26 4
gpt4 key购买 nike

BaseGameActivity.java中,我有一些可以轻松创建字体的方法。但不幸的是,当涉及到创建字体时,应用程序停止了。代码不存在语法错误。

BaseGameActivity.java

public class BaseGameActivity extends Activity {

private Map<String,Typeface> typefaces = new HashMap<String,Typeface>();

protected void addTypeface(String name) {
Typeface typeface = Typeface.createFromAsset(getAssets(), name+".tff");
typefaces.put(name, typeface);
}

protected void setTypeface(TextView v, String typefaceName) {
Typeface t = typefaces.get(typefaceName);
if(t != null) {
v.setTypeface(t);
}
}
}

MainAcitivity.java

public class MainActivity extends BaseGameActivity {

private static final String FONTNAME = "airmole";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addTypeface(FONTNAME);
setTypeface((TextView) findViewById(R.id.title), FONTNAME);
}
}

文件 airmole.tff 位于 assets 文件夹中。

logcat

Caused by: java.lang.RuntimeException: native typeface cannot be made
at android.graphics.Typeface.<init>(Typeface.java:319)
at android.graphics.Typeface.createFromAsset(Typeface.java:293)
at de.ucarweb.tools.BaseGameActivity.addTypeface(BaseGameActivity.java:23)
at de.ucarweb.silverball.MainActivity.onCreate(MainActivity.java:28)
at android.app.Activity.performCreate(Activity.java:5372)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)

            

at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
            at android.app.ActivityThread.access$700(ActivityThread.java:159)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:176)
            at android.app.ActivityThread.main(ActivityThread.java:5419)
            at java.lang.reflect.Method.invokeNative(Native Method)

最佳答案

通常你应该这样做:

Typeface typeface = Typeface.createFromAsset(getContext().getAssets(), name+".tff");

关于java - "Unfortunately (app name) hast stopped"的字体原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29421399/

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