-6

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

私はグーグルで検索し、SOの答えも見ましたが、何も機能していないようです

4

1 に答える 1

6

setContentView(R.layout.LAYOUTNAME);ビューを取得しようとする前に、oncreate に追加 します。

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

//LAYOUTNAMEこれは、これを定義するレイアウトの名前ですedittext

于 2013-01-23T09:34:21.730 に答える