谷歌地图Android片段崩溃

时间:2015-04-06 10:21:17

标签: java android eclipse google-maps android-fragments

希望每个人,我是法国人,因为我的英语很糟糕。 对于我的作业,我必须制作一个应用程序,在谷歌地图中显示我的智能手机位置,但是当我在我的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_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/txtView"
        android:layout_width="318dp"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/btnUpdate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Update position" />

        <Button
            android:id="@+id/btnGetCar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Where is my car ?" />
    </LinearLayout>

    <TextView
        android:id="@+id/txtPosition"
        android:layout_width="318dp"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

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

 </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   ComponentInfo{com.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

感谢您的帮助! :)

5 个答案:

答案 0 :(得分:2)

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

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

谢谢大家!

答案 1 :(得分:0)

你在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();

答案 2 :(得分:0)

使用FragmentActivity扩展您的活动,如下所示:

public class Connect extends FragmentActivity implements OnClickListener {

答案 3 :(得分:0)

只需为您的清单添加ACCESS NETWORK STATE权限。

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

请点击以下链接获取更多信息。

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

答案 4 :(得分:0)

为所有人进行搜索时,我的依赖项中包含了Google Play服务,但添加它解决了我经过数小时的通货膨胀问题。

  

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