Question

I made a method where all the font's get changed, but the problem is that I want to use the method once so I don't have to copy and paste it in every script.

Code example:

public void changeFont(){

    int[] id_et = {R.id.inputObjectName, R.id.inputObjectValue};
    int[] id_tv = {R.id.headerUserInfo, R.id.headerUserPW, R.id.headerUserGW};
    int[] id_btn = {R.id.privBtnIncome, R.id.privBtnExpense};

    Typeface font = Typeface.createFromAsset(getAssets(), "Sanford.TTF");

    for(int i = 0; i < id_et.length; i++){
        EditText textbox = (EditText) findViewById(id_et[i]);
        textbox.setTypeface(font);
    }

    for(int i = 0; i < id_tv.length; i++){
        TextView tv = (TextView) findViewById(id_tv[i]);
        tv.setTypeface(font);
    }

    for(int i = 0; i < id_btn.length; i++){
        Button btn = (Button) findViewById(id_btn[i]);
        btn.setTypeface(font);
    }

}

When I create a class and make an object to use the constructor to get the data, the program crashes.

This is how the class looks:

public class ChangeFont extends Activity{
    public ChangeFont(){

    int[] id_et = {R.id.inputObjectName, R.id.inputObjectValue};
    int[] id_tv = {R.id.headerUserInfo, R.id.headerUserPW, R.id.headerUserGW};
    int[] id_btn = {R.id.privBtnIncome, R.id.privBtnExpense};

    Typeface font = Typeface.createFromAsset(getAssets(), "Sanford.TTF");

    for(int i = 0; i < id_et.length; i++){
        EditText textbox = (EditText) findViewById(id_et[i]);
        textbox.setTypeface(font);
    }

    for(int i = 0; i < id_tv.length; i++){
        TextView tv = (TextView) findViewById(id_tv[i]);
        tv.setTypeface(font);
    }

    for(int i = 0; i < id_btn.length; i++){
        Button btn = (Button) findViewById(id_btn[i]);
        btn.setTypeface(font);
    }

    }

}

Error log:

12-12 17:23:49.746: E/AndroidRuntime(6159): FATAL EXCEPTION: main
12-12 17:23:49.746: E/AndroidRuntime(6159): java.lang.RuntimeException: Unable to start activity ComponentInfo{united.aristal.freewallet/united.aristal.freewallet.PrivateWallet}: java.lang.NullPointerException
12-12 17:23:49.746: E/AndroidRuntime(6159):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
12-12 17:23:49.746: E/AndroidRuntime(6159):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
12-12 17:23:49.746: E/AndroidRuntime(6159):     at android.app.ActivityThread.access$600(ActivityThread.java:122)
12-12 17:23:49.746: E/AndroidRuntime(6159):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
12-12 17:23:49.746: E/AndroidRuntime(6159):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-12 17:23:49.746: E/AndroidRuntime(6159):     at android.os.Looper.loop(Looper.java:137)
12-12 17:23:49.746: E/AndroidRuntime(6159):     at android.app.ActivityThread.main(ActivityThread.java:4340)
12-12 17:23:49.746: E/AndroidRuntime(6159):     at java.lang.reflect.Method.invokeNative(Native Method)
12-12 17:23:49.746: E/AndroidRuntime(6159):     at java.lang.reflect.Method.invoke(Method.java:511)
12-12 17:23:49.746: E/AndroidRuntime(6159):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-12 17:23:49.746: E/AndroidRuntime(6159):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-12 17:23:49.746: E/AndroidRuntime(6159):     at dalvik.system.NativeStart.main(Native Method)
12-12 17:23:49.746: E/AndroidRuntime(6159): Caused by: java.lang.NullPointerException
12-12 17:23:49.746: E/AndroidRuntime(6159):     at android.content.ContextWrapper.getAssets(ContextWrapper.java:75)
12-12 17:23:49.746: E/AndroidRuntime(6159):     at united.aristal.freewallet.ChangeFont.<init>(ChangeFont.java:16)
12-12 17:23:49.746: E/AndroidRuntime(6159):     at united.aristal.freewallet.PrivateWallet.onCreate(PrivateWallet.java:32)
12-12 17:23:49.746: E/AndroidRuntime(6159):     at android.app.Activity.performCreate(Activity.java:4465)
12-12 17:23:49.746: E/AndroidRuntime(6159):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-12 17:23:49.746: E/AndroidRuntime(6159):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
12-12 17:23:49.746: E/AndroidRuntime(6159):     ... 11 more

So can anyone help me here?

Was it helpful?

Solution

getAssets is throwing NullPointerException because you created the ChangeFont instance instead of letting Android do it. If ChangeFont is just a utility class, it should not extend Activity.

Instead, initialize it with an instance of your actual activity, and let it call methods on that activity.

public class ChangeFont {

    public ChangeFont(final Activity activity) {
        this.activity = activity;

        Typeface font = Typeface.createFromAsset(activity.getAssets(), "Sanford.TTF");
    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top