2013-10-04 83 views
0

我想通過這個 使用谷歌地圖在我的應用程序,但我不能看到我的設備上地圖谷歌地圖無法看到設備

這是日誌

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

我已經完成了該鏈接中給出的內容。 – user1659731

+0

Activity的第38行爲NULL值。它是什麼? –

回答

0

上MainThread運行圖是問題。在另一個活動中使用地圖,這是冰激凌三明治中的問題,但在果凍豆上效果很好。

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 
+0

它沒有工作。 – user1659731

+0

同樣的錯誤? ... –

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); 
} 
+0

我的應用程序崩潰了。 – user1659731

+0

錯誤是什麼 –