0

好吧,我现在尝试了将近一个星期来构建一个地图应用程序,但似乎没有任何效果。我知道以前有人问过这个问题,但是当时给出的答案似乎对我不起作用。当我试图在我的模拟器上运行该应用程序时,我收到以下错误:java.lang.classnotfoundexception android.view.fragment in loader dalvik.system.PathClassLoader

我的代码是:

xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

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

</RelativeLayout>

爪哇:

package com.enola.harti3;

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

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;

public class MainActivity extends FragmentActivity{
    private GoogleMap myGoogleMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        FragmentManager fm = getSupportFragmentManager();
        Fragment fr = fm.findFragmentById(R.id.map);
        SupportMapFragment mf = (SupportMapFragment)fr; 

    }
}

显现:

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

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

     <permission 
        android:name="com.enola.harti3.permission.MAPS_RECEIVE"></permission>

     <uses-permission android:name="android.permission.MAPS_RECEIVE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<!-- The following two permissions are not required to use
     Google Maps Android API v2, but are recommended. -->
<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"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

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


        <activity
            android:name="com.enola.harti3.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

任何建议都受到高度赞赏。

4

2 回答 2

0

I use it like this and it works fine!! Maybe it will work for you aswell.

FragmentManager myFragmentManager = getSupportFragmentManager();
SupportMapFragment mySupportMapFragment = (SupportMapFragment)myFragmentManager.findFragmentById(R.id.customer_map_fragment);
mMap = mySupportMapFragment.getMap();
于 2013-09-25T09:08:21.830 回答
0

为了避免 java.lang.classnotfoundexception android.view.fragment 你必须做两件事

1 - 在您的 Java 代码中,您的活动必须从 FramentActivity 扩展

2 - 在您的清单文件中,您必须添加此元数据行以完成您的配置:

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

有用!!

安团软件。

问候

于 2014-06-15T09:45:07.137 回答