0

嗨,我在这行代码中收到 NullPointerException:

 findViewById(R.id.mapScreen).setDrawingCacheEnabled(true);

当我尝试在模拟器中运行此活动时。

我认为这可能是我的 xml 的问题,但是对我来说一切看起来都是正确的。这是Java代码:

@Override
protected void onCreate(Bundle load)
{
  super.onCreate(load);

  //CODE FROM RIDETRACKING.JAVA
  FrameLayout frame = new FrameLayout(this);
  frame.setId(CONTENT_VIEW_ID);
  setContentView(frame, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
  //END OF CODE FROM RIDETRACKING.JAVA

  //setContentView(R.layout.map);
  findViewById(R.id.mapScreen).setDrawingCacheEnabled(true);
  mUnits = new UnitsI18n(this);
  mLoggerServiceManager = new GPSLoggerServiceManager(this);

}

这是我的 XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/mapScreen">
<com.google.android.maps.MapView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/myMapView"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:enabled="true"
  android:clickable="true"
  android:apiKey="0S37nJcRwodWjzWn7yJwFRc9qr5gvaD2bdvOpEQ" 
  android:visibility="visible" />
 </RelativeLayout>

这是我的 logcat 输出:

03-20 16:14:05.294: E/AndroidRuntime(2424): FATAL EXCEPTION: main
03-20 16:14:05.294: E/AndroidRuntime(2424): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.gpstracker/com.polaris.epicriders.Rides.RideTracking}: java.lang.NullPointerException
03-20 16:14:05.294: E/AndroidRuntime(2424):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-20 16:14:05.294: E/AndroidRuntime(2424):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-20 16:14:05.294: E/AndroidRuntime(2424):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-20 16:14:05.294: E/AndroidRuntime(2424):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-20 16:14:05.294: E/AndroidRuntime(2424):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-20 16:14:05.294: E/AndroidRuntime(2424):     at android.os.Looper.loop(Looper.java:137)
03-20 16:14:05.294: E/AndroidRuntime(2424):     at android.app.ActivityThread.main(ActivityThread.java:5039)
03-20 16:14:05.294: E/AndroidRuntime(2424):     at java.lang.reflect.Method.invokeNative(Native Method)
03-20 16:14:05.294: E/AndroidRuntime(2424):     at java.lang.reflect.Method.invoke(Method.java:511)
03-20 16:14:05.294: E/AndroidRuntime(2424):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-20 16:14:05.294: E/AndroidRuntime(2424):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-20 16:14:05.294: E/AndroidRuntime(2424):     at dalvik.system.NativeStart.main(Native Method)
03-20 16:14:05.294: E/AndroidRuntime(2424): Caused by: java.lang.NullPointerException
03-20 16:14:05.294: E/AndroidRuntime(2424):     at com.android.riders.Rides.RideTracking.onCreate(RideTracking.java:181)
03-20 16:14:05.294: E/AndroidRuntime(2424):     at android.app.Activity.performCreate(Activity.java:5104)
03-20 16:14:05.294: E/AndroidRuntime(2424):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-20 16:14:05.294: E/AndroidRuntime(2424):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-20 16:14:05.294: E/AndroidRuntime(2424):     ... 11 more
4

1 回答 1

4

设置activity的内容而不是FrameLayout。

setContentView(R.layout.layout_main);

你得到 null 因为你没有设置活动布局,在这里你试图从布局中获取视图。

findViewById(R.id.mapScreen).setDrawingCacheEnabled(true);
于 2013-03-20T16:33:56.553 回答