安卓谷歌地图:空指针异常

Posted

技术标签:

【中文标题】安卓谷歌地图:空指针异常【英文标题】:android google map:nullpointer exception 【发布时间】:2012-01-10 20:17:20 【问题描述】:

我在谷歌的教程中遵循了完全相同的程序: http://developer.android.com/guide/tutorials/views/hello-mapview.html

...当我得到“强制关闭”时

这是我的代码:

public class TrainstationActivity extends MapActivity 
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    findViews();
    mapview.setBuiltInZoomControls(true);


  private MapView mapview;
  LinearLayout linearLayout;


private void findViews() 
    mapview=(MapView)findViewById(R.id.map);

@Override
protected boolean isRouteDisplayed() 
    // TODO Auto-generated method stub
    return false;


logcat 中出现“空指针异常”的结果

这里是日志猫:

01-10 20:09:31.564: D/dalvikvm(789): GC_CONCURRENT freed 1161K, 54% free 3151K/6727K, external 1625K/2137K, paused 4ms+4ms
01-10 20:09:31.824: D/dalvikvm(789): GC_CONCURRENT freed 531K, 51% free 3311K/6727K, external 1625K/2137K, paused 4ms+7ms
01-10 20:09:32.034: D/AndroidRuntime(789): Shutting down VM
01-10 20:09:32.034: W/dalvikvm(789): threadid=1: thread exiting with uncaught exception (group=0x40015560)
01-10 20:09:32.044: E/AndroidRuntime(789): FATAL EXCEPTION: main
01-10 20:09:32.044: E/AndroidRuntime(789): java.lang.RuntimeException: Unable to start activity ComponentInfotrian.station/trian.station.TrainstationActivity: java.lang.NullPointerException
01-10 20:09:32.044: E/AndroidRuntime(789):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
01-10 20:09:32.044: E/AndroidRuntime(789):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-10 20:09:32.044: E/AndroidRuntime(789):  at  android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-10 20:09:32.044: E/AndroidRuntime(789):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-10 20:09:32.044: E/AndroidRuntime(789):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-10 20:09:32.044: E/AndroidRuntime(789):  at android.os.Looper.loop(Looper.java:130)
01-10 20:09:32.044: E/AndroidRuntime(789):  at android.app.ActivityThread.main(ActivityThread.java:3683)
01-10 20:09:32.044: E/AndroidRuntime(789):  at java.lang.reflect.Method.invokeNative(Native Method)
01-10 20:09:32.044: E/AndroidRuntime(789):  at java.lang.reflect.Method.invoke(Method.java:507)
01-10 20:09:32.044: E/AndroidRuntime(789):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-10 20:09:32.044: E/AndroidRuntime(789):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-10 20:09:32.044: E/AndroidRuntime(789):  at dalvik.system.NativeStart.main(Native Method)
01-10 20:09:32.044: E/AndroidRuntime(789): Caused by: java.lang.NullPointerException
01-10 20:09:32.044: E/AndroidRuntime(789):  at trian.station.TrainstationActivity.onCreate(TrainstationActivity.java:18)
01-10 20:09:32.044: E/AndroidRuntime(789):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-10 20:09:32.044: E/AndroidRuntime(789):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-10 20:09:32.044: E/AndroidRuntime(789):  ... 11 more
01-10 20:09:33.934: D/dalvikvm(789): GC_CONCURRENT freed 830K, 54% free 3098K/6727K, external 1641K/2137K, paused 3ms+5ms

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_
android:layout_
>
<com.google.android.maps.MapView
    android:id="@+id/map"
    android:layout_
    android:layout_
    android:apiKey="********"
    android:clickable="true"
    />
<!--<LinearLayout android:id="@+id/zoom"
    android:layout_
    android:layout_
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    />
-->

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.demo.android.trainstation"
  android:versionCode="1"
  android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
    <uses-library android:name="com.google.android.maps" />
    <activity android:name=".TrainStation"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>
<uses-sdk android:minSdkVersion="5" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>

有人能教我我做错了什么吗?

附: 没有 mapview.setBuiltInZoomControls(true); 这一行,可以正确运行map

【问题讨论】:

【参考方案1】:

你需要移动“mapview=(MapView)findViewById(R.id.map);”在你的 onCreate() 里面。您不需要调用 findViews()。

我也在做一个带有地图的项目,这就是我的样子。

您的清单和 .xml 文件在我看来是正确的,因此希望这对您有所帮助。

public class GoogleMap extends MapActivity
@Override
public void onCreate(Bundle savedInstanceState)    
    super.onCreate(savedInstanceState);    
    setContentView(R.layout.map);

    MapView mapView = (MapView) findViewById(R.id.mapview);    
    mapView.setBuiltInZoomControls(true);

     //End onCreate();

@Override
protected boolean isRouteDisplayed() 
    
    return false;

【讨论】:

仍然无法正常工作,顺便说一句,我在发布问题之前尝试过这个:)【参考方案2】:

logcat 告诉你在你的类 TrainstationActivity 的第 18 行有一个空引用。在没有看到更多代码的情况下,我怀疑它缺少“主”布局或 R.id.map。在 onCreate 方法的第一行设置断点,并使用调试器单步执行代码。

【讨论】:

感谢回答,没有遗漏“主”布局或 R.id.map。 在您的清单文件中,您的活动应该是 TrainstationActivity。另外,你能逐步调试吗?【参考方案3】:

我认为 main.xml 中的 RelativeLayout 没有关闭。 MapView 应该在布局中,这意味着 MapView 后面应该有 RelativeLayout 结束标记。

【讨论】:

以上是关于安卓谷歌地图:空指针异常的主要内容,如果未能解决你的问题,请参考以下文章

导航抽屉中的谷歌地图 - 空指针异常,哪个是正确的片段?

Android GoogleMaps 空指针异常 lagitutde

获取地图上的空指针异常

使用 Eclipse 插件追加新谷歌的空指针异常

Android自定义view,空指针异常求解决

Android Canvas drawcolor 抛出空指针异常