Android EditText setText不能正常工作

时间:2013-01-23 09:28:38

标签: android android-edittext

我正在尝试使用java设置edittext文本,但它无法正常工作并强制关闭我的应用程序,但我不知道为什么

Java代码

EditText UserProfileFirstName;
UserProfileFirstName = (EditText) findViewById(R.id.userFirstName);
UserProfileFirstName.setText("John");

XML

<EditText 
   android:id="@+id/userFirstName"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:inputType="text"
   android:textSize="22sp"
   android:layout_marginTop="40dp"
   android:layout_marginBottom="15dp"
   android:hint="First Name"
   android:ems="10"
  />

LogCat错误

        01-23 09:32:15.564: E/AndroidRuntime(4101): FATAL EXCEPTION: main
        01-23 09:32:15.564: E/AndroidRuntime(4101): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.unext.unextlibrary/com.unext.unextlibrary.ProfileActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.unext.unextlibrary/com.unext.unextlibrary.UserProfile}: java.lang.NullPointerException
        01-23 09:32:15.564: E/AndroidRuntime(4101):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
        01-23 09:32:15.564: E/AndroidRuntime(4101):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
        01-23 09:32:15.564: E/AndroidRuntime(4101):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
        01-23 09:32:15.564: E/AndroidRuntime(4101):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
        01-23 09:32:15.564: E/AndroidRuntime(4101):     at android.os.Handler.dispatchMessage(Handler.java:99)
        01-23 09:32:15.564: E/AndroidRuntime(4101):     at android.os.Looper.loop(Looper.java:137)
        01-23 09:32:15.564: E/AndroidRuntime(4101):     at android.app.ActivityThread.main(ActivityThread.java:5039)
        01-23 09:32:15.564: E/AndroidRuntime(4101):     at java.lang.reflect.Method.invokeNative(Native Method)
        01-23 09:32:15.564: E/AndroidRuntime(4101):     at java.lang.reflect.Method.invoke(Method.java:511)
        01-23 09:32:15.564: E/AndroidRuntime(4101):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
        01-23 09:32:15.564: E/AndroidRuntime(4101):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
        01-23 09:32:15.564: E/AndroidRuntime(4101):     at dalvik.system.NativeStart.main(Native Method)
        01-23 09:32:15.564: E/AndroidRuntime(4101): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.unext.unextlibrary/com.unext.unextlibrary.UserProfile}: java.lang.NullPointerException
        01-23 09:32:15.564: E/AndroidRuntime(4101):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
        01-23 09:32:15.564: E/AndroidRuntime(4101):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:2023)
        01-23 09:32:15.564: E/AndroidRuntime(4101):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
        01-23 09:32:15.564: E/AndroidRuntime(4101):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
        01-23 09:32:15.564: E/AndroidRuntime(4101):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:749)
        01-23 09:32:15.564: E/AndroidRuntime(4101):     at android.widget.TabHost.setCurrentTab(TabHost.java:413)
        01-23 09:32:15.564: E/AndroidRuntime(4101):     at android.widget.TabHost.addTab(TabHost.java:240)
        01-23 09:32:15.564: E/AndroidRuntime(4101):     at com.unext.unextlibrary.ProfileActivity.onCreate(ProfileActivity.java:105)
        01-23 09:32:15.564: E/AndroidRuntime(4101):     at android.app.Activity.performCreate(Activity.java:5104)
        01-23 09:32:15.564: E/AndroidRuntime(4101):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
        01-23 09:32:15.564: E/AndroidRuntime(4101):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
        01-23 09:32:15.564: E/AndroidRuntime(4101):     ... 11 more
        01-23 09:32:15.564: E/AndroidRuntime(4101): Caused by: java.lang.NullPointerException
        01-23 09:32:15.564: E/AndroidRuntime(4101):     at com.unext.unextlibrary.UserProfile.onCreate(UserProfile.java:57)
        01-23 09:32:15.564: E/AndroidRuntime(4101):     at android.app.Activity.performCreate(Activity.java:5104)
        01-23 09:32:15.564: E/AndroidRuntime(4101):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
        01-23 09:32:15.564: E/AndroidRuntime(4101):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
        01-23 09:32:15.564: E/AndroidRuntime(4101):     ... 21 more

我在google上搜索过,也看到了SO答案,但似乎没有什么对我有用

1 个答案:

答案 0 :(得分:6)

添加 在尝试通过

获取视图之前,在oncreate中setContentView(R.layout.LAYOUTNAME);

UserProfileFirstName = (EditText) findViewById(R.id.userFirstName);

//此处LAYOUTNAME是您定义此edittext

的布局名称