Java 映射活动出错
我开发了一个简单的mapActivity,让用户通过使用对话框警报和定位服务的意图来检查是否启用了gps系统。如果gps处于启用状态,它将直接进入地图视图,但我不会这样做。当我按下进入地图视图时,它使我强制关闭 这是对话框代码:Java 映射活动出错,java,android,google-maps,map,Java,Android,Google Maps,Map,我开发了一个简单的mapActivity,让用户通过使用对话框警报和定位服务的意图来检查是否启用了gps系统。如果gps处于启用状态,它将直接进入地图视图,但我不会这样做。当我按下进入地图视图时,它使我强制关闭 这是对话框代码: public void onClick(View arg0) { AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
public void onClick(View arg0) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set title
alertDialogBuilder.setTitle("Your Title");
// set dialog message
alertDialogBuilder
.setMessage("Click yes TO ENABLE GPS")
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
// if this button is clicked, close
// current activity
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(intent, 1);
}
})
.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
// if this button is clicked, just close
// the dialog box and do nothing
//dialog.cancel();
Intent intent = new Intent(MainActivity.this, AndroidGoogleMapsActivity.class);
context.startActivity(intent);
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
}
这是清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mkyong.android"
android:versionCode="1"
android:versionName="1.0" ><uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<uses-library
android:name="com.google.android.maps"
android:required="true" />
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".AndroidGoogleMapsActivity" >
</activity>
</application>
</manifest>
}
这就是我得到的错误:
09-20 05:16:22.512: D/AndroidRuntime(309): Shutting down VM
09-20 05:16:22.512: W/dalvikvm(309): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
09-20 05:16:22.543: E/AndroidRuntime(309): FATAL EXCEPTION: main
09-20 05:16:22.543: E/AndroidRuntime(309): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mkyong.android/com.mkyong.android.AndroidGoogleMapsActivity}: java.lang.NullPointerException
09-20 05:16:22.543: E/AndroidRuntime(309): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
09-20 05:16:22.543: E/AndroidRuntime(309): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-20 05:16:22.543: E/AndroidRuntime(309): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-20 05:16:22.543: E/AndroidRuntime(309): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-20 05:16:22.543: E/AndroidRuntime(309): at android.os.Handler.dispatchMessage(Handler.java:99)
09-20 05:16:22.543: E/AndroidRuntime(309): at android.os.Looper.loop(Looper.java:123)
09-20 05:16:22.543: E/AndroidRuntime(309): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-20 05:16:22.543: E/AndroidRuntime(309): at java.lang.reflect.Method.invokeNative(Native Method)
09-20 05:16:22.543: E/AndroidRuntime(309): at java.lang.reflect.Method.invoke(Method.java:521)
09-20 05:16:22.543: E/AndroidRuntime(309): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-20 05:16:22.543: E/AndroidRuntime(309): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-20 05:16:22.543: E/AndroidRuntime(309): at dalvik.system.NativeStart.main(Native Method)
09-20 05:16:22.543: E/AndroidRuntime(309): Caused by: java.lang.NullPointerException
09-20 05:16:22.543: E/AndroidRuntime(309): at com.mkyong.android.AndroidGoogleMapsActivity.onCreate(AndroidGoogleMapsActivity.java:27)
09-20 05:16:22.543: E/AndroidRuntime(309): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-20 05:16:22.543: E/AndroidRuntime(309): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-20 05:16:22.543: E/AndroidRuntime(309): ... 11 more
09-20 05:16:25.141: I/Process(309): Sending signal. PID: 309 SIG: 9
似乎布局文件中存在导致此NullPointerException的问题
// This command tries to get the mapView but fails to find it, so it returns null, instead of the mapView
MapView mapView = (MapView) findViewById(R.id.mapView);
// This tries to call a method on null, which will cause a NullPointerException.
mapView.setBuiltInZoomControls(true);
要解决此问题,请检查layout/map\u activity.xml
中的地图是否实际命名为mapView,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment" />
抱歉,各位,我是这个论坛的新手,谢谢你们的帮助问题出在您的AndroidGoogleMapsActivity.onCreate()方法中,如果可能的话,请将该方法的代码与行号一起发布,或者告诉我们第27行是哪一行,因为这是通过NullPointerException传递的好的,我用类编辑了我的问题,我知道错误在第27行,但我不知道如何修复。谢谢,请告诉我们第27行是哪一行代码。如中所示,第27行的内容是什么?顺便说一句,堆栈溢出不是一个论坛。论坛主要以讨论为基础,并且倾向于遵循关于帖子内容的不太严格的规则。对于堆栈溢出(以及一般的堆栈交换),我们要求每个新线程都以一个问题开始,并且每个对该问题的响应都是试图回答它。
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment" />