Pergunta

When I launch my app I get the message "Unfortunately [app name] has stopped" on my emulator.

According to my LogCat, the problem is caused by an error on line 19.

Line 19 in MainActivity.java is part of my custom text view code.

If I completely delete my custom text view code in MainActivity.java and the associated TextView in activity_main.xml I do not get the error. Therefore, I have pinpointed that this custom text view seems to be the root of the problem.

I have tried the following troubleshooting:

  • ensuring the .ttf font file is written in lower case
  • ensuring the .ttf font file is actually a .tff file
  • storing the .ttf font file under assets
  • storing the .ttf font file under fonts subfolder in assets

I have actually been on this simple issue for days. I have read numerous threads that address what seems to be my same problem, but no simple fix seems to work.

Cheers in advance for your help

Font file is located under 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 Check the last five or six last lines to see the error on line 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
Foi útil?

Solução

setContentView(R.layout.splash);

But your textview is in activity_main.xml

This

txt.setTypeface(font); 

will give you NUllPointeException

Should be

setContentView(R.layout.activity_main);

findViewById looks for a view with the id in the current infalted layout.

Outras dicas

you are using wrong layout file in your activity, that is why your TextView txt is null, change setContentView(R.layout.splash); to setContentView(R.layout.activity_main);

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top