使用谷歌地图片段时,应用程序崩溃

时间:2013-09-30 12:12:08

标签: android android-fragments fragment google-maps-android-api-2 android-fragmentactivity

当我使用片段时,我的应用程序崩溃,而且它明显与其中一个片段中的GoogleMap相关。

我已经将地图片段从MapFragment更改为SupportMapFragment,但它没有帮助。

这是所有片段所在的主页面(FragmentActivity):

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;

public class Fragments extends FragmentActivity {

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

        MainActivity fragment = new MainActivity();
        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction();
        transaction.add(R.id.fragment_place, fragment);
        transaction.commit();
    }

    public void onSelectFragment(View view) {

        Fragment newFragment;

        if (view == findViewById(R.id.add)) {
            newFragment = new Add();
        } else if (view == findViewById(R.id.map)) {
            newFragment = new MainActivity();
        } else {
            newFragment = new MainActivity();
        }

        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction();
        transaction.replace(R.id.fragment_place, newFragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }

}

logcat:

09-30 15:03:19.257: W/dalvikvm(12895): threadid=1: thread exiting with uncaught exception (group=0x40ed5300)
09-30 15:03:19.265: E/AndroidRuntime(12895): FATAL EXCEPTION: main
09-30 15:03:19.265: E/AndroidRuntime(12895): java.lang.NoClassDefFoundError: com.google.android.gms.R$styleable
09-30 15:03:19.265: E/AndroidRuntime(12895):    at com.google.android.gms.maps.GoogleMapOptions.createFromAttributes(Unknown Source)
09-30 15:03:19.265: E/AndroidRuntime(12895):    at com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown Source)
09-30 15:03:19.265: E/AndroidRuntime(12895):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:284)
09-30 15:03:19.265: E/AndroidRuntime(12895):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
09-30 15:03:19.265: E/AndroidRuntime(12895):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
09-30 15:03:19.265: E/AndroidRuntime(12895):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
09-30 15:03:19.265: E/AndroidRuntime(12895):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
09-30 15:03:19.265: E/AndroidRuntime(12895):    at com.example.free.MainActivity.onCreateView(MainActivity.java:122)
09-30 15:03:19.265: E/AndroidRuntime(12895):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
09-30 15:03:19.265: E/AndroidRuntime(12895):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
09-30 15:03:19.265: E/AndroidRuntime(12895):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
09-30 15:03:19.265: E/AndroidRuntime(12895):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
09-30 15:03:19.265: E/AndroidRuntime(12895):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
09-30 15:03:19.265: E/AndroidRuntime(12895):    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:556)
09-30 15:03:19.265: E/AndroidRuntime(12895):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1163)
09-30 15:03:19.265: E/AndroidRuntime(12895):    at android.app.Activity.performStart(Activity.java:5018)
09-30 15:03:19.265: E/AndroidRuntime(12895):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
09-30 15:03:19.265: E/AndroidRuntime(12895):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2210)
09-30 15:03:19.265: E/AndroidRuntime(12895):    at android.app.ActivityThread.access$600(ActivityThread.java:142)
09-30 15:03:19.265: E/AndroidRuntime(12895):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1208)
09-30 15:03:19.265: E/AndroidRuntime(12895):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-30 15:03:19.265: E/AndroidRuntime(12895):    at android.os.Looper.loop(Looper.java:137)
09-30 15:03:19.265: E/AndroidRuntime(12895):    at android.app.ActivityThread.main(ActivityThread.java:4931)
09-30 15:03:19.265: E/AndroidRuntime(12895):    at java.lang.reflect.Method.invokeNative(Native Method)
09-30 15:03:19.265: E/AndroidRuntime(12895):    at java.lang.reflect.Method.invoke(Method.java:511)
09-30 15:03:19.265: E/AndroidRuntime(12895):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
09-30 15:03:19.265: E/AndroidRuntime(12895):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
09-30 15:03:19.265: E/AndroidRuntime(12895):    at dalvik.system.NativeStart.main(Native Method)

并且片段有点大,所以我把它粘贴在pastebin上:

http://pastebin.com/G9MekK1Z

感谢您的协助!

4 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

您需要做的是在项目中加入Google Play服务库。 Google有非常好的指示here

请访问code.google.com并获取您的ApiKey。

在那里创建项目 - >转到服务 - >激活Google Maps Android API v2。然后为您的应用程序创建密钥。

将以下权限添加到清单。

 <uses-permission android:name="com.anchit.locationapi.maps.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-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

使用

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

还在应用程序标记内添加

<permission
        android:name="com.anchit.locationapi.maps.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

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

请在您的代码中使用以下内容:

map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
        .getMap();

答案 2 :(得分:0)

您可以查看我撰写的关于如何在您的应用中集成Google Map API V2的博客文章,它可能只是为您解决问题:

Google Maps API V2

答案 3 :(得分:0)

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    return inflater.inflate(R.layout.activity_main, container, false);
}

使用它而不是oncreate()