Android MapView in TabBar Application -> NullPointerException
I have a TabBarApplication. As i select the map-tab, the application crashes. The log shows a Nullpointerexception at line "final MapController mapControl = mapView.getController();". I have no idea :-(
Can someone help me?
HelloGoogleMaps.java:
package de.example;
import java.util.List;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import de.example.R;
public class HelloGoogleMaps extends MapActivity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView mapView = (MapView) findViewById(R.id.mapview);
final MapController mapControl = mapView.getController();
mapControl.setZoom(6);
mapView.setBuiltInZoomControls(true);
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.icon_32);
HelloItemizedOverlay itemizedoverlay = new
HelloItemizedOverlay(drawable,this);
GeoPoint point = new GeoPoint( 51257818,7150129);
OverlayItem overlayitem = new OverlayItem(point, "asdf","Wuppertal\nAlte
Freiheit 9");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
}
@Override
protected boolean isRouteDisplayed()
{
return false;
}
}
map.xml (in Layout folder):
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="here is my key"
/>
Log Cat:
08-05 13:13:34.787: ERROR/AndroidRuntime(463): FATAL EXCEPTION: main
08-05 13:13:34.787: ERROR/AndroidRuntime(463): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.example/de.example.HelloGoogleMaps}: java.lang.NullPointerException
08-05 13:13:34.787: ERROR/AndroidRuntime(463): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-05 13:13:34.787: ERROR/AndroidRuntime(463): at android.app.ActivityThread.startActivityNow(ActivityThread.java:1487)
08-05 13:13:34.787: ERROR/AndroidRuntime(463): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
08开发者_如何学Go-05 13:13:34.787: ERROR/AndroidRuntime(463): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
08-05 13:13:34.787: ERROR/AndroidRuntime(463): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:654)
08-05 13:13:34.787: ERROR/AndroidRuntime(463): at android.widget.TabHost.setCurrentTab(TabHost.java:326)
08-05 13:13:34.787: ERROR/AndroidRuntime(463): at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:132)
08-05 13:13:34.787: ERROR/AndroidRuntime(463): at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:456)
08-05 13:13:34.787: ERROR/AndroidRuntime(463): at android.view.View.performClick(View.java:2485)
08-05 13:13:34.787: ERROR/AndroidRuntime(463): at android.view.View$PerformClick.run(View.java:9080)
08-05 13:13:34.787: ERROR/AndroidRuntime(463): at android.os.Handler.handleCallback(Handler.java:587)
08-05 13:13:34.787: ERROR/AndroidRuntime(463): at android.os.Handler.dispatchMessage(Handler.java:92)
08-05 13:13:34.787: ERROR/AndroidRuntime(463): at android.os.Looper.loop(Looper.java:130)
08-05 13:13:34.787: ERROR/AndroidRuntime(463): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-05 13:13:34.787: ERROR/AndroidRuntime(463): at java.lang.reflect.Method.invokeNative(Native Method)
08-05 13:13:34.787: ERROR/AndroidRuntime(463): at java.lang.reflect.Method.invoke(Method.java:507)
08-05 13:13:34.787: ERROR/AndroidRuntime(463): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-05 13:13:34.787: ERROR/AndroidRuntime(463): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-05 13:13:34.787: ERROR/AndroidRuntime(463): at dalvik.system.NativeStart.main(Native Method)
08-05 13:13:34.787: ERROR/AndroidRuntime(463): Caused by: java.lang.NullPointerException
08-05 13:13:34.787: ERROR/AndroidRuntime(463): at de.example.HelloGoogleMaps.onCreate(HelloGoogleMaps.java:26)
08-05 13:13:34.787: ERROR/AndroidRuntime(463): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-05 13:13:34.787: ERROR/AndroidRuntime(463): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-05 13:13:34.787: ERROR/AndroidRuntime(463): ... 18 more
Thank you :-)
You're calling:
setContentView(R.layout.main);
Did you mean R.layout.map
? Because your layout is in map.xml
, not main.xml
.
精彩评论