I'm trying to add google maps to my app. The logcat window shows this error: Caused by: java.lang.NullPointerException at com.example.mymaps.MainActivity.onCreate(MainActivity.java:27) at android.app.Activity.performCreate(Activity.java:5104) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080).
我正在尝试将谷歌地图添加到我的应用程序中。 logcat窗口显示此错误:由android下的android.app.Activity.performCreate(Activity.java:5104)的com.example.mymaps.MainActivity.onCreate(MainActivity.java:27)中的java.lang.NullPointerException引起。 app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)。
How to remove this error?
如何删除此错误?
Main xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
</RelativeLayout>
Main Java import android.app.Activity; import android.os.Bundle; import android.view.Menu;
主要Java导入android.app.Activity; import android.os.Bundle; import android.view.Menu;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
public class MainActivity extends Activity {
static final LatLng HAMBURG = new LatLng(53.558, 9.927);
static final LatLng KIEL = new LatLng(53.551, 9.993);
private GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
.title("Hamburg"));
Marker kiel = map.addMarker(new MarkerOptions()
.position(KIEL)
.title("Kiel")
.snippet("Kiel is cool")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.ic_launcher)));
// Move the camera instantly to hamburg with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));
// Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
LogCat window:
08-13 23:32:27.279: E/AndroidRuntime(1010): FATAL EXCEPTION: main 08-13 23:32:27.279: E/AndroidRuntime(1010): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mymaps/com.example.mymaps.MainActivity}: java.lang.NullPointerException 08-13 23:32:27.279: E/AndroidRuntime(1010): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 08-13 23:32:27.279: E/AndroidRuntime(1010): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 08-13 23:32:27.279: E/AndroidRuntime(1010): at android.app.ActivityThread.access$600(ActivityThread.java:141) 08-13 23:32:27.279: E/AndroidRuntime(1010): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 08-13 23:32:27.279: E/AndroidRuntime(1010): at android.os.Handler.dispatchMessage(Handler.java:99) 08-13 23:32:27.279: E/AndroidRuntime(1010): at android.os.Looper.loop(Looper.java:137) 08-13 23:32:27.279: E/AndroidRuntime(1010): at android.app.ActivityThread.main(ActivityThread.java:5039) 08-13 23:32:27.279: E/AndroidRuntime(1010): at java.lang.reflect.Method.invokeNative(Native Method) 08-13 23:32:27.279: E/AndroidRuntime(1010): at java.lang.reflect.Method.invoke(Method.java:511) 08-13 23:32:27.279: E/AndroidRuntime(1010): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 08-13 23:32:27.279: E/AndroidRuntime(1010): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 08-13 23:32:27.279: E/AndroidRuntime(1010): at dalvik.system.NativeStart.main(Native Method) 08-13 23:32:27.279: E/AndroidRuntime(1010): Caused by: java.lang.NullPointerException 08-13 23:32:27.279: E/AndroidRuntime(1010): at com.example.mymaps.MainActivity.onCreate(MainActivity.java:27) 08-13 23:32:27.279: E/AndroidRuntime(1010): at android.app.Activity.performCreate(Activity.java:5104) 08-13 23:32:27.279: E/AndroidRuntime(1010): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 08-13 23:32:27.279: E/AndroidRuntime(1010): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 08-13 23:32:27.279: E/AndroidRuntime(1010): ... 11 more
08-13 23:32:27.279:E / AndroidRuntime(1010):FATAL EXCEPTION:main 08-13 23:32:27.279:E / AndroidRuntime(1010):java.lang.RuntimeException:无法启动活动ComponentInfo {com。 example.mymaps / com.example.mymaps.MainActivity}:java.lang.NullPointerException 08-13 23:32:27.279:E / AndroidRuntime(1010):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)08 -13 23:32:27.279:E / AndroidRuntime(1010):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)08-13 23:32:27.279:E / AndroidRuntime(1010):at android.app .ActivityThread.access $ 600(ActivityThread.java:141)08-13 23:32:27.279:E / AndroidRuntime(1010):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1234)08-13 23: 32:27.279:E / AndroidRuntime(1010):在android.os.Handler.dispatchMessage(Handler.java:99)08-13 23:32:27.279:E / AndroidRuntime(1010):在android.os.Looper.loop (Looper.java:137)08-13 23:32:27.279:E / AndroidRuntime(1010):在android.app.ActivityThread.main(Activ) ityThread.java:5039)08-13 23:32:27.279:E / AndroidRuntime(1010):at java.lang.reflect.Method.invokeNative(Native Method)08-13 23:32:27.279:E / AndroidRuntime(1010) ):at java.lang.reflect.Method.invoke(Method.java:511)08-13 23:32:27.279:E / AndroidRuntime(1010):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run( ZygoteInit.java:793)08-13 23:32:27.279:E / AndroidRuntime(1010):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)08-13 23:32:27.279: E / AndroidRuntime(1010):at dalvik.system.NativeStart.main(Native Method)08-13 23:32:27.279:E / AndroidRuntime(1010):引起:java.lang.NullPointerException 08-13 23:32: 27.279:E / AndroidRuntime(1010):at com.example.mymaps.MainActivity.onCreate(MainActivity.java:27)08-13 23:32:27.279:E / AndroidRuntime(1010):at android.app.Activity.performCreate (Activity.java:5104)08-13 23:32:27.279:E / AndroidRuntime(1010):at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)08-13 23:32:27.279:E / AndroidRunti me(1010):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)08-13 23:32:27.279:E / AndroidRuntime(1010):... 11更多
2 个解决方案
#1
0
It may be due to play service issue. So null check map object before using map so it will automatically ask to download map in device. please try and reply
这可能是由于发挥服务问题。因此,在使用地图之前,请检查地图对象,以便自动要求在设备中下载地图。请尝试回复
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
if(map!=null){
Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
.title("Hamburg"));
Marker kiel = map.addMarker(new MarkerOptions()
.position(KIEL)
.title("Kiel")
.snippet("Kiel is cool")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.ic_launcher)));
// Move the camera instantly to hamburg with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));
// Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
}
}
#2
0
-
First check google map is
null
ornot null
using this method :首先使用此方法检查google地图为null或不为null:
If(gMap!=null)
-
Whether you have to download marker icon form URL (or) add marker image to the marker info window directly from URL. It's possible to get the network main thread exception.
是否必须下载标记图标表单URL(或)直接从URL将标记图像添加到标记信息窗口。可以获得网络主线程异常。
- Network main thread exception leads to marker image
null
so that you may get null pointer exception in google map
网络主线程异常导致标记图像为null,因此您可能会在谷歌地图中获得空指针异常
Solution:
add the following lines in your onCreate
method
在onCreate方法中添加以下行
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
#1
0
It may be due to play service issue. So null check map object before using map so it will automatically ask to download map in device. please try and reply
这可能是由于发挥服务问题。因此,在使用地图之前,请检查地图对象,以便自动要求在设备中下载地图。请尝试回复
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
if(map!=null){
Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
.title("Hamburg"));
Marker kiel = map.addMarker(new MarkerOptions()
.position(KIEL)
.title("Kiel")
.snippet("Kiel is cool")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.ic_launcher)));
// Move the camera instantly to hamburg with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));
// Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
}
}
#2
0
-
First check google map is
null
ornot null
using this method :首先使用此方法检查google地图为null或不为null:
If(gMap!=null)
-
Whether you have to download marker icon form URL (or) add marker image to the marker info window directly from URL. It's possible to get the network main thread exception.
是否必须下载标记图标表单URL(或)直接从URL将标记图像添加到标记信息窗口。可以获得网络主线程异常。
- Network main thread exception leads to marker image
null
so that you may get null pointer exception in google map
网络主线程异常导致标记图像为null,因此您可能会在谷歌地图中获得空指针异常
Solution:
add the following lines in your onCreate
method
在onCreate方法中添加以下行
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);