Android EditText没有加载

时间:2013-06-14 01:59:23

标签: android android-edittext android-fragmentactivity

public class MainActivity extends FragmentActivity {
    /**
     * Note that this may be null if the Google Play services APK is not available.
     */
    private GoogleMap mMap;
    final EditText editText = (EditText) findViewById(R.id.editText1);



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:orientation="vertical" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPostalAddress"
        android:imeOptions="actionSend" >

        <requestFocus />
    </EditText>

    <fragment
      android:id="@+id/map"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      class="com.google.android.gms.maps.SupportMapFragment"/>

</LinearLayout>

出于某种原因只评论了这一行:

//final EditText editText = (EditText) findViewById(R.id.editText1);

然后我的申请运作良好。

一旦我将其添加回来:

final EditText editText = (EditText) findViewById(R.id.editText1);

片段活动无法加载并在Android设备上以错误终止。

            06-13 18:52:07.924: E/Trace(28101): error opening trace file: No such file or directory (2)
            06-13 18:52:08.004: E/AndroidRuntime(28101): FATAL EXCEPTION: main
            06-13 18:52:08.004: E/AndroidRuntime(28101): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.methanegaszonegeolocator/com.example.methanegaszonegeolocator.MainActivity}: java.lang.NullPointerException
            06-13 18:52:08.004: E/AndroidRuntime(28101):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2012)
            06-13 18:52:08.004: E/AndroidRuntime(28101):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2113)
            06-13 18:52:08.004: E/AndroidRuntime(28101):    at android.app.ActivityThread.access$700(ActivityThread.java:139)
            06-13 18:52:08.004: E/AndroidRuntime(28101):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1224)
            06-13 18:52:08.004: E/AndroidRuntime(28101):    at android.os.Handler.dispatchMessage(Handler.java:99)
            06-13 18:52:08.004: E/AndroidRuntime(28101):    at android.os.Looper.loop(Looper.java:137)
            06-13 18:52:08.004: E/AndroidRuntime(28101):    at android.app.ActivityThread.main(ActivityThread.java:4918)
            06-13 18:52:08.004: E/AndroidRuntime(28101):    at java.lang.reflect.Method.invokeNative(Native Method)
            06-13 18:52:08.004: E/AndroidRuntime(28101):    at java.lang.reflect.Method.invoke(Method.java:511)
            06-13 18:52:08.004: E/AndroidRuntime(28101):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
            06-13 18:52:08.004: E/AndroidRuntime(28101):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)

这条线没有注释,没有任何错误或警告,所以我知道我的进口已经下降......

2 个答案:

答案 0 :(得分:0)

您必须将以下行放在onCreate方法中。

final EditText editText = (EditText) findViewById(R.id.editText1);

所以在你的情况下,它会......

public class MainActivity extends FragmentActivity {

    /**
     * Note that this may be null if the Google Play services APK is not available.
     */
    private GoogleMap mMap;
    final EditText editText; 

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_main); // PASS YOUR LAYOUT HERE

        editText = (EditText) findViewById(R.id.editText1);
    }

答案 1 :(得分:0)

这归结为在类实例化时无法调用findViewById。

您必须在onCreate方法中调用该行。

如果您需要在班级的任何地方访问它,那么在课程顶部执行:

final EditText editText;

然后在onCreate方法中执行:

editText = (EditText) findViewById(R.id.editText1);