1

我在谷歌的教程中遵循了完全相同的程序: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 ComponentInfo{trian.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

主.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.google.android.maps.MapView
    android:id="@+id/map"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:apiKey="********"
    android:clickable="true"
    />
<!--<LinearLayout android:id="@+id/zoom"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    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>

某人可以教我我错了什么吗?

PS 没有 mapview.setBuiltInZoomControls(true); 这条线,可以正确运行地图

4

3 回答 3

0

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

于 2012-01-10T20:46:05.133 回答
0

您需要移动“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;
}
于 2012-01-10T21:55:44.107 回答
0

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

于 2013-10-23T15:12:04.450 回答