我已经阅读了文件 https://developers.google.com/maps/documentation/android/start 我在eclipse中创建了项目android,逐步创建了folow文档,但是当我无法运行项目时。
示例错误java.lang.NullPointerException
错误日志
03-20 15:13:39.332: D/AndroidRuntime(28930): Shutting down VM
03-20 15:13:39.332: W/dalvikvm(28930): threadid=1: thread exiting with uncaught exception (group=0x2b542210)
03-20 15:13:39.352: E/AndroidRuntime(28930): FATAL EXCEPTION: main
03-20 15:13:39.352: E/AndroidRuntime(28930): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.my_ap/com.mrbuoi.my_ap.MainActivity}: java.lang.NullPointerException
03-20 15:13:39.352: E/AndroidRuntime(28930): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
03-20 15:13:39.352: E/AndroidRuntime(28930): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
03-20 15:13:39.352: E/AndroidRuntime(28930): at android.app.ActivityThread.access$600(ActivityThread.java:127)
03-20 15:13:39.352: E/AndroidRuntime(28930): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
03-20 15:13:39.352: E/AndroidRuntime(28930): at android.os.Handler.dispatchMessage(Handler.java:99)
03-20 15:13:39.352: E/AndroidRuntime(28930): at android.os.Looper.loop(Looper.java:137)
03-20 15:13:39.352: E/AndroidRuntime(28930): at android.app.ActivityThread.main(ActivityThread.java:4441)
03-20 15:13:39.352: E/AndroidRuntime(28930): at java.lang.reflect.Method.invokeNative(Native Method)
03-20 15:13:39.352: E/AndroidRuntime(28930): at java.lang.reflect.Method.invoke(Method.java:511)
03-20 15:13:39.352: E/AndroidRuntime(28930): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-20 15:13:39.352: E/AndroidRuntime(28930): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565)
03-20 15:13:39.352: E/AndroidRuntime(28930): at dalvik.system.NativeStart.main(Native Method)
03-20 15:13:39.352: E/AndroidRuntime(28930): Caused by: java.lang.NullPointerException
03-20 15:13:39.352: E/AndroidRuntime(28930): at com.mrbuoi.my_ap.MainActivity.onCreate(MainActivity.java:31)
03-20 15:13:39.352: E/AndroidRuntime(28930): at android.app.Activity.performCreate(Activity.java:4465)
03-20 15:13:39.352: E/AndroidRuntime(28930): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
03-20 15:13:39.352: E/AndroidRuntime(28930): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
03-20 15:13:39.352: E/AndroidRuntime(28930): ... 11 more
-----文件AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.my_ap"
android:versionCode="1"
android:versionName="1.0" >
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="14" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<!-- The following two permissions are not required to use
Google Maps Android API v2, but are recommended. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data
android:name="com.google.android.gms.version"
android:value="15" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="MY_APIKEY"/>
<activity
android:name="com.mrbuoi.may_ap.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>
</application>
----文件MainActivity.java
package com.mrbuoi.my_ap;
import com.example.my_ap.R;
import com.google.android.gms.maps.*;
import com.google.android.gms.maps.model.*;
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Bundle;
import android.os.Build;
public class MainActivity extends Activity {
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GoogleMap map = ((MapFragment) getFragmentManager()
.findFragmentById(R.id.map)).getMap();
LatLng sydney = new LatLng(-33.867, 151.206);
map.setMyLocationEnabled(true);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));
map.addMarker(new MarkerOptions()
.title("Sydney")
.snippet("The most populous city in Australia.")
.position(sydney));
}
}
----- File Fragment_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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.mrbuoi.my_ap.MainActivity$PlaceholderFragment" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"/>
</RelativeLayout>
更新activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.mrbuoi.bactai_app.MainActivity"
tools:ignore="MergeRootFrame" />
有人有小费吗? Tks
更新
@PiYusH GuPtA,我试过这个,但这不起作用
我的错误日志
03-20 15:24:42.922: E/AndroidRuntime(29491): FATAL EXCEPTION: main
03-20 15:24:42.922: E/AndroidRuntime(29491): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.my_ap/com.mrbuoi.my_ap.MainActivity}: java.lang.NullPointerException
03-20 15:24:42.922: E/AndroidRuntime(29491): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
03-20 15:24:42.922: E/AndroidRuntime(29491): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
03-20 15:24:42.922: E/AndroidRuntime(29491): at android.app.ActivityThread.access$600(ActivityThread.java:127)
03-20 15:24:42.922: E/AndroidRuntime(29491): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
03-20 15:24:42.922: E/AndroidRuntime(29491): at android.os.Handler.dispatchMessage(Handler.java:99)
03-20 15:24:42.922: E/AndroidRuntime(29491): at android.os.Looper.loop(Looper.java:137)
03-20 15:24:42.922: E/AndroidRuntime(29491): at android.app.ActivityThread.main(ActivityThread.java:4441)
03-20 15:24:42.922: E/AndroidRuntime(29491): at java.lang.reflect.Method.invokeNative(Native Method)
03-20 15:24:42.922: E/AndroidRuntime(29491): at java.lang.reflect.Method.invoke(Method.java:511)
03-20 15:24:42.922: E/AndroidRuntime(29491): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-20 15:24:42.922: E/AndroidRuntime(29491): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565)
03-20 15:24:42.922: E/AndroidRuntime(29491): at dalvik.system.NativeStart.main(Native Method)
03-20 15:24:42.922: E/AndroidRuntime(29491): Caused by: java.lang.NullPointerException
03-20 15:24:42.922: E/AndroidRuntime(29491): at com.mrbuoi.my_ap.MainActivity.onCreate(MainActivity.java:22)
03-20 15:24:42.922: E/AndroidRuntime(29491): at android.app.Activity.performCreate(Activity.java:4465)
03-20 15:24:42.922: E/AndroidRuntime(29491): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
03-20 15:24:42.922: E/AndroidRuntime(29491): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
03-20 15:24:42.922: E/AndroidRuntime(29491): ... 11 more
答案 0 :(得分:1)
您需要为您的课程扩展 FragmentActivity ,而不是 活动 。
您必须添加的其他内容
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
在您的清单文件中。
同时更改此
if(map!=null){
LatLng sydney = new LatLng(-33.867, 151.206);
map.setMyLocationEnabled(true);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));
map.addMarker(new MarkerOptions()
.title("Sydney")
.snippet("The most populous city in Australia.")
.position(sydney));
}
的更新:强> 的
从此处更改
setContentView(R.layout.activity_main);
到
setContentView(R.layout.Fragment_main);
使用此
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.mrbuoi.bactai_app.MainActivity"
tools:ignore="MergeRootFrame" >
<include layout="@layout/Fragment_main"></include>
</FrameLayout>
答案 1 :(得分:1)
首先,您需要将Activity
更改为FragmentActivity
,同时在课堂上进行扩展,如下所示:
public class MainActivity extends FragmentActivity{
此外,包含GoogleMap
视图的布局展开的名称为Fragment_main
,并且在您的活动中,您已经夸大了错误的布局activity_main
。
因此,请更改setContentView
布局
setContentView(R.layout.activity_main);
到
setContentView(R.layout.Fragment_main);
此外,由于您的最低api级别低于11,因此您必须使用SupportMapFragment
加载地图,如下所示:
GoogleMap map = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
<强>编辑:强>
在你的清单中更改下面的行
<meta-data
android:name="com.google.android.gms.version"
android:value="15" />
到
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
同时在Fragment
布局文件中更改您的fragment_main
班级名称。
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"/>