2015-04-02 88 views
1

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) 
+0

當它崩潰是什麼'logcat'說? – DigitalNinja 2015-04-02 20:00:35

+0

重新加載頁面。 – Ucar 2015-04-02 20:01:47

+1

看看[這](http://stackoverflow.com/questions/12766930/native-typeface-cannot-be-made-only-for-some-people)和[this](http://stackoverflow.com/ questions/20049643/runtimeexception-native-typeface-not-be-made) – DigitalNinja 2015-04-02 20:04:42

回答

-1

通常你應該做這樣的事情:

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

這與問題中的內容有什麼不同?它添加了getContext(),但代碼位於已經是上下文的Activity中。這並沒有回答這個問題。 – 2015-04-02 20:11:01

+0

由於該解決方案,解決了幾個案例。 你可以在這裏查看一個例子:約getAssets的其他情況下()] [1] 或者你可以有文件:[文件] [2] [1]:http://stackoverflow.com/問題/ 18436703/android-typeface-createfromasset [2]:http://developer.android.com/reference/android/app/Dialog.html#getContext – 2015-04-02 20:15:15

+0

第一個鏈接是關於getAssets()在View上不可用,第二個是指向Dialog的api文檔的鏈接。這兩個都不適用,發佈的代碼在_Activity_ – 2015-04-02 20:24:14

相關問題