我正试图通过此Link在我的应用中使用Google地图 但我无法在设备上看到地图
这是日志
10-04 16:12:16.631: E/AndroidRuntime(1023): FATAL EXCEPTION: main
10-04 16:12:16.631: E/AndroidRuntime(1023): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.restaurantfinder/com.example.restaurantfinder.MainActivity}: java.lang.NullPointerException
10-04 16:12:16.631: E/AndroidRuntime(1023): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
10-04 16:12:16.631: E/AndroidRuntime(1023): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-04 16:12:16.631: E/AndroidRuntime(1023): at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-04 16:12:16.631: E/AndroidRuntime(1023): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-04 16:12:16.631: E/AndroidRuntime(1023): at android.os.Handler.dispatchMessage(Handler.java:99)
10-04 16:12:16.631: E/AndroidRuntime(1023): at android.os.Looper.loop(Looper.java:137)
10-04 16:12:16.631: E/AndroidRuntime(1023): at android.app.ActivityThread.main(ActivityThread.java:4745)
10-04 16:12:16.631: E/AndroidRuntime(1023): at java.lang.reflect.Method.invokeNative(Native Method)
10-04 16:12:16.631: E/AndroidRuntime(1023): at java.lang.reflect.Method.invoke(Method.java:511)
10-04 16:12:16.631: E/AndroidRuntime(1023): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-04 16:12:16.631: E/AndroidRuntime(1023): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-04 16:12:16.631: E/AndroidRuntime(1023): at dalvik.system.NativeStart.main(Native Method)
10-04 16:12:16.631: E/AndroidRuntime(1023): Caused by: java.lang.NullPointerException
10-04 16:12:16.631: E/AndroidRuntime(1023): at com.example.restaurantfinder.MainActivity.onCreate(MainActivity.java:38)
10-04 16:12:16.631: E/AndroidRuntime(1023): at android.app.Activity.performCreate(Activity.java:5008)
10-04 16:12:16.631: E/AndroidRuntime(1023): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
10-04 16:12:16.631: E/AndroidRuntime(1023): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
10-04 16:12:16.631: E/AndroidRuntime(1023): ... 11 more
代码:
public class MainActivity extends Activity {
private GoogleMap googleMap;
double latitude = 0;
double longitude = 0;
// create marker
MarkerOptions marker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
// Loading map
initilizeMap();
} catch (Exception e) {
e.printStackTrace();
}
// create marker
MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Hello Maps ");
// adding marker
googleMap.addMarker(marker);
}
private void initilizeMap() {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}
protected void onResume()
{
super.onResume();
initilizeMap();
}
感谢您的帮助..
答案 0 :(得分:0)
private GoogleMap googleMap=null;
尝试初始化它 可能是错误 如错误所说
10-04 16:12:16.631: E/AndroidRuntime(1023): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.restaurantfinder/com.example.restaurantfinder.MainActivity}: java.lang.NullPointerException
答案 1 :(得分:0)
试试这个 - >
private void initilizeMap() {
Handler handler = new Handler();
handler.postDelayed(new Runnable()
@Override
public void run() {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
if(googleMap != null) {
googleMap.addMarker(new MarkerOptions().position(result)).setVisible(true);
// Move the camera instantly to location with a zoom of 15.
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(result, 15));
// Zoom in, animating the camera.
googleMap.animateCamera(CameraUpdateFactory.zoomTo(14), 2000, null);
googleMap.getUiSettings().setZoomControlsEnabled(false);
googleMap.getUiSettings().setCompassEnabled(false);
googleMap.getUiSettings().setMyLocationButtonEnabled(false);
handler.removeCallbacksAndMessages(null);
}
else {
handler.postDelayed(this, 500);
}
}, 500);
}
答案 2 :(得分:0)
在主线程上运行地图是个问题。在另一项活动中使用地图这是冰淇淋三明治中的问题,但在果冻豆上它运作良好。