java - 与自定义字体相关的 Eclipse LogCat 错误

标签 java android eclipse logcat custom-font

当我启动应用程序时,我的模拟器上收到消息“不幸的是 [应用程序名称] 已停止”。

根据我的LogCat,问题是由第19行的错误引起的。

MainActivity.java 中的第 19 行是我的自定义 TextView 代码的一部分。

如果我完全删除 MainActivity.java 中的自定义 TextView 代码和 Activity_main.xml 中关联的 TextView,我不会收到错误。因此,我发现这个自定义 TextView 似乎是问题的根源。

我已尝试以下故障排除:

  • 确保 .ttf 字体文件以小写形式编写
  • 确保 .ttf 字体文件实际上是 .tff 文件
  • 将.ttf字体文件存储在assets下
  • 将 .ttf 字体文件存储在 asset 的 fonts 子文件夹下

我实际上已经研究这个简单的问题好几天了。我读过许多帖子来解决似乎与我相同的问题,但似乎没有简单的解决办法。

提前为您的帮助干杯

字体文件位于 asset/fonts/quicksandlight.ttf 下

MainActivity.java

package [package name not displayed];

import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import com.jdubbing.scissored.R;
import android.widget.TextView;
import android.graphics.Typeface;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);

    TextView txt = (TextView) findViewById(R.id.customfont);  
    Typeface font = Typeface.createFromAsset(getAssets(), "fonts/quicksandlight.ttf");                                       
    txt.setTypeface(font); 


     Thread logoTimer = new Thread() {
            public void run(){
                try{
                    int logoTimer = 0;
                    while(logoTimer < 5000){
                        sleep(100);
                        logoTimer = logoTimer +100;
                    };
                    startActivity(new Intent("com.jdubbing.CLEARSCREEN"));
                } 

                catch (InterruptedException e) {
                    e.printStackTrace();
                }

                finally{
                    finish();
                }
            }
        };

        logoTimer.start();
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
android:background="@drawable/startbackground" >

<TextView
    android:id="@+id/customfont"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="37dp"
    android:textSize="15sp" 
    android:textColor="#e5e3e3"
    android:text="@string/tell" />


</RelativeLayout>

LogCat检查最后五六行,看看第19行的错误

01-07 03:52:12.442: I/Process(2497): Sending signal. PID: 2497 SIG: 9
01-07 03:52:19.162: D/dalvikvm(2536): GC_FOR_ALLOC freed 34K, 4% free 2731K/2832K, paused 104ms, total 105ms
01-07 03:52:19.162: I/dalvikvm-heap(2536): Grow heap (frag case) to 3.312MB for 614416-byte allocation
01-07 03:52:19.232: D/dalvikvm(2536): GC_FOR_ALLOC freed <1K, 4% free 3331K/3436K, paused 59ms, total 59ms
01-07 03:52:19.412: D/AndroidRuntime(2536): Shutting down VM
01-07 03:52:19.412: W/dalvikvm(2536): threadid=1: thread exiting with uncaught exception (group=0xb4ad4b90)
01-07 03:52:19.422: E/AndroidRuntime(2536): FATAL EXCEPTION: main
01-07 03:52:19.422: E/AndroidRuntime(2536): Process: [project name not displayed], PID: 2536
01-07 03:52:19.422: E/AndroidRuntime(2536): java.lang.RuntimeException: Unable to start activity ComponentInfo{[project name not displayed]/[project name not displayed].MainActivity}: java.lang.NullPointerException
01-07 03:52:19.422: E/AndroidRuntime(2536):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
01-07 03:52:19.422: E/AndroidRuntime(2536):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
01-07 03:52:19.422: E/AndroidRuntime(2536):     at android.app.ActivityThread.access$700(ActivityThread.java:135)
01-07 03:52:19.422: E/AndroidRuntime(2536):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
01-07 03:52:19.422: E/AndroidRuntime(2536):     at android.os.Handler.dispatchMessage(Handler.java:102)
01-07 03:52:19.422: E/AndroidRuntime(2536):     at android.os.Looper.loop(Looper.java:137)
01-07 03:52:19.422: E/AndroidRuntime(2536):     at android.app.ActivityThread.main(ActivityThread.java:4998)
01-07 03:52:19.422: E/AndroidRuntime(2536):     at java.lang.reflect.Method.invokeNative(Native Method)
01-07 03:52:19.422: E/AndroidRuntime(2536):     at java.lang.reflect.Method.invoke(Method.java:515)
01-07 03:52:19.422: E/AndroidRuntime(2536):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
01-07 03:52:19.422: E/AndroidRuntime(2536):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
01-07 03:52:19.422: E/AndroidRuntime(2536):     at dalvik.system.NativeStart.main(Native Method)
01-07 03:52:19.422: E/AndroidRuntime(2536): Caused by: java.lang.NullPointerException
01-07 03:52:19.422: E/AndroidRuntime(2536):     at com.jdubbing.scissored.MainActivity.onCreate(MainActivity.java:19)
01-07 03:52:19.422: E/AndroidRuntime(2536):     at android.app.Activity.performCreate(Activity.java:5243)
01-07 03:52:19.422: E/AndroidRuntime(2536):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-07 03:52:19.422: E/AndroidRuntime(2536):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
01-07 03:52:19.422: E/AndroidRuntime(2536):     ... 11 more

最佳答案

setContentView(R.layout.splash);

但是您的 TextView 位于activity_main.xml

这个

txt.setTypeface(font); 

会给你NUllPointeException

应该是

setContentView(R.layout.activity_main);

findViewById 在当前损坏的布局中查找具有该 id 的 View 。

关于java - 与自定义字体相关的 Eclipse LogCat 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20967508/

相关文章:

java - 我应该如何使用递归递增数字?

Android 相机在没有 ExIf 的情况下保存照片

Android - Facebook SDK 3 - 仅限墙贴

java - 无法从maven中安装的jar加载类

java - 在 eclipse 项目中找不到错误

java - 对 HashTable 的assertNull 行为感到困惑

java - 停止线程等待网络 Activity 的正确方法是什么?

java - OnTouchListener 不适用于相对布局

android - 从哪里构造R.id?

java - 如何在项目中找到扩展特定库的所有类?