我試圖跟蹤該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): 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
燦弄清楚發生了什麼問題。任何人都可以看到它嗎?謝謝!
就是這樣。謝謝! – Alex 2013-05-09 22:09:32