2

当我使用片段时,我的应用程序崩溃了,它显然与GoogleMap其中一个片段有关。

我已经将地图片段从更改为MapFragmentSupportMapFragment但没有帮助。

这是所有片段所在的主页(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();
    }

}

日志猫:

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

4 回答 4

0

您可以查看我写的关于如何在您的应用程序中集成 Google Map API V2 的这篇博文,它可能会为您提供解决问题的方法:

谷歌地图 API V2

于 2013-09-30T13:11:10.860 回答
0

您需要做的是在您的项目中包含一个 Google Play 服务库。谷歌在这里有很好的说明:

请访问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();
于 2013-09-30T12:42:48.573 回答
0

我相信你必须使用 SupportMapFragment:http: //developer.android.com/reference/com/google/android/gms/maps/SupportMapFragment.html

这是一个很好的教程: http ://www.truiton.com/2013/05/android-supportmapfragment-example/

于 2013-09-30T12:20:20.010 回答
0
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

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

使用它而不是 oncreate()

于 2015-10-05T20:42:32.050 回答