在mainActivity上设置新的fragmentActivity

时间:2013-06-17 19:28:17

标签: android google-maps android-fragmentactivity

在我的项目中,我应该使用谷歌地图API,所以我已经有了一个类Maps.class,并且在我的MainActivity上我应该在一个事件后使用该地图,我不知道为什么这不起作用,但是我真的不知道如何实现它。 (该事件使用fragmentactivity调用Maps类以在屏幕上显示它。)

地图类:

import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.Marker;

public class Maps extends FragmentActivity {

GoogleMap googleMap;

Marker marker = null;

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

    // Getting Google Play availability status
    int status = GooglePlayServicesUtil
            .isGooglePlayServicesAvailable(getBaseContext());

    // Showing status
    if (status != ConnectionResult.SUCCESS) { // Google Play Services are
                                                // not available

        int requestCode = 10;
        Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this,
                requestCode);
        dialog.show();

    } else { // Google Play Services are available

        // Getting reference to the SupportMapFragment of activity_main.xml
        SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);

        // Getting GoogleMap object from the fragment
        googleMap = fm.getMap();
    }
}
}

MainActivity类:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.Marker;

public class MainActivity extends FragmentActivity {

GoogleMap googleMap;

Marker marker = null;

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

public void teste(View view) {
    Maps a = new Maps();
    MainActivity.this.finish();
}
}

编辑:我的新清单xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mzubair.mapkey"
android:versionCode="1"
android:versionName="1.0" >

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

<!-- android:name="YOUR_PACKAGE_NAME.permission.MAPS_RECEIVE" -->
<permission
    android:name="com.mzubair.mapkey.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.mzubair.mapkey.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission   android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

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

<application
    android:icon="@drawable/common_signin_btn_icon_disabled_dark"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".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>
    <activity
        android:name=".Maps"
        android:label="@string/app_name" >
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyCzeIVpUbL-HnxloaRbsOGxJbnAfP-tmpk" />
    </activity>
</application>

</manifest>

1 个答案:

答案 0 :(得分:1)

Maps a = new Maps();

这不是我们开展活动的方式。

请改用此代码:

Intent intent = new Intent(this, Maps.class);
startActivity(intent);

请务必将Maps添加到AndroidManifest.xml

此外,您可能不希望在两个活动中使用相同的布局,因此请更改此行:

setContentView(R.layout.activity_main);

Maps.java中的其他内容。

玩得开心学习Android。