与自定义字体关联的Eclipse LogCat错误

时间:2014-01-07 09:00:22

标签: java android eclipse logcat custom-font

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

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

MainActivity.java中的第19行是我自定义文本视图代码的一部分。

如果我完全删除了MainActivity.java中的自定义文本视图代码以及activity_main.xml中的关联TextView,则不会收到错误。因此,我已经确定这个自定义文本视图似乎是问题的根源。

我尝试过以下问题排查:

  • 确保.ttf字体文件以小写字母书写
  • 确保.ttf字体文件实际上是.tff文件
  • 在资产
  • 下存储.ttf字体文件
  • 将.ttf字体文件存储在assets
  • 中的fonts子文件夹下

我实际上已经在这个简单的问题上待了好几天了。我已经阅读了许多线程来解决似乎是同样的问题,但似乎没有简单的解决方法。

提前为你的帮助干杯

字体文件位于assets / 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

2 个答案:

答案 0 :(得分:0)

setContentView(R.layout.splash);

但您的textview位于activity_main.xml

txt.setTypeface(font); 

会给你NUllPointeException

应该是

setContentView(R.layout.activity_main);

findViewById在当前的无效布局中查找id为id的视图。

答案 1 :(得分:0)

您在活动中使用了错误的布局文件,这就是为什么TextView txt为空,将setContentView(R.layout.splash);更改为setContentView(R.layout.activity_main);