2014-03-25 37 views
-1

我试图执行this代码到我的应用程序,并使用我的字体在assets/fonts。我没有在logcat中得到任何异常,但每次启动it.It崩溃。什么是我做错了应用程序每次启动时崩溃没有任何错误

这里是mycode的

public class appp extends Application { 
     private Typeface normalFont; 
     private Typeface boldFont; 



     // -- Fonts -- // 
     public void setTypeface(TextView textView) { 
      if(textView != null) { 
       if(textView.getTypeface() != null && textView.getTypeface().isBold()) { 
        textView.setTypeface(getBoldFont()); 
       } else { 
        textView.setTypeface(getNormalFont()); 
       } 
      } 
     } 

     private Typeface getNormalFont() { 
      if(normalFont == null) { 
       normalFont = Typeface.createFromAsset(getAssets(),"fonts/SERSAL.ttf"); 
      } 
      return this.normalFont; 
     } 

     private Typeface getBoldFont() { 
      if(boldFont == null) { 
       boldFont = Typeface.createFromAsset(getAssets(),"fonts/Cheboyga.ttf"); 
      } 
      return this.boldFont; 
     } 

} 

,这是主类

appp application = (appp) getApplication(); 
     TextView myTextView = (TextView) findViewById(R.id.my_textview); 
     application.setTypeface(myTextView); 
+0

您正在使用自定义应用程序,所以已经在您的清单中展示了这个** android:name =“your packagename.appp”**。 –

+0

Withouy任何额外的类,你可以简单地使用这样的自定义字体 TextView myTextView =(TextView)findViewById(R.id.my_textview); Typeface tf = Typeface.createFromAsset(getAssets(),“fonts/SERSAL.ttf”); \t \t myTextView .setTypeface(tf); –

+0

你能看到我的答案吗? –

回答

0

您正在使用定制应用程序,让你拥有delcare这个

android:name="your packagename.appp"在您的清单中

0

可以设定型脸是这样的:

Typeface font; 
font = Typeface.createFromAsset(getActivity().getAssets(), "xxx.ttf"); 
TextView myTextView = (TextView) findViewById(R.id.my_textview); 
myTextView.setTypeface(font); 

可以通过这个应该工作,

0

每个应用程序都有用独特的包名自己的清单文件。

您应该对每个应用程序具有唯一性。

喜欢APP1,你可能有 -

package="com.package.app1" 

要访问包名使用 -

public String getAppName() 
{ 
    final PackageManager pm = getApplicationContext().getPackageManager(); 


    ApplicationInfo ai; 
    try { 
     ai = pm.getApplicationInfo(getApplicationContext().getPackageName(), 0); 
    } catch (final NameNotFoundException e) { 
     ai = null; 
    } 
    final String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)"); 

    return applicationName; 
} 

同样适用于应用程序标签..

<application android:name="com.package.app1.app"> ... </application>