我想在我的活动中添加片段,但我不知道我怎么能这样做我在堆栈溢出中检查多个答案但是我没有得到它请帮助我。
这是我的Activity布局(activity_home.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.foodshahi.www.foodshahi.Home">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="250dp"
android:background="#000000">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your Location: "
android:layout_marginTop="5dp"
android:textSize="16sp"
android:textColor="#FFFFFF"
android:id="@+id/textView"
android:layout_marginLeft="15dp"/>
<TextView
android:id="@+id/user_location_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:textSize="16sp"
android:layout_marginTop="5dp"
android:layout_toEndOf="@+id/textView"
android:layout_toRightOf="@+id/textView"
android:text="Test"
android:textColor="#FFFFFF" />
<TextView
android:id="@+id/change_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:onClick="changeLocation"
android:layout_alignLeft="@+id/textView"
android:layout_alignStart="@+id/textView"
android:layout_below="@+id/textView"
android:text="@string/change_location"
android:textColor="#FFFFFF"
android:textSize="16sp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginTop="50dp"
android:background="@color/colorPrimary">
<ImageView
android:layout_width="150dp"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/logo"/>
</RelativeLayout>
<LinearLayout android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"
android:orientation="horizontal"
android:id="@+id/linearLayout2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:drawableRight="@drawable/search"
android:hint="Search by Food and Chef"
android:textColor="#FFFFFF"
android:textColorHint="#FFFFFF" />
</RelativeLayout>
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/home_fragment">
</fragment>
</LinearLayout>
这是我的Activity类(Home.java):
public class Home extends AppCompatActivity {
TextView menuTextView;
ExpandableRelativeLayout expandableLayout1;
ImageView lunchImageView;
String cityName;
TextView cityNameTextView;
LunchResultFragment lunchResultFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
MenuFragment menuFragment = new MenuFragment();
getFragmentManager().beginTransaction()
.add(R.id.home_fragment,menuFragment)
.commit();
}
}
这是我的片段布局(menu_fragment.xml):
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:id="@+id/menu_text_view"
android:text="@string/home_menu"
android:textSize="18sp"
android:layout_centerHorizontal="true"
/>
<com.github.aakira.expandablelayout.ExpandableRelativeLayout
android:id="@+id/expandableMenuLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/menu_text_view"
android:background="#FFFF"
android:padding="16dp"
app:ael_duration="400"
app:ael_expanded="false"
app:ael_interpolator="accelerate"
app:ael_orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/button"
android:text="Home"
android:textColor="#FFFFFF"
android:textSize="18sp"
android:id="@+id/menu_home_button"
android:layout_centerHorizontal="true"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/button"
android:layout_below="@id/menu_home_button"
android:text="Order Food"
android:layout_marginTop="2dp"
android:id="@+id/menu_order_food"
android:textSize="18sp"
android:textColor="#FFFFFF"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/button"
android:layout_below="@id/menu_order_food"
android:layout_marginTop="2dp"
android:text="Special Deal"
android:textColor="#FFFFFF"
android:textSize="18sp"
android:id="@+id/menu_special_deal"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/button"
android:layout_below="@id/menu_special_deal"
android:layout_marginTop="2dp"
android:text="Chef Special"
android:textColor="#FFFFFF"
android:textSize="18sp"
android:id="@+id/menu_chef_special"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/button"
android:layout_below="@id/menu_chef_special"
android:text="Restaurant"
android:layout_marginTop="2dp"
android:id="@+id/menu_restaurant"
android:textSize="18sp"
android:textColor="#FFFFFF"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/button"
android:layout_below="@id/menu_restaurant"
android:layout_marginTop="2dp"
android:text="Home Made"
android:textSize="18sp"
android:textColor="#FFFFFF"
android:id="@+id/menu_home_made"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/button"
android:layout_below="@id/menu_home_made"
android:layout_marginTop="2dp"
android:text="Bakery"
android:textSize="18sp"
android:textColor="#FFFFFF"
android:id="@+id/menu_bakery"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/button"
android:layout_below="@id/menu_bakery"
android:id="@+id/menu_cart"
android:layout_marginTop="2dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/cart"
android:layout_marginRight="10dp"
android:layout_toLeftOf="@id/cart_text_view"
android:layout_centerVertical="true"
/>
<TextView
android:id="@+id/cart_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cart"
android:textAllCaps="true"
android:textColor="#FFFFFF"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textSize="18sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="18sp"
android:textColor="#FFFFFF"
android:layout_toRightOf="@id/cart_text_view"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"/>
</RelativeLayout>
</com.github.aakira.expandablelayout.ExpandableRelativeLayout>
</RelativeLayout>
</ScrollView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_marginTop="5dp"
android:layout_height="220dp"
android:id="@+id/banner_relative_layout"
android:background="@drawable/banner">
<ImageView
android:id="@+id/breakfast"
android:layout_width="600dp"
android:layout_height="70dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="17dp"
android:src="@drawable/breakfast" />
<ImageView
android:layout_width="100dp"
android:layout_height="60dp"
android:layout_below="@+id/breakfast"
android:layout_centerHorizontal="true"
android:src="@drawable/lunch"
android:layout_marginTop="5dp"
android:id="@+id/lunch" />
<ImageView
android:layout_width="100dp"
android:layout_height="60dp"
android:id="@+id/dinner"
android:layout_alignLeft="@+id/lunch"
android:layout_alignStart="@+id/lunch"
android:layout_below="@+id/lunch"
android:layout_marginTop="12dp"
android:src="@drawable/dinner" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/home_restaurant"
android:layout_marginTop="5dp"
android:layout_centerHorizontal="true"
android:textSize="18sp"/>
</RelativeLayout>
</LinearLayout>
这是我的Fragment java类(MenuFragment.java):
public class MenuFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.menu_fragment,container,false);
return rootView;
}
}
当我运行应用程序时,它会崩溃并向我显示此错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.foodshahi.www.foodshahi, PID: 5330
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.foodshahi.www.foodshahi/com.foodshahi.www.foodshahi.Home}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2572)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2654)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1488)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5728)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
Caused by: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class fragment
at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.foodshahi.www.foodshahi.Home.onCreate(Home.java:32)
at android.app.Activity.performCreate(Activity.java:6331)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2519)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2654)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1488)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5728)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.foodshahi.www.foodshahi.Home.onCreate(Home.java:32)
at android.app.Activity.performCreate(Activity.java:6331)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2519)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2654)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1488)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5728)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
Caused by: java.lang.NullPointerException
at java.lang.VMClassLoader.findLoadedClass(Native Method)
at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:362)
at java.lang.ClassLoader.loadClass(ClassLoader.java:499)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at android.support.v4.app.Fragment.isSupportFragmentClass(Fragment.java:499)
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3637)
at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:111)
at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:338)
at android.support.v4.app.BaseFragmentActivityApi14.onCreateView(BaseFragmentActivityApi14.java:39)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:67)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:754)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.foodshahi.www.foodshahi.Home.onCreate(Home.java:32)
at android.app.Activity.performCreate(Activity.java:6331)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2519)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2654)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1488)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5728)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
答案 0 :(得分:1)
首先,您应该使用
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</FrameLayout>
同时检查你在课堂上扩展的片段是 android.app.Fragment , getFragmentManager 是android.app.Fragment的片段。人们通常会通过扩展 android.support.v4.Fragment
来发生冲突答案 1 :(得分:0)
尝试更改
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/home_fragment">
到
<FrameLayout
android:layout_width="match_parent"
android:layout_height="150dp" //any particular size
android:id="@+id/home_fragment"/>
答案 2 :(得分:0)
如果您使用<fragment />
android:name="com.example.android.fragments.MyFragment"
,则应在xml中添加片段类详细信息。
在这种情况下,活动中不需要任何代码。
由于此信息在xml中不可用,因此充气机会抛出InflateException
并且您的应用程序崩溃。
请参阅documentation。
如果您想以编程方式执行此操作,请添加ViewGroup
,最好是FrameLayout
,然后通过Fragment
添加FragmentManager
。此外,还可以添加默认android.R.id.content
,这是添加了布局中所有视图的基础ViewGroup
。
答案 3 :(得分:0)
将
fragment
添加到Activity
或FramentActivity
所需的内容中 一个容器。该容器应该是&#34;Framelayout
&#34;,可以是 包含在xml中,否则您可以使用默认容器 喜欢&#34;android.R.id.content
&#34;删除或替换fragment
Activity
。
例如:
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />