如何解决"java.lang.RuntimeException"错误



我是安卓新手。我构建了一个应用程序,其中有一个启动ActivityButton,并且该Activity中还有两个Buttons将打开两个单独的活动。其中一个Activity包含名为附近搜索的谷歌地图。当我开始附近的搜索时,应用程序崩溃,而这个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>

你需要改变这个

<uses-sdk
  android:minSdkVersion="8"

<uses-sdk
  android:minSdkVersion="12"

您应该从 11 以上的 API 级别使用MapFragment

看起来GooleMap对象maps为空。最好在初始化地图对象之前检查谷歌播放服务的可用性。

查看检查谷歌播放服务

http://developer.android.com/training/location/retrieve-current.html

跟随

https://developers.google.com/maps/documentation/android/start#getting_the_google_maps_android_api_v2

公共最终谷歌地图获取地图 ()

获取与由此包装的视图相关联的基础 GoogleMap 片段。

返回谷歌地图。如果片段的视图尚未为空 准备。如果片段生命周期未经过,则可能会发生这种情况 onCreateView(LayoutInflater, ViewGroup, Bundle)这也可以 如果谷歌播放服务不可用,则会发生。如果谷歌播放 之后服务变得可用,片段已经消失 通过onCreateView(LayoutInflater,ViewGroup,Bundle),调用这个 方法将再次初始化并返回谷歌地图。

相关内容

  • 没有找到相关文章

最新更新