【问题标题】:my google map v2 test apps crashes我的谷歌地图 v2 测试应用程序崩溃
【发布时间】:2013-11-26 19:15:30
【问题描述】:

我是 stackoverflow 的新手 我有使用的谷歌地图测试项目 here 我喜欢教程,但应用程序崩溃了。

我完成了教程中的每一个步骤,并获得了 android 密钥、添加的库等等: 我的类似问题是这个链接: here 但它没有任何有效的答案。 我完全删除了 eclips,卸载了 java JDK 和 JRE,甚至我的整个系统 java。但应用程序崩溃了。 任何机构都可以帮助我吗?非常感谢

这是我的代码:

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.SupportMapFragment;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.NavUtils;



public class MainActivity extends FragmentActivity {

     private GoogleMap googleMap;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {
            // Loading map
            initilizeMap();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    private void initilizeMap() {
        if (googleMap == null) {

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

            googleMap =   ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

            // check if map is created successfully or not
            if (googleMap == null) {
                Toast.makeText(getApplicationContext(),
                        "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }
}

清单文件是:

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


     <permission
        android:name="com.test.maptest.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.test.maptest.permission.MAPS_RECEIVE" />

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

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

    <!-- Required to show current location -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <!-- Required OpenGL ES 2.0. for Maps V2 -->
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

            <!-- Goolge API Key -->
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyBYju6h2BWvZOaSDQpe5f9tv6fJsZy6cY8" />

</manifest>

布局是:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

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

</RelativeLayout>

这是 logcat:

11-25 14:04:50.663: E/Trace(1411): error opening trace file: No such file or directory (2)
11-25 14:04:50.853: D/AndroidRuntime(1411): Shutting down VM
11-25 14:04:50.853: W/dalvikvm(1411): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
11-25 14:04:50.873: E/AndroidRuntime(1411): FATAL EXCEPTION: main
11-25 14:04:50.873: E/AndroidRuntime(1411): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ariagostar.maptest/com.ariagostar.maptest.MainActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
11-25 14:04:50.873: E/AndroidRuntime(1411):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
11-25 14:04:50.873: E/AndroidRuntime(1411):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
11-25 14:04:50.873: E/AndroidRuntime(1411):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
11-25 14:04:50.873: E/AndroidRuntime(1411):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
11-25 14:04:50.873: E/AndroidRuntime(1411):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-25 14:04:50.873: E/AndroidRuntime(1411):     at android.os.Looper.loop(Looper.java:137)
11-25 14:04:50.873: E/AndroidRuntime(1411):     at android.app.ActivityThread.main(ActivityThread.java:4745)
11-25 14:04:50.873: E/AndroidRuntime(1411):     at java.lang.reflect.Method.invokeNative(Native Method)
11-25 14:04:50.873: E/AndroidRuntime(1411):     at java.lang.reflect.Method.invoke(Method.java:511)
11-25 14:04:50.873: E/AndroidRuntime(1411):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-25 14:04:50.873: E/AndroidRuntime(1411):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-25 14:04:50.873: E/AndroidRuntime(1411):     at dalvik.system.NativeStart.main(Native Method)
11-25 14:04:50.873: E/AndroidRuntime(1411): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
11-25 14:04:50.873: E/AndroidRuntime(1411):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
11-25 14:04:50.873: E/AndroidRuntime(1411):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
11-25 14:04:50.873: E/AndroidRuntime(1411):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
11-25 14:04:50.873: E/AndroidRuntime(1411):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
11-25 14:04:50.873: E/AndroidRuntime(1411):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
11-25 14:04:50.873: E/AndroidRuntime(1411):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
11-25 14:04:50.873: E/AndroidRuntime(1411):     at android.app.Activity.setContentView(Activity.java:1867)
11-25 14:04:50.873: E/AndroidRuntime(1411):     at com.ariagostar.maptest.MainActivity.onCreate(MainActivity.java:25)
11-25 14:04:50.873: E/AndroidRuntime(1411):     at android.app.Activity.performCreate(Activity.java:5008)
11-25 14:04:50.873: E/AndroidRuntime(1411):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
11-25 14:04:50.873: E/AndroidRuntime(1411):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
11-25 14:04:50.873: E/AndroidRuntime(1411):     ... 11 more
11-25 14:04:50.873: E/AndroidRuntime(1411): Caused by: java.lang.ClassCastException: com.google.android.gms.maps.MapFragment cannot be cast to android.support.v4.app.Fragment
11-25 14:04:50.873: E/AndroidRuntime(1411):     at android.support.v4.app.Fragment.instantiate(Fragment.java:388)
11-25 14:04:50.873: E/AndroidRuntime(1411):     at android.support.v4.app.Fragment.instantiate(Fragment.java:363)
11-25 14:04:50.873: E/AndroidRuntime(1411):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:264)
11-25 14:04:50.873: E/AndroidRuntime(1411):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)

【问题讨论】:

    标签: android google-maps


    【解决方案1】:

    改变

    public class MainActivity extends FragmentActivity {
    

    public class MainActivity extends Activity {
    

    改变这个

    googleMap =   ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
    

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

    您还需要清单中的应用程序标记中的以下内容

    <meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />
    

    这是 google play services rev 13 的要求

    编辑:

    移动

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

    清单中的应用程序标签内

    【讨论】:

    • 谢谢。我完成了,但仍然崩溃。应用程序出现了一段时间,但它崩溃了:(
    • @mahdit 您需要发布堆栈跟踪以获得更多帮助
    • 我如何向您发送堆栈跟踪?
    • @mahdit 提取 logcat 中显示异常的行并将其发布在此处。
    • 我把 Log.e("LOG", "I got an error", e);捕获 initilizeMap() 尝试部分。但是用 LOG 过滤器记录了任何内容。
    【解决方案2】:

    试试这个。

    你的 MainActivity

    private void initilizeMap() {
        if (googleMap == null) {
            googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
            // Valida si el mapa no se creo correctamente
            if (googleMap == null) {
                Toast.makeText(getApplicationContext(), "Sorry! el mapa no pudo ser cargado", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }
    

    在你的布局中

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment" />
    

    在清单中,我在 MAC 中使用了这一行,因为在 Windows 中没有它们也能正常工作

    <meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />
    

    【讨论】:

    • 我的问题已经解决了,我认为这是因为不要让元数据进入 标签。感谢您的关注;)
    【解决方案3】:

    我也有类似的问题。对我来说:

    import com.google.android.gms.maps.SupportMapFragment;
    

    并将 xml 更改为

        <fragment
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/header"  />
    

    解决了这个问题。希望你也解决!

    【讨论】:

      猜你喜欢
      • 2012-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多