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)
该行取消注释很好,没有任何类型的错误或警告,所以我知道我已经导入了...