Question

i had followed exactly same procedure on google's tutorial: http://developer.android.com/guide/tutorials/views/hello-mapview.html

...while i got "force close"

here's my code:

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;
}
}

the outcome with "null pointer exception" in logcat

here the log cat:

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

main.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"
    />
-->

manifest

<?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>

could sb teach me what had i mistaken?

P.S. without mapview.setBuiltInZoomControls(true); this line , could run map correctly

Was it helpful?

Solution

You need to move "mapview=(MapView)findViewById(R.id.map);" to be inside of your onCreate(). You dont need to have the call to findViews().

I am doing a project with a map as well and this is how mine looks.

Your manifest and .xml files look correct to me so hopefully this helps you.

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;
}

OTHER TIPS

The logcat is telling you that there is a null reference at line 18 of your class TrainstationActivity. Without seeing more of your code, I suspect its a missing 'main' layout or the R.id.map. Set a breakpoint in the first line of your onCreate method and step through the code using the debugger.

I think RelativeLayout in the main.xml is not closed. MapView should be in the layout which means there should be RelativeLayout end tag after the MapView.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top