【问题标题】:Eclipse LogCat error associated with custom font与自定义字体相关的 Eclipse LogCat 错误
【发布时间】:2014-01-07 09:00:21
【问题描述】:

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

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

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

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

我已尝试以下故障排除:

  • 确保 .ttf 字体文件以小写形式编写
  • 确保 .ttf 字体文件实际上是 .tff 文件
  • 将 .ttf 字体文件存储在 assets 下
  • 将 .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

【问题讨论】:

    标签: java android eclipse logcat custom-font


    【解决方案1】:
    setContentView(R.layout.splash);
    

    但是你的文本视图在activity_main.xml

    这个

    txt.setTypeface(font); 
    

    会给你NUllPointeException

    应该是

    setContentView(R.layout.activity_main);
    

    findViewById 在当前错误布局中查找具有 id 的视图。

    【讨论】:

    • 好的,很酷。我了解我将错误的布局文件与 TextView 文本结合使用。不过,如果我只是更改为: setContentView(R.layout.activity_main);我无法先显示我的启动画面。我想显示我的初始屏幕图像 10 秒,然后在我的下一个屏幕上显示自定义文本。我需要为 Splash 屏幕和 activity_main 屏幕编写单独的 setContentView 吗?
    • 启动画面是一项活动。您导航到第二个活动。它还需要一个布局来显示
    • @jdubbing 你的启动画面是做什么的。有些人写博客说闪屏不好,因为它会让用户等待,尤其是当它除了显示图像什么都不做时
    • 谢谢,这是一个更容易解决的问题。全部固定。
    【解决方案2】:

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

    【讨论】:

    • 谢谢,如上所述,这解决了我的大部分问题。如果您能在最后一点帮助我,请查看我对 Raghunandan 的回复。
    猜你喜欢
    • 1970-01-01
    • 2018-03-18
    • 2022-12-11
    • 2011-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-11
    相关资源
    最近更新 更多