如何使用Intent

时间:2017-11-26 18:17:13

标签: android google-maps

我创建了活动: MapsActivity ,它会在地图上显示我的位置。

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;
private LocationManager locationManager;


@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
}


@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false));
    double lat = location.getLatitude();
    double lng = location.getLongitude();

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

        return;
    }
    mMap.setMyLocationEnabled(true);
    // Add a marker in Sydney and move the camera
    LatLng place = new LatLng(lat,lng);
    mMap.addMarker(new MarkerOptions().position(place).title("Marker in Sydney"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(place));
}
}

我创建了 MainActivity ,带有一个按钮,我想点击该按钮移动到 MapsActivity ,这里是代码:

public class MainActivity extends AppCompatActivity {

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

public void showMyLocation(View view){
    Intent intentControls = new Intent(this, MapsActivity.class);
    startActivity(intentControls);
}
}

Gradle

    android {
    compileSdkVersion 27
    buildToolsVersion "27.0.1"
    defaultConfig {
        applicationId "com.example.lukasz.aaaaa"
        minSdkVersion 21
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })

    compile 'com.android.support:appcompat-v7:26.0.0-alpha1'
    compile 'com.google.android.gms:play-services-maps:10.2.6'
    compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha8'
    testCompile 'junit:junit:4.12'
}

AndroidManifest

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.lukasz.aaaaa">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />

    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_key" />

    <activity
        android:name=".MapsActivity"
        android:label="@string/title_activity_maps">
    </activity>

    <activity android:name=".MainActivity"
              android:label="Mapy">>
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>

问题是,在我单击MainActivity中的按钮后,应用程序崩溃了

  

致命的例外:主要                                                                           过程:com.example.lukasz.aaaaa,PID:2433                                                                           java.lang.IllegalArgumentException:invalid provider:null                                                                               在android.location.LocationManager.checkProvider(LocationManager.java:2099)                                                                               在android.location.LocationManager.getLastKnownLocation(LocationManager.java:1200)                                                                               在com.example.lukasz.aaaaa.MapsActivity.onMapReady(MapsActivity.java:56)                                                                               在com.google.android.gms.maps.SupportMapFragment $ zza $ 1.zza(未知来源:7)                                                                               在com.google.android.gms.maps.internal.zzt $ zza.onTransact(未知来源:30)                                                                               在android.os.Binder.transact(Binder.java:627)                                                                               在gl.b(:com.google.android.gms.DynamiteModulesB@11577470:20)                                                                               在com.google.android.gms.maps.internal.bf.a(:com.google.android.gms.DynamiteModulesB@11577470:5)                                                                               在com.google.maps.api.android.lib6.impl.bc.run(:com.google.android.gms.DynamiteModulesB@11577470:5)                                                                               在android.os.Handler.handleCallback(Handler.java:790)                                                                               在android.os.Handler.dispatchMessage(Handler.java:99)                                                                               在android.os.Looper.loop(Looper.java:164)                                                                               在android.app.ActivityThread.main(ActivityThread.java:6494)                                                                               at java.lang.reflect.Method.invoke(Native Method)                                                                               在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:438)                                                                               在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)r

我的问题是如何使用Intent从其他活动运行Google Map Activity?

1 个答案:

答案 0 :(得分:0)

在应用程序标记

之上保留以下权限
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
        <uses-permission android:name="android.permission.INTERNET" />
     <application
          ......
          .....
    <activity>

          .....
   </activity>

</application>

<强>(或)

  

由于无法使用,您最有可能收到此错误   GPS Providers。如果Gps,请让此人检查设置   提供商或网络提供商已启用Location   设置。