【问题标题】:custom font null pointer exception自定义字体空指针异常
【发布时间】:2014-10-19 13:36:01
【问题描述】:

我已经尝试了所有我能找到的方法来让自定义字体正常工作,但我不断收到“空指针异常”。这是我所做的:

  1. 在“assets”目录中创建文件夹“fonts”。

  2. 从此处下载 Google 的“Roboto”字体:http://www.fontspace.com/google-android/roboto

  3. 将文件“Roboto-Regular.ttf”重命名为“r.ttf”

  4. 上传到 assets/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”的弹出窗口。

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

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

编辑: 异常由此引发: txt.setTypeface(font) 上的 java.lang.NullPointerException;

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">  

【问题讨论】:

  • 你确定你有那个 id,并且在你尝试访问它时它已经被夸大了吗?
  • 我有那个ID,我可以按住ctrl+click_on_id,它会把我带到xml文件。不确定“在您尝试访问它时已经膨胀”是什么意思,我在“onCreate”事件中有此代码
  • 你有 setContentView 在正确的 xml 上吗?
  • 我有 setContentView(R.layout.activity_main);谢谢!我用布局代码更新了我的问题。

标签: android fonts


【解决方案1】:

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/layout/row.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"> 

【讨论】:

  • 非常感谢您的回答。我收到 Eclipse 错误:XML 文档结构必须在同一个实体内开始和结束。我将名称更改为我的项目。
【解决方案2】:

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

确定后,你的字体在assets/fonts

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

【讨论】:

  • 我的代码完全一样,我在这一行遇到了异常:myTextView.setTypeface(myTypeface);
  • 您是否尝试过清理并构建您的项目
  • 我无法发布错误堆栈,因为我正在手机上上传/安装 apk,模拟器不工作,只显示黑屏。
  • 先生使用这个模拟器,它又好又快,这是链接genymotion.com
  • java.lang.NullPointerException
猜你喜欢
  • 2013-03-04
  • 2018-04-28
  • 1970-01-01
  • 2012-08-01
  • 2013-04-13
  • 1970-01-01
  • 1970-01-01
  • 2019-10-16
相关资源
最近更新 更多