尝试更改密码提示字体时出错

时间:2013-05-09 21:10:52

标签: android fonts

我正在尝试遵循此post的建议:

但是当我运行此代码时,我的程序崩溃了。这是我的代码:

@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);
}

以下是此对象的XML:

<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" />

最后,这里是LogCat输出,我可以看到错误:

  

05-09 00:30:21.408:E / Trace(16987):错误打开跟踪文件:没有这样的   文件或目录(2)05-09 00:30:21.918:D / AndroidRuntime(16987):   关闭VM 05-09 00:30:21.918:W / dalvikvm(16987):threadid = 1:   线程退出与未捕获的异常(组= 0x40a71930)05-09   00:30:21.978:E / AndroidRuntime(16987):致命异常:主05-09   00:30:21.978:E / AndroidRuntime(16987):java.lang.RuntimeException:   无法开始活动   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):引起: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更多

无法弄清楚出了什么问题。谁能看到它?谢谢!

1 个答案:

答案 0 :(得分:1)

更改setContentView(R.layout.activity_main);

的来电

super.onCreate(savedInstanceState);

之后将其称为

<小时/> 当您执行EditText pw = (EditText) findViewById(R.id.password);时,findViewById“无法检索您在EditText中定义的layout(因为您之后调用了setContentView)因此findViewById返回null,当您执行pw.setTypeface(Typeface.DEFAULT);时,它会抛出NPE因为pw为null