0

I'm trying to follow the advice from this post:

But when I run this code, my program crashes. Here is my code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    /* Change the font for the password hint to match that of the
     * user name field */
    EditText pw = (EditText) findViewById(R.id.password);
    pw.setTypeface(Typeface.DEFAULT);    //<---Crashes on this line
    pw.setTransformationMethod(new PasswordTransformationMethod());

    setContentView(R.layout.activity_main);
}

Here is the XML for this object:

<EditText
    android:id="@+id/password"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/userName"
    android:layout_alignRight="@+id/appName"
    android:layout_below="@+id/userName"
    android:layout_marginTop="14dp"
    android:ems="10"
    android:hint="@string/password"
    android:inputType="textPassword" />

And finally, here is the LogCat output where I can see the error:

05-09 00:30:21.408: E/Trace(16987): error opening trace file: No such file or directory (2) 05-09 00:30:21.918: D/AndroidRuntime(16987): Shutting down VM 05-09 00:30:21.918: W/dalvikvm(16987): threadid=1: thread exiting with uncaught exception (group=0x40a71930) 05-09 00:30:21.978: E/AndroidRuntime(16987): FATAL EXCEPTION: main 05-09 00:30:21.978: E/AndroidRuntime(16987): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mobilenicity.gen_co_event_marketing_app/com.mobilenicity.gen_co_event_marketing_app.MainActivity}: java.lang.NullPointerException 05-09 00:30:21.978: E/AndroidRuntime(16987): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 05-09 00:30:21.978: E/AndroidRuntime(16987): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 05-09 00:30:21.978: E/AndroidRuntime(16987): at android.app.ActivityThread.access$600(ActivityThread.java:141) 05-09 00:30:21.978: E/AndroidRuntime(16987): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 05-09 00:30:21.978: E/AndroidRuntime(16987): at android.os.Handler.dispatchMessage(Handler.java:99) 05-09 00:30:21.978: E/AndroidRuntime(16987): at android.os.Looper.loop(Looper.java:137) 05-09 00:30:21.978: E/AndroidRuntime(16987): at android.app.ActivityThread.main(ActivityThread.java:5041) 05-09 00:30:21.978: E/AndroidRuntime(16987): at java.lang.reflect.Method.invokeNative(Native Method) 05-09 00:30:21.978: E/AndroidRuntime(16987): at java.lang.reflect.Method.invoke(Method.java:511) 05-09 00:30:21.978: E/AndroidRuntime(16987): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 05-09 00:30:21.978: E/AndroidRuntime(16987): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 05-09 00:30:21.978: E/AndroidRuntime(16987): at dalvik.system.NativeStart.main(Native Method) 05-09 00:30:21.978: E/AndroidRuntime(16987): Caused by: java.lang.NullPointerException 05-09 00:30:21.978: E/AndroidRuntime(16987): at com.mobilenicity.gen_co_event_marketing_app.MainActivity.onCreate(MainActivity.java:26) 05-09 00:30:21.978: E/AndroidRuntime(16987): at android.app.Activity.performCreate(Activity.java:5104) 05-09 00:30:21.978: E/AndroidRuntime(16987): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 05-09 00:30:21.978: E/AndroidRuntime(16987): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 05-09 00:30:21.978: E/AndroidRuntime(16987): ... 11 more

Can't figure out what is going wrong. Can anybody see it? Thanks!

Community
  • 1
  • 1
AndroidDev
  • 20,466
  • 42
  • 148
  • 239

1 Answers1

1

Change the call of setContentView(R.layout.activity_main);

Call it just after super.onCreate(savedInstanceState);


When you're doing EditText pw = (EditText) findViewById(R.id.password);, findViewById "can't retrieve" the EditText that you have defined in your layout (because you call setContentView after) so findViewById returns null and when you're doing pw.setTypeface(Typeface.DEFAULT); it throws a NPE because pw is null.
Alexis C.
  • 91,686
  • 21
  • 171
  • 177