我想通過這個 使用谷歌地圖在我的應用程序,但我不能看到我的設備上地圖谷歌地圖無法看到設備
這是日誌
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();
}
感謝您的幫助..
請在這裏發佈你的代碼。 –
我已經完成了該鏈接中給出的內容。 – user1659731
Activity的第38行爲NULL值。它是什麼? –