0
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);

fragmentactivity 无法加载并在 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)

该行取消注释很好,没有任何类型的错误或警告,所以我知道我已经导入了...

4

2 回答 2

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);
    }
于 2013-06-14T02:04:07.223 回答
0

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

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

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

final EditText editText;

然后在 onCreate 方法中执行:

editText = (EditText) findViewById(R.id.editText1);
于 2013-06-14T02:05:11.120 回答