我正在创建一个应用程序,我必须在单击按钮时显示地图。但是当点击地图按钮而不是显示地图时,应用程序崩溃并且它说“不幸的是,'应用程序'已停止"这是我的代码:
java文件:
package com.example.jamaattiming;
import android.os.Bundle;
import android.app.Activity;
//import android.app.FragmentTransaction;
import android.view.Menu;
import com.example.jamaat_times.R;
import com.google.android.gms.maps.GoogleMap;
//import com.google.android.gms.maps.MapFragment;
public class TagYourself extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tag_yourself);
/* MapFragment mMapFragment = MapFragment.newInstance();
FragmentTransaction fragmentTransaction =
getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.map, mMapFragment);
fragmentTransaction.commit();
GoogleMap maps;
maps=(((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap());*/
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.tag_yourself, menu);
return true;
}
}
这里是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:orientation="vertical"
android:background="#808080">
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jamaat_times"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<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="info.androidhive.googlemapsv2.permission.MAPS_RECEIVE" />
<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_jamaat"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.jamaattiming.Splash"
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="com.example.jamaattiming.MainPage"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.example.CLEARSCREEN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.jamaattiming.Qibla"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="com.example.COMPASS" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.jamaattiming.JamaatFinder"
android:label="@string/title_activity_jamaat_finder" >
</activity>
<activity
android:name="com.example.jamaattiming.QiblaFinder"
android:label="@string/title_activity_qibla_finder" >
</activity>
<activity
android:name="com.example.jamaattiming.TagYourself"
android:label="@string/title_activity_tag_yourself" >
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="my key" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name="com.example.jamaattiming.NearbySearch"
android:label="@string/title_activity_nearby_search" >
</activity>
<activity
android:name="com.example.jamaattiming.ManualSearch"
android:label="@string/title_activity_manual_search" >
</activity>
</application>
</manifest>
这是log cat:
04-01 00:32:36.587:I / SurfaceTextureClient(18546):[STC :: queueBuffer] (这个:0x518786b0)fps:0.88,dur:2279.85,最大值:1974.08,最小值:305.77 04-01 00:32:36.990:V / Provider / Settings(18546):invalidate [system]:current 214!=缓存0 04-01 00:32:37.020:D / ActivityThread(18546): installProvider:04-01 00:32:37.034:V / Provider / Settings(18546):from db cache,name = sound_effects_enabled,value = 0 04-01 00:32:37.223: D / mylog(18546):Wifi State true 04-01 00:32:38.102: D / ActivityThread(18546):getPackageInfo:securityViolation = true 04-01 00:32:38.102:W / asset(18546):AssetManager - &gt; addDefaultAssets CIP路径 不是没有! 04-01 00:32:38.741:I / Google Maps Android API(18546): Google Play服务客户端版本:4242000 04-01 00:32:39.025: I / Google Maps Android API(18546):Google Play服务包 版本:4324034 04-01 00:32:39.538:D / AndroidRuntime(18546):正在关闭 VM 04-01 00:32:39.538:W / dalvikvm(18546):threadid = 1:thread 退出未捕获的异常(组= 0x419b19a8)04-01 00:32:39.709: E / AndroidRuntime(18546):致命异常:主04-01 00:32:39.709: E / AndroidRuntime(18546):java.lang.RuntimeException:无法启动 活动 ComponentInfo {com.example.jamaat_times / com.example.jamaattiming.TagYourself}: android.view.InflateException:二进制XML文件行#8:错误 inflating class fragment 04-01 00:32:39.709:E / AndroidRuntime(18546): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343) 04-01 00:32:39.709:E / AndroidRuntime(18546):at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395) 04-01 00:32:39.709:E / AndroidRuntime(18546):at android.app.ActivityThread.access $ 600(ActivityThread.java:162)04-01 00:32:39.709:E / AndroidRuntime(18546):at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1364) 04-01 00:32:39.709:E / AndroidRuntime(18546):at android.os.Handler.dispatchMessage(Handler.java:107)04-01 00:32:39.709:E / AndroidRuntime(18546):at android.os.Looper.loop(Looper.java:194)04-01 00:32:39.709: E / AndroidRuntime(18546):at android.app.ActivityThread.main(ActivityThread.java:5371)04-01 00:32:39.709:E / AndroidRuntime(18546):at java.lang.reflect.Method.invokeNative(Native Method)04-01 00:32:39.709:E / AndroidRuntime(18546):at java.lang.reflect.Method.invoke(Method.java:525)04-01 00:32:39.709: E / AndroidRuntime(18546):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:833) 04-01 00:32:39.709:E / AndroidRuntime(18546):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)04-01 00:32:39.709:E / AndroidRuntime(18546):at dalvik.system.NativeStart.main(Native Method)04-01 00:32:39.709: E / AndroidRuntime(18546):引起:android.view.InflateException: 二进制XML文件行#8:错误膨胀类片段04-01 00:32:39.709:E / AndroidRuntime(18546):at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 04-01 00:32:39.709:E / AndroidRuntime(18546):at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)04-01 00:32:39.709:E / AndroidRuntime(18546):at android.view.LayoutInflater.inflate(LayoutInflater.java:489)04-01 00:32:39.709:E / AndroidRuntime(18546):at android.view.LayoutInflater.inflate(LayoutInflater.java:396)04-01 00:32:39.709:E / AndroidRuntime(18546):at android.view.LayoutInflater.inflate(LayoutInflater.java:352)04-01 00:32:39.709:E / AndroidRuntime(18546):at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:281) 04-01 00:32:39.709:E / AndroidRuntime(18546):at android.app.Activity.setContentView(Activity.java:1881)04-01 00:32:39.709:E / AndroidRuntime(18546):at com.example.jamaattiming.TagYourself.onCreate(TagYourself.java:16) 04-01 00:32:39.709:E / AndroidRuntime(18546):at android.app.Activity.performCreate(Activity.java:5122)04-01 00:32:39.709:E / AndroidRuntime(18546):at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081) 04-01 00:32:39.709:E / AndroidRuntime(18546):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307) 04-01 00:32:39.709:E / AndroidRuntime(18546):... 11更多04-01 00:32:39.709:E / AndroidRuntime(18546):引起: java.lang.SecurityException:Maps API需要额外的 以下权限在AndroidManifest.xml中设置以确保 正确行为:04-01 00:32:39.709:E / AndroidRuntime(18546): 04-01 00:32:39.709:E / AndroidRuntime(18546):at maps.e.ci.a(Unknown Source) 04-01 00:32:39.709:E / AndroidRuntime(18546):at maps.e.ay.a(Unknown 来源)04-01 00:32:39.709:E / AndroidRuntime(18546):at maps.e.ay.a(Unknown Source)04-01 00:32:39.709: E / AndroidRuntime(18546):at maps.e.al.a(Unknown Source)04-01 00:32:39.709:E / AndroidRuntime(18546):at maps.e.bh.a(Unknown Source) 04-01 00:32:39.709:E / AndroidRuntime(18546):at maps.e.bg.a(Unknown 来源)04-01 00:32:39.709:E / AndroidRuntime(18546):at etu.onTransact(SourceFile:107)04-01 00:32:39.709: E / AndroidRuntime(18546):at android.os.Binder.transact(Binder.java:310)04-01 00:32:39.709: E / AndroidRuntime(18546):at com.google.android.gms.maps.internal.IMapFragmentDelegate $ A $ a.onCreateView(未知 来源)04-01 00:32:39.709:E / AndroidRuntime(18546):at com.google.android.gms.maps.MapFragment $ a.onCreateView(未知来源) 04-01 00:32:39.709:E / AndroidRuntime(18546):at com.google.android.gms.dynamic.a $ 4.b(未知来源)04-01 00:32:39.709:E / AndroidRuntime(18546):at com.google.android.gms.dynamic.a.a(Unknown Source)04-01 00:32:39.709: E / AndroidRuntime(18546):at com.google.android.gms.dynamic.a.onCreateView(未知来源)04-01 00:32:39.709:E / AndroidRuntime(18546):at com.google.android.gms.maps.MapFragment.onCreateView(未知来源) 04-01 00:32:39.709:E / AndroidRuntime(18546):at android.app.Fragment.performCreateView(Fragment.java:1695)04-01 00:32:39.709:E / AndroidRuntime(18546):at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:861) 04-01 00:32:39.709:E / AndroidRuntime(18546):at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035) 04-01 00:32:39.709:E / AndroidRuntime(18546):at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1137) 04-01 00:32:39.709:E / AndroidRuntime(18546):at android.app.Activity.onCreateView(Activity.java:4735)04-01 00:32:39.709:E / AndroidRuntime(18546):at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680) 04-01 00:32:39.709:E / AndroidRuntime(18546):... 21更多
enter code here
答案 0 :(得分:2)
看一下logcat的这一行:
Caused by:
java.lang.SecurityException: The Maps API requires the additional following permissions to be set in the AndroidManifest.xml to ensure a correct behavior: 04-01 00:32:39.709:
E/AndroidRuntime(18546): <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 04-01 00:32:39.709: E/AndroidRuntime(18546): at maps.e.ci.a(Unknown Source)
您似乎忘了添加权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
到 AndroidManifest xml文件