尝试加载mapfragment时出错

时间:2013-09-27 17:57:36

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

我刚开始在Android上使用片段而且我在尝试启动时非常糟糕 片段活动中的地图片段(全屏) ​​- 我尝试过e-v-e-r-y-t-h-i-n-g -  1. Myproject> Android工具>添加支持库  2.将支持和Google Play服务添加为jar \ libs  3.重启ecipse 似乎没什么帮助...... 附件是我的logcat输出 -

09-27 20:49:09.421: E/AndroidRuntime(11714): FATAL EXCEPTION: main
09-27 20:49:09.421: E/AndroidRuntime(11714): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.telofun/com.telofun.FragmentMap}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
09-27 20:49:09.421: E/AndroidRuntime(11714):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1872)
09-27 20:49:09.421: E/AndroidRuntime(11714):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893)
09-27 20:49:09.421: E/AndroidRuntime(11714):    at android.app.ActivityThread.access$1500(ActivityThread.java:135)
09-27 20:49:09.421: E/AndroidRuntime(11714):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1054)
09-27 20:49:09.421: E/AndroidRuntime(11714):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-27 20:49:09.421: E/AndroidRuntime(11714):    at android.os.Looper.loop(Looper.java:150)
09-27 20:49:09.421: E/AndroidRuntime(11714):    at android.app.ActivityThread.main(ActivityThread.java:4385)
09-27 20:49:09.421: E/AndroidRuntime(11714):    at java.lang.reflect.Method.invokeNative(Native Method)
09-27 20:49:09.421: E/AndroidRuntime(11714):    at java.lang.reflect.Method.invoke(Method.java:507)
09-27 20:49:09.421: E/AndroidRuntime(11714):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
09-27 20:49:09.421: E/AndroidRuntime(11714):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
09-27 20:49:09.421: E/AndroidRuntime(11714):    at dalvik.system.NativeStart.main(Native Method)
09-27 20:49:09.421: E/AndroidRuntime(11714): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
09-27 20:49:09.421: E/AndroidRuntime(11714):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:587)
09-27 20:49:09.421: E/AndroidRuntime(11714):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
09-27 20:49:09.421: E/AndroidRuntime(11714):    at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
09-27 20:49:09.421: E/AndroidRuntime(11714):    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
09-27 20:49:09.421: E/AndroidRuntime(11714):    at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
09-27 20:49:09.421: E/AndroidRuntime(11714):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:250)
09-27 20:49:09.421: E/AndroidRuntime(11714):    at android.app.Activity.setContentView(Activity.java:1742)
09-27 20:49:09.421: E/AndroidRuntime(11714):    at com.telofun.FragmentMap.onCreate(FragmentMap.java:23)
09-27 20:49:09.421: E/AndroidRuntime(11714):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
09-27 20:49:09.421: E/AndroidRuntime(11714):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1836)
09-27 20:49:09.421: E/AndroidRuntime(11714):    ... 11 more
09-27 20:49:09.421: E/AndroidRuntime(11714): Caused by: android.support.v4.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
09-27 20:49:09.421: E/AndroidRuntime(11714):    at android.support.v4.app.Fragment.instantiate(Fragment.java:409)
09-27 20:49:09.421: E/AndroidRuntime(11714):    at android.support.v4.app.Fragment.instantiate(Fragment.java:377)
09-27 20:49:09.421: E/AndroidRuntime(11714):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:277)
09-27 20:49:09.421: E/AndroidRuntime(11714):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
09-27 20:49:09.421: E/AndroidRuntime(11714):    ... 20 more
09-27 20:49:09.421: E/AndroidRuntime(11714): Caused by: java.lang.ClassNotFoundException: com.google.android.gms.maps.MapFragment in loader dalvik.system.PathClassLoader[/data/app/com.telofun-1.apk]
09-27 20:49:09.421: E/AndroidRuntime(11714):    at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
09-27 20:49:09.421: E/AndroidRuntime(11714):    at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
09-27 20:49:09.421: E/AndroidRuntime(11714):    at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
09-27 20:49:09.421: E/AndroidRuntime(11714):    at android.support.v4.app.Fragment.instantiate(Fragment.java:399)

这就是布局 -

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

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

</RelativeLayout> 

代码 -

package com.telofun;

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 FragmentMap extends FragmentActivity {
    GoogleMap googleMap;

    Marker marker = null;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_map);

        // 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();

        }

    }
}

2 个答案:

答案 0 :(得分:2)

更改此

   class="com.google.android.gms.maps.MapFragment" 

   class="com.google.android.gms.maps.SupportMapFragment" 

确保您已正确引用Google Play服务库项目。

答案 1 :(得分:1)

正如Raghunandan所说,更改MapFragment for SupportMapFragment。但我认为这不是那么简单。我有同样的错误,尝试了6个教程,并阅读谷歌的所有文档。最后,我让它运行并显示地图。这就是我的工作方式。

1.从你的eclipse和你的PC上删除库google-play-services和android support-v4。 2.从de sdk-manager再次下载。 3.按照说明[此处](https://developers.google.com/maps/documentation/android/intro#sample_code)转到示例代码并按照说明进行操作。特别检查这个现有的Android Code Into Workspace,不要将库作为项目导入,它们将无法正常工作。 4.更改所有MapFragment for SupportMapFragment,不要忘记将你的Actvity扩展到FragmentActivity(这是我的失败)。 5.Disable(如果已启用)自动构建项目中的选项。 6.清理项目。 5.启用构建自动选项。 它应该工作。

如果它有效但地图没有显示您应该转到API consolo并打开Goog​​le Android Maps v2并关闭Google Maps v2。

希望你能解决它!