package info.androidhive.googlemapsv2;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.TileOverlayOptions;
import com.google.android.gms.maps.model.TileProvider;
import com.google.android.gms.maps.model.UrlTileProvider;
import android.os.Bundle;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Locale;
/**
* This demonstrates how to add a tile overlay to a map.
*/
public class MainActivity extends android.support.v4.app.FragmentActivity {
/** This returns moon tiles. */
private static final String MOON_MAP_URL_FORMAT =
"http://mw1.google.com/mw-planetary/lunar/lunarmaps_v1/clem_bw/%d/%d/%d.jpg";
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpMapIfNeeded();
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
mMap.setMapType(GoogleMap.MAP_TYPE_NONE);
TileProvider tileProvider = new UrlTileProvider(256, 256) {
@Override
public synchronized URL getTileUrl(int x, int y, int zoom) {
// The moon tile coordinate system is reversed. This is not normal.
int reversedY = (1 << zoom) - y - 1;
String s = String.format(Locale.US, MOON_MAP_URL_FORMAT, zoom, x, reversedY);
URL url = null;
try {
url = new URL(s);
} catch (MalformedURLException e) {
throw new AssertionError(e);
}
return url;
}
};
mMap.addTileOverlay(new TileOverlayOptions().tileProvider(tileProvider));
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="info.androidhive.googlemapsv2"
android:versionCode="1"
android:versionName="1.0" >
<permission
android:name="info.androidhive.googlemapsv2.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="info.androidhive.googlemapsv2.permission.MAPS_RECEIVE" />
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="19" />
<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" />
<!-- Requires OpenGL ES version 2 -->
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name="info.androidhive.googlemapsv2.MainActivity"
android:label="@string/app_name"
android:theme="@style/AppBaseTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Goolge API Key -->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyBVoQX4aDTd7tg9R_C-XOxgXqWlwR8sDTo" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
</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
06-18 10:20:32.797: D/AndroidRuntime(19115): Shutting down VM
06-18 10:20:32.797: W/dalvikvm(19115): threadid=1: thread exiting with uncaught exception (group=0x41409930)
06-18 10:20:32.827: E/AndroidRuntime(19115): FATAL EXCEPTION: main
06-18 10:20:32.827: E/AndroidRuntime(19115): java.lang.RuntimeException: Unable to start activity ComponentInfo{info.androidhive.googlemapsv2/info.androidhive.googlemapsv2.MainActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
06-18 10:20:32.827: E/AndroidRuntime(19115): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2255)
06-18 10:20:32.827: E/AndroidRuntime(19115): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2309)
06-18 10:20:32.827: E/AndroidRuntime(19115): at android.app.ActivityThread.access$700(ActivityThread.java:157)
06-18 10:20:32.827: E/AndroidRuntime(19115): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289)
06-18 10:20:32.827: E/AndroidRuntime(19115): at android.os.Handler.dispatchMessage(Handler.java:99)
06-18 10:20:32.827: E/AndroidRuntime(19115): at android.os.Looper.loop(Looper.java:176)
06-18 10:20:32.827: E/AndroidRuntime(19115): at android.app.ActivityThread.main(ActivityThread.java:5317)
06-18 10:20:32.827: E/AndroidRuntime(19115): at java.lang.reflect.Method.invokeNative(Native Method)
06-18 10:20:32.827: E/AndroidRuntime(19115): at java.lang.reflect.Method.invoke(Method.java:511)
06-18 10:20:32.827: E/AndroidRuntime(19115): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
06-18 10:20:32.827: E/AndroidRuntime(19115): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
06-18 10:20:32.827: E/AndroidRuntime(19115): at dalvik.system.NativeStart.main(Native Method)
06-18 10:20:32.827: E/AndroidRuntime(19115): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
06-18 10:20:32.827: E/AndroidRuntime(19115): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710)
06-18 10:20:32.827: E/AndroidRuntime(19115): at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
06-18 10:20:32.827: E/AndroidRuntime(19115): at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
06-18 10:20:32.827: E/AndroidRuntime(19115): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
06-18 10:20:32.827: E/AndroidRuntime(19115): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
06-18 10:20:32.827: E/AndroidRuntime(19115): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:360)
06-18 10:20:32.827: E/AndroidRuntime(19115): at android.app.Activity.setContentView(Activity.java:1932)
06-18 10:20:32.827: E/AndroidRuntime(19115): at info.androidhive.googlemapsv2.MainActivity.onCreate(MainActivity.java:29)
06-18 10:20:32.827: E/AndroidRuntime(19115): at android.app.Activity.performCreate(Activity.java:5326)
06-18 10:20:32.827: E/AndroidRuntime(19115): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
06-18 10:20:32.827: E/AndroidRuntime(19115): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2218)
06-18 10:20:32.827: E/AndroidRuntime(19115): ... 11 more
06-18 10:20:32.827: E/AndroidRuntime(19115): Caused by: java.lang.ClassCastException: com.google.android.gms.maps.MapFragment cannot be cast to android.support.v4.app.Fragment
06-18 10:20:32.827: E/AndroidRuntime(19115): at android.support.v4.app.Fragment.instantiate(Fragment.java:394)
06-18 10:20:32.827: E/AndroidRuntime(19115): at android.support.v4.app.Fragment.instantiate(Fragment.java:369)
06-18 10:20:32.827: E/AndroidRuntime(19115): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
06-18 10:20:32.827: E/AndroidRuntime(19115): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:682)
06-18 10:20:32.827: E/AndroidRuntime(19115): ... 21 more
06-18 10:20:34.419: I/Process(19115): Sending signal. PID: 19115 SIG: 9
您的minsdk=“12”
所以您应该更改此
public class MainActivity extends android.support.v4.app.FragmentActivity
至
public class MainActivity extends Activity
也要更改这个
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
我刚刚实现了我的第四个选项卡到我的应用程序中,已经实现了谷歌地图。在此实现之前,我的应用程序运行良好,但现在当我更改选项卡时,它会崩溃。我在这里看到过类似的问题,但没有一个有答案(实际上有这么多不同的原因和回应这个问题) 下面是fragmentshouts_maps类 查看寻呼机适配器类 这是我的错误日志
这里的主要问题是:上面的代码在几乎所有的设备(受刺激的设备,或者一些真实的设备)上都能顺利运行。但当我在三星S3上运行时。它注意到这个错误: 请告诉我如何修复错误,谢谢:)
这里是MainActivity类 我的LogCat文件是 求你了,救命
当我点击后退按钮并返回到位置片段时,我遇到了一些问题,下面出现了一个错误: 我确实查了很多帖子,尝试了所有的解决方案,但它不起作用。似乎其他菜单项不是问题,只是这个位置页面给出了一个错误。 fragment_location.xml LocationFragment.java
我很沮丧,我可能无法很好地解释,但基本上我试图编码一个音乐应用程序播放器,但每当我点击按钮,应用程序只是崩溃。Logcat真的帮不了什么忙,因为我不知道它出了什么问题。请帮帮我 主要活动。Java语言 } activity_main.xml: 登录。java:“ }" activity_login.xml:" 错误: