Android – 将地图v2实施到导航抽屉

我试图合并两个Android演示程序,但我无法正确。

我尝试了几种方法,但我无法理解。 你可以帮我吗?

这是我的错误消息:

09-20 03:28:25.617: E/AndroidRuntime(20108): FATAL EXCEPTION: main 09-20 03:28:25.617: E/AndroidRuntime(20108): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.navigation/com.example.android.navigation.MainActivity}: android.view.InflateException: Binary XML file line #18: Error inflating class fragment 09-20 03:28:25.617: E/AndroidRuntime(20108): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 09-20 03:28:25.617: E/AndroidRuntime(20108): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 09-20 03:28:25.617: E/AndroidRuntime(20108): at android.app.ActivityThread.access$600(ActivityThread.java:141) 09-20 03:28:25.617: E/AndroidRuntime(20108): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 09-20 03:28:25.617: E/AndroidRuntime(20108): at android.os.Handler.dispatchMessage(Handler.java:99) 09-20 03:28:25.617: E/AndroidRuntime(20108): at android.os.Looper.loop(Looper.java:137) 09-20 03:28:25.617: E/AndroidRuntime(20108): at android.app.ActivityThread.main(ActivityThread.java:5041) 09-20 03:28:25.617: E/AndroidRuntime(20108): at java.lang.reflect.Method.invokeNative(Native Method) 09-20 03:28:25.617: E/AndroidRuntime(20108): at java.lang.reflect.Method.invoke(Method.java:511) 09-20 03:28:25.617: E/AndroidRuntime(20108): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 09-20 03:28:25.617: E/AndroidRuntime(20108): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 09-20 03:28:25.617: E/AndroidRuntime(20108): at dalvik.system.NativeStart.main(Native Method) 09-20 03:28:25.617: E/AndroidRuntime(20108): Caused by: android.view.InflateException: Binary XML file line #18: Error inflating class fragment 09-20 03:28:25.617: E/AndroidRuntime(20108): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 09-20 03:28:25.617: E/AndroidRuntime(20108): at android.view.LayoutInflater.inflate(LayoutInflater.java:466) 09-20 03:28:25.617: E/AndroidRuntime(20108): at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 09-20 03:28:25.617: E/AndroidRuntime(20108): at com.example.android.navigation.BasicMapActivity.onCreateView(BasicMapActivity.java:57) 09-20 03:28:25.617: E/AndroidRuntime(20108): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1470) 09-20 03:28:25.617: E/AndroidRuntime(20108): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:925) 09-20 03:28:25.617: E/AndroidRuntime(20108): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1102) 09-20 03:28:25.617: E/AndroidRuntime(20108): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) 09-20 03:28:25.617: E/AndroidRuntime(20108): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1458) 09-20 03:28:25.617: E/AndroidRuntime(20108): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:551) 09-20 03:28:25.617: E/AndroidRuntime(20108): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1164) 09-20 03:28:25.617: E/AndroidRuntime(20108): at android.app.Activity.performStart(Activity.java:5114) 09-20 03:28:25.617: E/AndroidRuntime(20108): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153) 09-20 03:28:25.617: E/AndroidRuntime(20108): ... 11 more 09-20 03:28:25.617: E/AndroidRuntime(20108): Caused by: java.lang.RuntimeException: API key not found. Check that  is in the  element of AndroidManifest.xml 09-20 03:28:25.617: E/AndroidRuntime(20108): at maps.ag.bb.a(Unknown Source) 09-20 03:28:25.617: E/AndroidRuntime(20108): at maps.ag.bb.a(Unknown Source) 09-20 03:28:25.617: E/AndroidRuntime(20108): at maps.ag.an.a(Unknown Source) 09-20 03:28:25.617: E/AndroidRuntime(20108): at maps.ag.bi.a(Unknown Source) 09-20 03:28:25.617: E/AndroidRuntime(20108): at maps.ag.bh.a(Unknown Source) 09-20 03:28:25.617: E/AndroidRuntime(20108): at blf.onTransact(SourceFile:107) 09-20 03:28:25.617: E/AndroidRuntime(20108): at android.os.Binder.transact(Binder.java:310) 09-20 03:28:25.617: E/AndroidRuntime(20108): at com.google.android.gms.maps.internal.IMapFragmentDelegate$a$a.onCreateView(Unknown Source) 09-20 03:28:25.617: E/AndroidRuntime(20108): at com.google.android.gms.maps.SupportMapFragment$a.onCreateView(Unknown Source) 09-20 03:28:25.617: E/AndroidRuntime(20108): at com.google.android.gms.internal.bh$4.b(Unknown Source) 09-20 03:28:25.617: E/AndroidRuntime(20108): at com.google.android.gms.internal.bh.a(Unknown Source) 09-20 03:28:25.617: E/AndroidRuntime(20108): at com.google.android.gms.internal.bh.onCreateView(Unknown Source) 09-20 03:28:25.617: E/AndroidRuntime(20108): at com.google.android.gms.maps.SupportMapFragment.onCreateView(Unknown Source) 09-20 03:28:25.617: E/AndroidRuntime(20108): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1470) 09-20 03:28:25.617: E/AndroidRuntime(20108): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:898) 09-20 03:28:25.617: E/AndroidRuntime(20108): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080) 09-20 03:28:25.617: E/AndroidRuntime(20108): at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1182) 09-20 03:28:25.617: E/AndroidRuntime(20108): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:280) 09-20 03:28:25.617: E/AndroidRuntime(20108): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676) 09-20 03:28:25.617: E/AndroidRuntime(20108): ... 23 more 09-20 03:30:16.829: D/dalvikvm(20329): GC_CONCURRENT freed 189K, 3% free 8774K/9016K, paused 1ms+0ms, total 3ms 09-20 03:30:17.005: D/AndroidRuntime(20329): Shutting down VM 09-20 03:30:17.009: W/dalvikvm(20329): threadid=1: thread exiting with uncaught exception (group=0xa617f908) 09-20 03:30:17.025: E/AndroidRuntime(20329): FATAL EXCEPTION: main 09-20 03:30:17.025: E/AndroidRuntime(20329): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.navigation/com.example.android.navigation.MainActivity}: android.view.InflateException: Binary XML file line #18: Error inflating class fragment 09-20 03:30:17.025: E/AndroidRuntime(20329): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 09-20 03:30:17.025: E/AndroidRuntime(20329): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 09-20 03:30:17.025: E/AndroidRuntime(20329): at android.app.ActivityThread.access$600(ActivityThread.java:141) 09-20 03:30:17.025: E/AndroidRuntime(20329): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 09-20 03:30:17.025: E/AndroidRuntime(20329): at android.os.Handler.dispatchMessage(Handler.java:99) 09-20 03:30:17.025: E/AndroidRuntime(20329): at android.os.Looper.loop(Looper.java:137) 09-20 03:30:17.025: E/AndroidRuntime(20329): at android.app.ActivityThread.main(ActivityThread.java:5041) 09-20 03:30:17.025: E/AndroidRuntime(20329): at java.lang.reflect.Method.invokeNative(Native Method) 09-20 03:30:17.025: E/AndroidRuntime(20329): at java.lang.reflect.Method.invoke(Method.java:511) 09-20 03:30:17.025: E/AndroidRuntime(20329): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 09-20 03:30:17.025: E/AndroidRuntime(20329): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 09-20 03:30:17.025: E/AndroidRuntime(20329): at dalvik.system.NativeStart.main(Native Method) 09-20 03:30:17.025: E/AndroidRuntime(20329): Caused by: android.view.InflateException: Binary XML file line #18: Error inflating class fragment 09-20 03:30:17.025: E/AndroidRuntime(20329): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 09-20 03:30:17.025: E/AndroidRuntime(20329): at android.view.LayoutInflater.inflate(LayoutInflater.java:466) 09-20 03:30:17.025: E/AndroidRuntime(20329): at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 09-20 03:30:17.025: E/AndroidRuntime(20329): at com.example.android.navigation.BasicMapActivity.onCreateView(BasicMapActivity.java:57) 09-20 03:30:17.025: E/AndroidRuntime(20329): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1470) 09-20 03:30:17.025: E/AndroidRuntime(20329): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:925) 09-20 03:30:17.025: E/AndroidRuntime(20329): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1102) 09-20 03:30:17.025: E/AndroidRuntime(20329): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) 09-20 03:30:17.025: E/AndroidRuntime(20329): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1458) 09-20 03:30:17.025: E/AndroidRuntime(20329): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:551) 09-20 03:30:17.025: E/AndroidRuntime(20329): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1164) 09-20 03:30:17.025: E/AndroidRuntime(20329): at android.app.Activity.performStart(Activity.java:5114) 09-20 03:30:17.025: E/AndroidRuntime(20329): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153) 09-20 03:30:17.025: E/AndroidRuntime(20329): ... 11 more 09-20 03:30:17.025: E/AndroidRuntime(20329): Caused by: java.lang.RuntimeException: API key not found. Check that  is in the  element of AndroidManifest.xml 09-20 03:30:17.025: E/AndroidRuntime(20329): at maps.ag.bb.a(Unknown Source) 09-20 03:30:17.025: E/AndroidRuntime(20329): at maps.ag.bb.a(Unknown Source) 09-20 03:30:17.025: E/AndroidRuntime(20329): at maps.ag.an.a(Unknown Source) 09-20 03:30:17.025: E/AndroidRuntime(20329): at maps.ag.bi.a(Unknown Source) 09-20 03:30:17.025: E/AndroidRuntime(20329): at maps.ag.bh.a(Unknown Source) 09-20 03:30:17.025: E/AndroidRuntime(20329): at blf.onTransact(SourceFile:107) 09-20 03:30:17.025: E/AndroidRuntime(20329): at android.os.Binder.transact(Binder.java:310) 09-20 03:30:17.025: E/AndroidRuntime(20329): at com.google.android.gms.maps.internal.IMapFragmentDelegate$a$a.onCreateView(Unknown Source) 09-20 03:30:17.025: E/AndroidRuntime(20329): at com.google.android.gms.maps.SupportMapFragment$a.onCreateView(Unknown Source) 09-20 03:30:17.025: E/AndroidRuntime(20329): at com.google.android.gms.internal.bh$4.b(Unknown Source) 09-20 03:30:17.025: E/AndroidRuntime(20329): at com.google.android.gms.internal.bh.a(Unknown Source) 09-20 03:30:17.025: E/AndroidRuntime(20329): at com.google.android.gms.internal.bh.onCreateView(Unknown Source) 09-20 03:30:17.025: E/AndroidRuntime(20329): at com.google.android.gms.maps.SupportMapFragment.onCreateView(Unknown Source) 09-20 03:30:17.025: E/AndroidRuntime(20329): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1470) 09-20 03:30:17.025: E/AndroidRuntime(20329): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:898) 09-20 03:30:17.025: E/AndroidRuntime(20329): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080) 09-20 03:30:17.025: E/AndroidRuntime(20329): at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1182) 09-20 03:30:17.025: E/AndroidRuntime(20329): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:280) 09-20 03:30:17.025: E/AndroidRuntime(20329): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676) 09-20 03:30:17.025: E/AndroidRuntime(20329): ... 23 more 

这是我的源代码,我只更改了这个函数:

 private void selectItem(int position) { // update the main content by replacing fragments Fragment mapfragment = new BasicMapActivity(); Fragment fragment = new PlanetFragment(); Bundle args = new Bundle(); args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position); fragment.setArguments(args); mapfragment.getFragmentManager(); FragmentManager fragmentManager = getSupportFragmentManager(); fragmentManager.beginTransaction().replace(R.id.content_frame, mapfragment).commit(); // update selected item and title, then close the drawer mDrawerList.setItemChecked(position, true); setTitle(mPlanetTitles[position]); mDrawerLayout.closeDrawer(mDrawerList); } 

这是我的地图片段:

 public class BasicMapActivity extends Fragment { /** * Note that this may be null if the Google Play services APK is not available. */ private GoogleMap mMap; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setUpMapIfNeeded(); //setContentView(R.layout.basic_demo); } public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //setUpMapIfNeeded(); View rootView = inflater.inflate(R.layout.basic_demo, container, false); setUpMapIfNeeded(); return rootView; } @Override public 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) getFragmentManager().findFragmentById(R.id.map)).getMap(); // Check if we were successful in obtaining the map. if (mMap != null) { setUpMap(); } } } private void setUpMap() { mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker")); } } 

BasicMapActivity的xml文件:

  

编辑:

这是主程序的XML:

       

只要我的清单:

                          

谁能给我一个提示? 我被困在这里2-3天。

非常感谢!

你的发行很简单

API KEY必须位于Application标记内,您也应将其放在最后

示例:

          
 Caused by: java.lang.RuntimeException: API key not found. Check that  is in the  element of AndroidManifest.xml 

获取有效的API密钥。 除此之外,您无法在应用程序中运行地图V2。 这是doc如何获得一个。

第18行的BasicMapActivity的xml布局中发生了另一个错误。 请在您的问题中发布该XML。

在错误消息中,您有两个问题:

1.-“android.view.InflateException:二进制XML文件行#18:错误膨胀类片段”

2.-“java.lang.RuntimeException:找不到API密钥。检查它是否在AndroidManifest.xml的元素中”

“yume117”提供的解决方案解决了数字2,但我认为“膨胀类片段”的问题是由BasicMapActivity的xml文件引起的。

而不是“布局”中的“片段”,您需要更改为以下内容:

  

您还必须更改java类。 在这个url是一个控制它的例子(西class牙语,但我认为代码是可理解的): https : //groups.google.com/forum/#! msg / desarrolladores-android/1cvqPm0EZZU / 9srw_9feamUJ