我是android的新手。我构建了一个应用程序,其中Button
开始Activity
,Buttons
中还有两个Activity
将打开两个单独的活动。其中一个Activity
包含名为附近搜索的Google地图。当我开始附近的搜索时,应用程序崩溃,而Activity
在整合地图之前运行完美。
以下是日志猫
04-02 02:32:40.354: E/AndroidRuntime(22037): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jamaat_times/com.example.jamaattiming.NearbySearch}: java.lang.NullPointerException
04-02 02:32:40.354: E/AndroidRuntime(22037): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
04-02 02:32:40.354: E/AndroidRuntime(22037): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
04-02 02:32:40.354: E/AndroidRuntime(22037): at android.app.ActivityThread.access$600(ActivityThread.java:162)
04-02 02:32:40.354: E/AndroidRuntime(22037): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
04-02 02:32:40.354: E/AndroidRuntime(22037): at android.os.Handler.dispatchMessage(Handler.java:107)
04-02 02:32:40.354: E/AndroidRuntime(22037): at android.os.Looper.loop(Looper.java:194)
04-02 02:32:40.354: E/AndroidRuntime(22037): at android.app.ActivityThread.main(ActivityThread.java:5371)
04-02 02:32:40.354: E/AndroidRuntime(22037): at java.lang.reflect.Method.invokeNative(Native Method)
04-02 02:32:40.354: E/AndroidRuntime(22037): at java.lang.reflect.Method.invoke(Method.java:525)
04-02 02:32:40.354: E/AndroidRuntime(22037): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
04-02 02:32:40.354: E/AndroidRuntime(22037): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
04-02 02:32:40.354: E/AndroidRuntime(22037): at dalvik.system.NativeStart.main(Native Method)
04-02 02:32:40.354: E/AndroidRuntime(22037): Caused by: java.lang.NullPointerException
04-02 02:32:40.354: E/AndroidRuntime(22037): at com.example.jamaattiming.NearbySearch.onCreate(NearbySearch.java:36)
04-02 02:32:40.354: E/AndroidRuntime(22037): at android.app.Activity.performCreate(Activity.java:5122)
04-02 02:32:40.354: E/AndroidRuntime(22037): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
04-02 02:32:40.354: E/AndroidRuntime(22037): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
04-02 02:32:40.354: E/AndroidRuntime(22037): ... 11 more
这是java文件:
public class NearbySearch extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nearby_search);
GoogleMapOptions mapOptions = new GoogleMapOptions();
GoogleMap maps=(((MapFragment) getFragmentManager().findFragmentById(R.id.map2)).getMap());
mapOptions.mapType(GoogleMap.MAP_TYPE_HYBRID);
//maps.setMapType(GoogleMap.MAP_TYPE_HYBRID);
maps.setMyLocationEnabled(true);
maps.addMarker(new MarkerOptions()
.position(new LatLng(24.9967 , 66.1234))
.title("Hello world"));
}
}
这是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:orientation="vertical"
android:background="#808080">
<fragment
android:id="@+id/map2"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
这是清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jamaat_times"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="info.androidhive.googlemapsv2.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_jamaat"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.jamaattiming.Splash"
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="com.example.jamaattiming.MainPage"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.example.CLEARSCREEN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.jamaattiming.Qibla"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="com.example.COMPASS" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.jamaattiming.JamaatFinder"
android:label="@string/title_activity_jamaat_finder" >
</activity>
<activity
android:name="com.example.jamaattiming.QiblaFinder"
android:label="@string/title_activity_qibla_finder" >
</activity>
<activity
android:name="com.example.jamaattiming.TagYourself"
android:label="@string/title_activity_tag_yourself" >
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="my key" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name="com.example.jamaattiming.NearbySearch"
android:label="@string/title_activity_nearby_search" >
</activity>
<activity
android:name="com.example.jamaattiming.ManualSearch"
android:label="@string/title_activity_manual_search" >
</activity>
</application>
</manifest>
答案 0 :(得分:0)
您需要更改此
<uses-sdk
android:minSdkVersion="8"
到
<uses-sdk
android:minSdkVersion="12"
你应该从11以上的api级别使用MapFragment
看起来GooleMap
对象maps
为空。最好在初始化地图对象之前检查Google Play服务的可用性。
查看检查Google Play服务
http://developer.android.com/training/location/retrieve-current.html
请
公共最终GoogleMap getMap()
获取与此包装的视图关联的基础GoogleMap 片段。
返回GoogleMap。如果片段的视图尚未显示为空 准备。如果片段生命周期没有通过,就会发生这种情况 onCreateView(LayoutInflater,ViewGroup,Bundle)呢。这也可以 如果Google Play服务不可用,则会发生。如果谷歌播放 服务随后可用,片段已经消失 通过onCreateView(LayoutInflater,ViewGroup,Bundle),调用它 方法将再次初始化并返回GoogleMap。