2014-10-19 54 views
0

我尝试了一切可以找到自定义字体的工作,但我不断收到'空指针异常。以下是我所做的:自定义字体空指针异常

  1. 在'资产'目录中创建文件夹'字体'。

  2. 下载谷歌的 '的Roboto' 从这里字体:http://www.fontspace.com/google-android/roboto

  3. 重命名的文件 '的Roboto-Regular.ttf' 到 'r.ttf'

  4. 上传到资产/ fonts /目录中

  5. 做项目 - >清洁

现在我有这样的代码:

  try { 
       fileList = am.list("fonts"); 

       if (fileList != null) 
       { 
        for (int i = 0;i<fileList.length;i++) 
        { 
         Toast.makeText(getApplicationContext(), fileList[i] + "!", Toast.LENGTH_LONG).show(); 
        } 
       } 
      } catch (IOException e) { 
        Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show(); 
      } 


     try { 

     Typeface font = Typeface.createFromAsset(getAssets(), "fonts/r.ttf"); 

     TextView txt = (TextView) findViewById(R.id.rowTextView); 
     txt.setTypeface(font); 
     } 
     catch(Exception e) 
     { 
      Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show(); 
     } 

代码的第一部分列出'字体'文件夹中的所有项目,我得到一个弹出窗口显示'r.ttf'。

代码的第二部分尝试加载字体,我从那里用'空指针异常'得到一个弹出窗口。

任何想法我做错了什么?谢谢!

编辑: 该异常是在由该抛出: 显示java.lang.NullPointerException上txt.setTypeface(字体);

EDIT2:

这是我activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#000000" 

android:paddingBottom="@dimen/activity_vertical_margin" 

android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="com.kb.klog.MainActivity" > 

<ListView 
    android:id="@+id/listView1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

</ListView> 

</RelativeLayout> 

这是我想要的字体更改到行:/res/layout/row.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/rowTextView" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:padding="5dp" 
android:textColor="#aaa" 
android:textSize="18sp"> 

+0

您确定您有该ID,并且在您尝试访问它的位置时它已经膨胀了吗? – helleye 2014-10-19 13:52:13

+0

我有这个ID,我可以按住ctrl + click_on_id,它将我带到xml文件。不确定你在'尝试访问它时已经膨胀了'的意思,我在'onCreate'事件中有这个代码 – user3578847 2014-10-19 13:55:37

+0

你有** setContentView **正确的xml吗? – helleye 2014-10-19 13:58:53

回答

3

TXT值为null,因为该行没有被当时充气字体。 我会建议使用这个答案https://stackoverflow.com/a/9035924/2160877
一般来说,你应该创建自己的扩展TextView的类。然后你在xml中引用那个类,这就是全部。

如果该链接被删除,这里是你的类的代码(如从链接采取了一些修改您的情况):

package com.yourPackageNameGoesHere; 

import android.content.Context; 
import android.graphics.Typeface; 
import android.util.AttributeSet; 
import android.widget.TextView; 

public class MyTextView extends TextView { 

    public MyTextView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     init(); 
    } 

    public MyTextView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(); 
    } 

    public MyTextView(Context context) { 
     super(context); 
     init(); 
    } 

    private void init() { 
     Typeface tf = Typeface.createFromAsset(getContext().getAssets(), 
               "r.ttf"); 
     setTypeface(tf); 
    } 

} 

然后你/ RES /布局/行。xml将如下所示:

<com.yourPackageNameGoesHere.MyTextView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/rowTextView" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:padding="5dp" 
android:textColor="#aaa" 
android:textSize="18sp"> 
+0

非常感谢您的回答。我遇到了一个Eclipse错误:XML文档结构必须在同一个实体中开始和结束。我将名称更改为我的项目。 – user3578847 2014-10-19 14:30:24

+0

nvm我愚蠢的初学者错误。它工作完美,谢谢sooooo多! – user3578847 2014-10-19 14:37:50

0

如果您需要自定义字体只需使用此代码

确保你把后,你在assets/fonts

 Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/myFont.ttf"); 
     TextView myTextView = (TextView)findViewById(R.id.myTextView); 
     myTextView.setTypeface(myTypeface); 
+0

我的代码完全一样的东西,我得到这一行的异常:myTextView.setTypeface(myTypeface); – user3578847 2014-10-19 13:49:57

+0

您是否尝试清洁并构建您的项目 – 2014-10-19 13:51:51

+0

如果您更新错误堆栈的问题 – 2014-10-19 13:52:19