0

我刚开始学习安卓。我整天都在尝试在 Android 上使用谷歌地图,但每次我在模拟器上运行它时,它都会发出“不幸的是,已停止”的通知。这是我的代码: 1. 清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mapmapmap"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="17"
        android:targetSdkVersion="17" />

    <permission
        android:name="com.example.mapdemo.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.example.mapdemo.permission.MAPS_RECEIVE" />
    <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" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.mapmapmap.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>

        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyCSOXImKxtdCbhph6WAW6QammSm7rDa9lA" />
    </application>
</manifest>

活动主.xml:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />

MainActivity.java:

package com.example.mapmapmap;
import android.os.Bundle;
import android.app.Activity;

public class MainActivity extends Activity {

@Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }
}

日志猫:

04-26 09:32:54.001: E/Trace(2210): error opening trace file: No such file or directory (2)
04-26 09:32:55.111: D/AndroidRuntime(2210): Shutting down VM
04-26 09:32:55.111: W/dalvikvm(2210): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
04-26 09:32:55.141: E/AndroidRuntime(2210): FATAL EXCEPTION: main
04-26 09:32:55.141: E/AndroidRuntime(2210): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mapmapmap/com.example.mapmapmap.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
04-26 09:32:55.141: E/AndroidRuntime(2210):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-26 09:32:55.141: E/AndroidRuntime(2210):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-26 09:32:55.141: E/AndroidRuntime(2210):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-26 09:32:55.141: E/AndroidRuntime(2210):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-26 09:32:55.141: E/AndroidRuntime(2210):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-26 09:32:55.141: E/AndroidRuntime(2210):     at android.os.Looper.loop(Looper.java:137)
04-26 09:32:55.141: E/AndroidRuntime(2210):     at android.app.ActivityThread.main(ActivityThread.java:5041)
04-26 09:32:55.141: E/AndroidRuntime(2210):     at java.lang.reflect.Method.invokeNative(Native Method)
04-26 09:32:55.141: E/AndroidRuntime(2210):     at java.lang.reflect.Method.invoke(Method.java:511)
04-26 09:32:55.141: E/AndroidRuntime(2210):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-26 09:32:55.141: E/AndroidRuntime(2210):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-26 09:32:55.141: E/AndroidRuntime(2210):     at dalvik.system.NativeStart.main(Native Method)
04-26 09:32:55.141: E/AndroidRuntime(2210): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
04-26 09:32:55.141: E/AndroidRuntime(2210):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
04-26 09:32:55.141: E/AndroidRuntime(2210):     at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
04-26 09:32:55.141: E/AndroidRuntime(2210):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
04-26 09:32:55.141: E/AndroidRuntime(2210):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
04-26 09:32:55.141: E/AndroidRuntime(2210):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
04-26 09:32:55.141: E/AndroidRuntime(2210):     at android.app.Activity.setContentView(Activity.java:1881)
04-26 09:32:55.141: E/AndroidRuntime(2210):     at com.example.mapmapmap.MainActivity.onCreate(MainActivity.java:11)
04-26 09:32:55.141: E/AndroidRuntime(2210):     at android.app.Activity.performCreate(Activity.java:5104)
04-26 09:32:55.141: E/AndroidRuntime(2210):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-26 09:32:55.141: E/AndroidRuntime(2210):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-26 09:32:55.141: E/AndroidRuntime(2210):     ... 11 more
04-26 09:32:55.141: E/AndroidRuntime(2210): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.MapFragment: make sure class name exists, is public, and has an empty constructor that is public
04-26 09:32:55.141: E/AndroidRuntime(2210):     at android.app.Fragment.instantiate(Fragment.java:592)
04-26 09:32:55.141: E/AndroidRuntime(2210):     at android.app.Fragment.instantiate(Fragment.java:560)
04-26 09:32:55.141: E/AndroidRuntime(2210):     at android.app.Activity.onCreateView(Activity.java:4709)
04-26 09:32:55.141: E/AndroidRuntime(2210):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
04-26 09:32:55.141: E/AndroidRuntime(2210):     ... 20 more
04-26 09:32:55.141: E/AndroidRuntime(2210): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.maps.MapFragment" on path: /data/app/com.example.mapmapmap-1.apk
04-26 09:32:55.141: E/AndroidRuntime(2210):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
04-26 09:32:55.141: E/AndroidRuntime(2210):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
04-26 09:32:55.141: E/AndroidRuntime(2210):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
04-26 09:32:55.141: E/AndroidRuntime(2210):     at android.app.Fragment.instantiate(Fragment.java:582)
04-26 09:32:55.141: E/AndroidRuntime(2210):     ... 23 more

我非常感谢任何帮助的尝试!

4

2 回答 2

2
Try This code will work succesfully


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment" />

</LinearLayout>
package com.towntour.android;

import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton; 
import android.widget.TextView;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.InfoWindowAdapter;
import com.google.android.gms.maps.GoogleMap.OnInfoWindowClickListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory; 
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

public class PlacesMapActivity extends android.support.v4.app.FragmentActivity
    implements OnClickListener, LocationListener {
/**
 * Note that this may be null if the Google Play services APK is not
 * available.
 */
ImageButton btn_home;
private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);

    SupportMapFragment fragment = (SupportMapFragment)getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mMap = fragment.getMap();
    mMap.setMyLocationEnabled(true);

    // mMap = ((SupportMapFragment) getSupportFragmentManager()
    // .findFragmentById(R.id.map)).getMap();

    MarkerOptions markerOptions = new MarkerOptions();
    markerOptions.title("First Location");
    markerOptions.snippet("This Is Test Location");

    LatLng latlng = new LatLng(23.0333, 72.6167);

    markerOptions.position(latlng);
    // markerOptions.title("Ahmedabad Cordinat Found here");

    // Marker m = mMap.addMarker(markerOptions);

    mMap.setInfoWindowAdapter(new InfoWindowAdapter() {

        @Override
        public View getInfoWindow(Marker arg0) {

            return null;
        }

        @Override
        public View getInfoContents(Marker marker) {

            View myContentView = getLayoutInflater().inflate(
                    R.layout.custommarker, null);
            TextView tvTitle = ((TextView) myContentView
                    .findViewById(R.id.title));
            tvTitle.setText(marker.getTitle());
            TextView tvSnippet = ((TextView) myContentView
                    .findViewById(R.id.snippet));
            tvSnippet.setText(marker.getSnippet());
            return myContentView;

        }
    });

    mMap.addMarker(new MarkerOptions()
            .position(latlng)
            .title("This is Sabarmati Ashram")
            .snippet("Ahmedabad")
            .icon(BitmapDescriptorFactory
                .defaultMarker(BitmapDescriptorFactory.HUE_RED)));

    mMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {

        @Override
        public void onInfoWindowClick(Marker arg0) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(getBaseContext(),
                    DetailsOfPlacesActivity.class);
            startActivity(intent);
        }
    });

    btn_home = (ImageButton) findViewById(R.id.activity_map_ibtn_home);
    btn_home.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            finish();
        }
    });
}

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    finish();
    super.onBackPressed();
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    finish();
    super.onPause();
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

}

@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}
}





<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.towntour.android"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="com.example.towntour.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

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

<permission
    android:name="com.towntour.android.maps.googlev2.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.towntour.android.maps.googlev2.permission.MAPS_RECEIVE" 

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.Black.NoTitleBar" >
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="AIzaSyDp6PHK3NYNwS74cKGmX-xc4LuH_uM1mIo" />

    <activity
        android:name="com.towntour.android.PlaceActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>
于 2013-07-12T11:51:04.393 回答
1

您的错误表明您手动修改了项目的构建路径以引用 JAR。那将代表两个错误:

  1. 永远不要手动修改标准 Android 应用项目的构建路径

  2. Play Services SDK 是一个 Android 库项目,而不仅仅是一个 JAR

可以在Play Services SDK 文档中找到将 Play Services SDK 附加到您的项目的说明。

于 2013-04-26T17:19:23.130 回答