我尝试使用NEXUS 7 google和android版本5.1.1在平板电脑设备中安装我的android -studio apk。应用程序根本不加载我的地图,显示的信息是“应用已停止”。
我在NEXUS 7平板电脑上尝试了我的应用程序的旧私有版本并且它是成功的,唯一的区别是我不显示Toast消息。 Toast消息指示用户设备中是否可以连接Wi-Fi或移动设备。请在下面找到代码:
private void setUpMap() {
ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (conMan != null) {
//mobile
NetworkInfo.State mobile = conMan.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
//wifi
NetworkInfo.State wifi = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
if (mobile == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTED)
{
Toast.makeText(getApplicationContext(),"Connection available",Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(),"Connection not available",Toast.LENGTH_LONG).show();
}
} // end if
else
{ //do nothing
}
// set position, title, snippet and icon for each marker
mMap.addMarker(new MarkerOptions().position(new LatLng(X, Y)).title("NAME").snippet("SURNAME").icon(BitmapDescriptorFactory.fromResource(R.drawable.location_icon)));
.....
...
} // END SETUPMAP
如果conMan不为null,则执行检查(wi-fi或mobile),否则不执行任何操作并继续在地图上加载标记。对于NEXUS 7,检查应该输入到else,因为conMan为null,并且应该继续加载标记。但仍然无法正常工作,该应用程序在NEXUS 7中崩溃..你能告诉我该怎么办?
java.lang.RuntimeException: Unable to start activity
ComponentInfo{project.name/project.name.MapsActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2122)
at android.app.ActivityThread.access$600(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4895)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at project.name.MapsActivity.setUpMap(MapsActivity.java:147)
at project.name.MapsActivity.setUpMapIfNeeded(MapsActivity.java:64)
at project.name.MapsActivity.onCreate(MapsActivity.java:43)
at android.app.Activity.performCreate(Activity.java:5163)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2061)
... 11 more
以下是第147行:
private void setUpMap() {
ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (conMan != null) { //line 147
//mobile
NetworkInfo.State mobile = conMan.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
答案 0 :(得分:1)
Nexus 7 wifi将返回null:
conMan.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
因为它的wifi并不支持移动数据, 所以你必须检查该行是否为Null然后得到它的状态。
ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if(conMan != null)
{
NetworkInfo inf = conMan.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if(inf != null) {
NetworkInfo.State mobile = inf.getState();
}
}