我有一个TabBarApplication。当我选择地图选项卡时,应用程序崩溃。该日志在“最终MapController mapControl = mapView.getController();”行显示Nullpointerexception。我不知道: - (
有人能帮助我吗?
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(在布局文件夹中):
<?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-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
谢谢: - )
答案 0 :(得分:2)
你在打电话:
setContentView(R.layout.main);
您的意思是R.layout.map
吗?因为您的布局位于map.xml
,而不是main.xml
。