我需要使用supportMapfragment在Android GooglemAp代码上提供帮助

时间:2014-03-19 15:38:19

标签: java android xml

我的代码是

`

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab1_layout);

    try {
        // Loading map
        //initilizeMap();

    } catch (Exception e) {
        e.printStackTrace();
    }

}

/**
 * function to load map. If map is not created it will create it for you
 * */
private void initilizeMap() {

        //
        FragmentManager fragmentManager = getSupportFragmentManager();
        SupportMapFragment mapFragment = (SupportMapFragment) fragmentManager
                .findFragmentById(R.id.map);
        googleMap = mapFragment.getMap();
        googleMap.getUiSettings().setMyLocationButtonEnabled(true);
        googleMap.setMyLocationEnabled(true);
        googleMap.getUiSettings().setCompassEnabled(true);

        CameraPosition cameraPosition = new CameraPosition.Builder()
                .target(new LatLng(7.0722388, 125.6125396)).zoom(12)
                .build();
        googleMap.setMapType(mapType);
        googleMap.animateCamera(CameraUpdateFactory
                .newCameraPosition(cameraPosition));
        // latitude and longitude
        double latitude = 7.0861111;
        double longitude = 125.4866666;
        double latitude1 = 7.3142777;
        double longitude1 = 125.6693055;
        double latitude2 = 6.9973333;
        double longitude2 = 125.4896666;
        double latitude3 = 7.2653333;
        double longitude3 = 125.6656333;
        double latitude4 = 7.27905557;
        double longitude4 = 125.3088333;
        double latitude5 = 6.6912333;
        double longitude5 = 125.1862;
        double latitude6 = 7.0722388;
        double longitude6 = 125.6125396;
        double latitude7 = 7.1759166;
        double longitude7 = 125.6333;
        double latitude8 = 7.0601333;
        double longitude8 = 125.59775;
        double latitude9 = 7.0003333;
        double longitude9 = 125.4538;

        // create marker
        MarkerOptions marker = new MarkerOptions()
                .position(new LatLng(latitude, longitude))
                .title("AWS03 - UPMin").snippet("Tugbok Dist., Davao City");
        MarkerOptions marker1 = new MarkerOptions()
                .position(new LatLng(latitude1, longitude1))
                .title("AWS04 - DNSC").snippet("Panabo, Davao del Norte");
        MarkerOptions marker2 = new MarkerOptions()
                .position(new LatLng(latitude2, longitude2))
                .title("AWS07 - Unlad Kabayan")
                .snippet("Toril, Davao City");
        MarkerOptions marker3 = new MarkerOptions()
                .position(new LatLng(latitude3, longitude3))
                .title("AWS09 - Lasang")
                .snippet("Lasang NHS, Lasang, Davao City");
        MarkerOptions marker4 = new MarkerOptions()
                .position(new LatLng(latitude4, longitude4))
                .title("AWS0A - Marilog")
                .snippet("Marilog Dist., Davao City");
        MarkerOptions marker5 = new MarkerOptions()
                .position(new LatLng(latitude5, longitude5))
                .title("AWS0B - NIA")
                .snippet("Kapoc, Matanao, Davao del Sur");
        MarkerOptions marker6 = new MarkerOptions()
                .position(new LatLng(latitude6, longitude6))
                .title("AWS0C - AdDU")
                .snippet("AdDU, Roxas Ave., Davao City");
        MarkerOptions marker7 = new MarkerOptions()
                .position(new LatLng(latitude7, longitude7))
                .title("AWS11 - Mudiang")
                .snippet("Brgy. Mudiang, Davao City");
        MarkerOptions marker8 = new MarkerOptions()
                .position(new LatLng(latitude8, longitude8))
                .title("AWS12 - AdDU HS")
                .snippet("AdDU High School, Matina");
        MarkerOptions marker9 = new MarkerOptions()
                .position(new LatLng(latitude9, longitude9))
                .title("AWS14 - Catigan")
                .snippet("Catigan, Toril, Davao City");

        //

        // adding marker
        googleMap.addMarker(marker);
        googleMap.addMarker(marker1);
        googleMap.addMarker(marker2);
        googleMap.addMarker(marker3);
        googleMap.addMarker(marker4);
        googleMap.addMarker(marker5);
        googleMap.addMarker(marker6);
        googleMap.addMarker(marker7);
        googleMap.addMarker(marker8);
        googleMap.addMarker(marker9);

        // check if map is created successfully or not
        /*if (googleMap == null) {
            Toast.makeText(getApplicationContext(),
                    "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                    .show();
        }*/
    }


@Override
protected void onResume() {
    super.onResume();
    initilizeMap();

}

我也一直收到这个错误 java.lang.RuntimeException:无法启动活动ComponentInfo {}:android.view.InflateException:二进制XML文件行#6:错误膨胀类片段  谁能告诉我我的代码有什么问题?

这是我的LOG

01-01 10:51:37.820: E/liblog(1486): failed to call dumpstate

01-01 10:51:37.820:E / AndroidRuntime(9040):致命异常:主要 01-01 10:51:37.820:E / AndroidRuntime(9040):java.lang.RuntimeException:无法恢复活动{com.thesis.awsapp / com.thesis.awsapp.Tab1Activity}:java.lang.NullPointerException 01-01 10:51:37.820:E / AndroidRuntime(9040):在android.app.ActivityThread.performResumeActivity(ActivityThread.java:2124) 01-01 10:51:37.820:E / AndroidRuntime(9040):在android.app.LocalActivityManager.moveToState(LocalActivityManager.java:138) 01-01 10:51:37.820:E / AndroidRuntime(9040):在android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339) 01-01 10:51:37.820:E / AndroidRuntime(9040):在android.widget.TabHost $ IntentContentStrategy.getContentView(TabHost.java:676) 01-01 10:51:37.820:E / AndroidRuntime(9040):在android.widget.TabHost.setCurrentTab(TabHost.java:348) 01-01 10:51:37.820:E / AndroidRuntime(9040):在android.widget.TabHost $ 2.onTabSelectionChanged(TabHost.java:141) 01-01 10:51:37.820:E / AndroidRuntime(9040):在android.widget.TabWidget $ TabClickListener.onClick(TabWidget.java:456) 01-01 10:51:37.820:E / AndroidRuntime(9040):在android.view.View.performClick(View.java:2485) 01-01 10:51:37.820:E / AndroidRuntime(9040):在android.view.View $ PerformClick.run(View.java:9080) 01-01 10:51:37.820:E / AndroidRuntime(9040):在android.os.Handler.handleCallback(Handler.java:587) 01-01 10:51:37.820:E / AndroidRuntime(9040):在android.os.Handler.dispatchMessage(Handler.java:92) 01-01 10:51:37.820:E / AndroidRuntime(9040):在android.os.Looper.loop(Looper.java:130) 01-01 10:51:37.820:E / AndroidRuntime(9040):在android.app.ActivityThread.main(ActivityThread.java:3687) 01-01 10:51:37.820:E / AndroidRuntime(9040):at java.lang.reflect.Method.invokeNative(Native Method) 01-01 10:51:37.820:E / AndroidRuntime(9040):at java.lang.reflect.Method.invoke(Method.java:507) 01-01 10:51:37.820:E / AndroidRuntime(9040):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:867) 01-01 10:51:37.820:E / AndroidRuntime(9040):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 01-01 10:51:37.820:E / AndroidRuntime(9040):at dalvik.system.NativeStart.main(Native Method) 01-01 10:51:37.820:E / AndroidRuntime(9040):引起:java.lang.NullPointerException 01-01 10:51:37.820:E / AndroidRuntime(9040):at com.thesis.awsapp.Tab1Activity.initilizeMap(Tab1Activity.java:43) 01-01 10:51:37.820:E / AndroidRuntime(9040):at com.thesis.awsapp.Tab1Activity.onResume(Tab1Activity.java:141) 01-01 10:51:37.820:E / AndroidRuntime(9040):在android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1150) 01-01 10:51:37.820:E / AndroidRuntime(9040):在android.app.Activity.performResume(Activity.java:3832) 01-01 10:51:37.820:E / AndroidRuntime(9040):在android.app.ActivityThread.performResumeActivity(ActivityThread.java:2114) 01-01 10:51:37.820:E / AndroidRuntime(9040):...还有17个 01-01 10:51:37.828:E /(1486):Dumpstate> /数据/数/ dumpstate_app_error 01-01 10:51:38.171:E / StatusBarPolicy(1539):ecio:23 01-01 10:51:38.171:E / StatusBarPolicy(1539):iconLevel:4

任何人都可以请求我!

我的清单

 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<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="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.thesis.awsapp.permission.MAPS_RECEIVE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

<application
    android:allowBackup="true"
    android:debuggable="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <!-- Splash SCreen -->
    <activity
        android:name=".Splash"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.Black.NoTitleBar" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <!-- MAIN ACTIVTY -->
    <activity
        android:name=".MainScreen"
        android:label="@string/app_name" >
    </activity>
    <!-- About Activity -->
    <activity
        android:name=".AboutScreen"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.about_layout" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <!-- Tab1 Activity -->
    <activity
        android:name=".Tab1Activity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.tab1_layout" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <!-- Tab2 Activity -->
</application>

<meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />
<meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="YourApiKey" />

1 个答案:

答案 0 :(得分:0)

阅读错误消息,它说明了您需要做的事情:

  

01-01 10:11:20.570:E / AndroidRuntime(8222):引起:   java.lang.IllegalStateException:您的必需元数据标记   应用程序的AndroidManifest.xml不存在。您必须具备以下条件   元素内的声明:01-01   10:11:20.570:E / AndroidRuntime(8222):at   com.google.android.gms.common.GooglePlayServicesUtil.n(未知来源)   01-01 10:11:20.570:E / AndroidRuntime(8222):at   com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(未知   来源)01-01 10:11:20.570:E / AndroidRuntime(8222):at   com.google.android.gms.maps.internal.q.v(未知来源)0

另外,请确保将元数据节点放在标记内,而不是放在标记之外。

<application
    android:allowBackup="true"
    android:debuggable="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="@integer/google_play_services_version" />
<meta-data
 android:name="com.google.android.maps.v2.API_KEY"
    android:value="YourKeyHere" />


    <!-- Splash SCreen -->
    <activity
        android:name=".Splash"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.Black.NoTitleBar" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <!-- MAIN ACTIVTY -->
    <activity
        android:name=".MainScreen"
        android:label="@string/app_name" >
    </activity>
    <!-- About Activity -->
    <activity
        android:name=".AboutScreen"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.about_layout" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <!-- Tab1 Activity -->
    <activity
        android:name=".Tab1Activity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.tab1_layout" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <!-- Tab2 Activity -->
</application>