Java Android-改变类片段的错误
我有两个片段和一个有导航抽屉的主要活动。在导航抽屉中,我有两个菜单项,即主页和位置。我已经为这两个菜单项创建了xml片段 fragment_home.xmlJava Android-改变类片段的错误,java,android,xml,android-layout,android-fragments,Java,Android,Xml,Android Layout,Android Fragments,我有两个片段和一个有导航抽屉的主要活动。在导航抽屉中,我有两个菜单项,即主页和位置。我已经为这两个菜单项创建了xml片段 fragment_home.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:la
<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:padding="0dp"
tools:context="com.gaurav.googlemap.HomeMap" >
<fragment
class="com.google.android.gms.maps.SupportMapFragment"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent">
</fragment>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Places"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
java文件中是否有任何更正以正确打开片段?谢谢 LogCat
02-16 16:50:58.324: V/Provider/Setting(25249): from settings cache , name = sound_effects_enabled value = 0
02-16 16:50:58.358: D/AndroidRuntime(25249): Shutting down VM
02-16 16:50:58.359: W/dalvikvm(25249): threadid=1: thread exiting with uncaught exception (group=0x4188b908)
02-16 16:50:58.388: E/AndroidRuntime(25249): FATAL EXCEPTION: main
02-16 16:50:58.388: E/AndroidRuntime(25249): android.view.InflateException: Binary XML file line #8: Error inflating class fragment
02-16 16:50:58.388: E/AndroidRuntime(25249): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
02-16 16:50:58.388: E/AndroidRuntime(25249): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
02-16 16:50:58.388: E/AndroidRuntime(25249): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
当我们经历片段生命周期时,似乎当片段再次从onDestroyView()调用时,取消调用onCreateView()。所以我的建议是通过方法保存片段的实例 addToBackStack() 或使用 SetRetainState(真)
检查mainfaist文件,如果您拥有实现
googleMap
所需的所有权限,请查看。
如果所有权限都正常,请尝试更改代码,如下所示:
由于您使用的是SupportMapFragment
,并且selectItem
方法中的变量Fragment
有时包含HomeFragment
类(其中是映射),因此您应该导入以下内容:
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
这一行:
FragmentManager fragmentManager = getFragmentManager();
您应更改为:
FragmentManager fragmentManager = getSupportFragmentManager();
并导入以下内容:
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
因为和我上面提到的相同的原因
希望这会对您有所帮助。请发布您的完整日志。我认为这是因为您正在使用support.v4.app.FragmentActivity和android.app.Fragment一起使用,您应该使用support.v4.app.fragmentation在我的代码中哪里更改@SboneloMbhamali@93gaurav93:在
HomeFragment
中扩展SupportMapFragment
,而不是Fragment
删除此行:导入android.app.Fragment;并将其替换为import android.support.v4.app.Fragment;如何在我的代码中放置此方法?我不知道我已经修改了我的代码,但这不起作用…:(