好吧,我现在正在尝试将近一周时间来构建一个地图应用程序,但似乎没有任何工作。我知道之前有人问过这个问题,但当时给出的答案对我来说似乎不起作用。 当我在我的模拟器上运行应用程序时,我收到以下错误: java.lang.classnotfoundexception在loader dalvik.system.PathClassLoader中的android.view.fragment
我的代码是:
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>
的java:
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>
任何建议都非常感谢。
答案 0 :(得分:0)
我这样使用它并且工作正常!!也许它对你有用。
FragmentManager myFragmentManager = getSupportFragmentManager();
SupportMapFragment mySupportMapFragment = (SupportMapFragment)myFragmentManager.findFragmentById(R.id.customer_map_fragment);
mMap = mySupportMapFragment.getMap();
答案 1 :(得分: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"/>
它有效!!
AntuanSoft。
问候