谷歌地图Android片段崩溃

Posted

技术标签:

【中文标题】谷歌地图Android片段崩溃【英文标题】:Google map Android fragment crash 【发布时间】:2015-06-10 17:42:44 【问题描述】:

Hye Everybody,我是法国人,所以sory为我的英语不好。 对于我的家庭作业,我必须制作一个应用程序,在谷歌地图中显示我的智能手机位置,但是当我在 connect.xml 中添加片段时,应用程序崩溃:“不幸的是 [app] 已停止”

我的 Google Play 服务运行良好!

com.google.android.gms文本:configapi连接成功

我觉得我的 AndroidManifest 也不错

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.supinfo.tesas"
android:versionCode="1"
android:versionName="1.0" >


<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"/>
<uses-feature android:glEsVersion="0x00020000" android:required="true"/>

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".Main"
        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=".Connect" >
    </activity>

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

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

</manifest>

现在是我的 connect.xml

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_ >

<LinearLayout
    android:layout_
    android:layout_
    android:orientation="vertical" >

    <TextView
        android:id="@+id/txtView"
        android:layout_
        android:layout_
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <LinearLayout
        android:layout_
        android:layout_ >

        <Button
            android:id="@+id/btnUpdate"
            android:layout_
            android:layout_
            android:text="Update position" />

        <Button
            android:id="@+id/btnGetCar"
            android:layout_
            android:layout_
            android:text="Where is my car ?" />
    </LinearLayout>

    <TextView
        android:id="@+id/txtPosition"
        android:layout_
        android:layout_
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

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

 </LinearLayout>

 </merge>

我的 Connect.java

package com.supinfo.tesas;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.supinfo.tesas.model.Car;
import com.supinfo.tesas.model.User;

public class Connect extends Activity implements OnClickListener 
SharedPreferences user;
TextView txtView; 
TextView txtPosition;
Button btnGetCar;
Button btnUpdate;


String username;
String password;

User u = new User();
Car c;

@Override
public void onCreate(Bundle savedInstanceState) 

    super.onCreate(savedInstanceState);
    setContentView(R.layout.connect);
    btnGetCar = (Button) findViewById(R.id.btnGetCar);
    btnGetCar.setOnClickListener(this);

    btnUpdate = (Button) findViewById(R.id.btnUpdate);
    btnUpdate.setOnClickListener(this);

    user = this.getSharedPreferences("user",Context.MODE_PRIVATE);
    username = user.getString("username", "");
    password = user.getString("password", "");

    txtView = (TextView) findViewById(R.id.txtView);
    txtView.setText("Bonjour "+username);

    txtPosition = (TextView) findViewById(R.id.txtPosition);

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





@Override
public void onClick(View v) 
    switch(v.getId()) 
        case R.id.btnGetCar:
            c = u.getCarPosition(username,password);
            Log.i("latitude",""+c.getLatitude());
            break;
        case R.id.btnUpdate:

            //mettre les latitude longiture
            if(u.updatePosition(username, password, "", ""))
                Log.i("updatePosition","true");
            
            break;
           


@Override
protected void onDestroy() 
    // TODO Auto-generated method stub
    //map.removeView(this.mMapView);
    super.onDestroy();

我是一个main.java谁重定向到connect.java

这是我的日志

04-06 10:29:04.963: D/AndroidRuntime(2671): Shutting down VM
04-06 10:29:04.986: E/AndroidRuntime(2671): FATAL EXCEPTION: main
04-06 10:29:04.986: E/AndroidRuntime(2671): Process: com.supinfo.tesas, PID:  2671
04-06 10:29:04.986: E/AndroidRuntime(2671): java.lang.RuntimeException:   Unable to start activity   ComponentInfocom.supinfo.tesas/com.supinfo.tesas.Connect:     android.view.InflateException: Binary XML file line #42: Error inflating class     fragment
04-06 10:29:04.986: E/AndroidRuntime(2671):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
04-06 10:29:04.986: E/AndroidRuntime(2671):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
04-06 10:29:04.986: E/AndroidRuntime(2671):     at android.app.ActivityThread.access$800(ActivityThread.java:144)
04-06 10:29:04.986: E/AndroidRuntime(2671):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
04-06 10:29:04.986: E/AndroidRuntime(2671):     at android.os.Handler.dispatchMessage(Handler.java:102)
04-06 10:29:04.986: E/AndroidRuntime(2671):     at android.os.Looper.loop(Looper.java:135)
04-06 10:29:04.986: E/AndroidRuntime(2671):     at android.app.ActivityThread.main(ActivityThread.java:5221)
04-06 10:29:04.986: E/AndroidRuntime(2671):     at java.lang.reflect.Method.invoke(Native Method)
04-06 10:29:04.986: E/AndroidRuntime(2671):     at java.lang.reflect.Method.invoke(Method.java:372)
04-06 10:29:04.986: E/AndroidRuntime(2671):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
04-06 10:29:04.986: E/AndroidRuntime(2671):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
04-06 10:29:04.986: E/AndroidRuntime(2671): Caused by: android.view.InflateException: Binary XML file line #42: Error inflating class fragment
04-06 10:29:04.986: E/AndroidRuntime(2671):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
04-06 10:29:04.986: E/AndroidRuntime(2671):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
04-06 10:29:04.986: E/AndroidRuntime(2671):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
04-06 10:29:04.986: E/AndroidRuntime(2671):     at android.view.LayoutInflater.inflate(LayoutInflater.java:479)
04-06 10:29:04.986: E/AndroidRuntime(2671):     at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
04-06 10:29:04.986: E/AndroidRuntime(2671):     at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
04-06 10:29:04.986: E/AndroidRuntime(2671):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:377)
04-06 10:29:04.986: E/AndroidRuntime(2671):     at android.app.Activity.setContentView(Activity.java:2144)
04-06 10:29:04.986: E/AndroidRuntime(2671):     at com.supinfo.tesas.Connect.onCreate(Connect.java:36)
04-06 10:29:04.986: E/AndroidRuntime(2671):     at android.app.Activity.performCreate(Activity.java:5937)
04-06 10:29:04.986: E/AndroidRuntime(2671):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
04-06 10:29:04.986: E/AndroidRuntime(2671):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
04-06 10:29:04.986: E/AndroidRuntime(2671):     ... 10 more
04-06 10:29:04.986: E/AndroidRuntime(2671): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.SupportMapFragment: make sure class name exists, is public, and has an empty constructor that is public
04-06 10:29:04.986: E/AndroidRuntime(2671):     at android.app.Fragment.instantiate(Fragment.java:618)
04-06 10:29:04.986: E/AndroidRuntime(2671):     at android.app.Fragment.instantiate(Fragment.java:582)
04-06 10:29:04.986: E/AndroidRuntime(2671):     at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2108)
04-06 10:29:04.986: E/AndroidRuntime(2671):     at android.app.Activity.onCreateView(Activity.java:5286)
04-06 10:29:04.986: E/AndroidRuntime(2671):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
04-06 10:29:04.986: E/AndroidRuntime(2671):     ... 21 more
04-06 10:29:04.986: E/AndroidRuntime(2671): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.maps.SupportMapFragment" on path: DexPathList[[zip file "/data/app/com.supinfo.tesas-2/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
04-06 10:29:04.986: E/AndroidRuntime(2671):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
04-06 10:29:04.986: E/AndroidRuntime(2671):     at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
04-06 10:29:04.986: E/AndroidRuntime(2671):     at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
04-06 10:29:04.986: E/AndroidRuntime(2671):     at android.app.Fragment.instantiate(Fragment.java:604)
04-06 10:29:04.986: E/AndroidRuntime(2671):     ... 25 more
04-06 10:29:04.986: E/AndroidRuntime(2671):     Suppressed: java.lang.NoClassDefFoundError: com.google.android.gms.maps.SupportMapFragment
04-06 10:29:04.986: E/AndroidRuntime(2671):         at dalvik.system.DexFile.defineClassNative(Native Method)
04-06 10:29:04.986: E/AndroidRuntime(2671):         at dalvik.system.DexFile.defineClass(DexFile.java:226)
04-06 10:29:04.986: E/AndroidRuntime(2671):         at dalvik.system.DexFile.loadClassBinaryName(DexFile.java:219)
04-06 10:29:04.986: E/AndroidRuntime(2671):         at dalvik.system.DexPathList.findClass(DexPathList.java:321)
04-06 10:29:04.986: E/AndroidRuntime(2671):         at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:54)
04-06 10:29:04.986: E/AndroidRuntime(2671):         ... 28 more
04-06 10:29:04.986: E/AndroidRuntime(2671):     Suppressed: java.lang.ClassNotFoundException: com.google.android.gms.maps.SupportMapFragment
04-06 10:29:04.986: E/AndroidRuntime(2671):         at java.lang.Class.classForName(Native Method)
04-06 10:29:04.986: E/AndroidRuntime(2671):         at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
04-06 10:29:04.986: E/AndroidRuntime(2671):         at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
04-06 10:29:04.986: E/AndroidRuntime(2671):         at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
04-06 10:29:04.986: E/AndroidRuntime(2671):         ... 27 more
04-06 10:29:04.986: E/AndroidRuntime(2671):     Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

感谢您的帮助! :)

【问题讨论】:

你会在这里发布日志吗? span> 【参考方案1】:

这是工作!这是因为我忘记了这个

<permission
    android:name="com.supinfo.tesas.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />
<uses-permission android:name="com.supinfo.tesas.permission.MAPS_RECEIVE" />

谢谢大家!!

【讨论】:

【参考方案2】:

你在connect.xml 中输入了错误的android:name:

而不是:

android:name="com.google.android.gms.maps.MapFragment"

使用:

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

在java文件中:而不是

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

使用:

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

【讨论】:

如果我这样做,我应该将 Activity 更改为 FragmentActivity 吗? 那么,我如何重定向到 FragmentActivity ?有意图?【参考方案3】:

使用 FragmentActivity 扩展您的 Activity,如下行:

public class Connect extends FragmentActivity implements OnClickListener 

【讨论】:

如何重定向到 FragmentActivity ?有意图? 这与您调用任何其他活动的意图相同。【参考方案4】:

只需将 ACESS NETWORK STATE 权限添加到您的清单。

  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

点击以下链接了解更多信息。

http://www.androidhive.info/2013/08/android-working-with-google-maps-v2/

【讨论】:

【参考方案5】:

为任何搜索的人添加此功能,我的依赖项中包含了 Google Play 服务,但添加此功能解决了我经过数小时搜索后的通货膨胀问题。

实现 'com.google.android.gms:play-services-maps:+'

【讨论】:

以上是关于谷歌地图Android片段崩溃的主要内容,如果未能解决你的问题,请参考以下文章

谷歌地图在片段android

片段中的 Android 谷歌地图

带有标记的Android谷歌地图片段

在 Android 片段中使用谷歌地图

Android谷歌地图不断崩溃

Android 谷歌地图、移动标记崩溃应用