简体   繁体   中英

My fragments won't show on my Activity

I am new to android develpment and also new to the fragments .

So , i've created 6 xml files that i am trying to import to an activity as fragments , i've done everything step by step from the Google's instructions but the fragments won't show up in my activity menu .

ActivityMenu.xml file

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <fragment android:name="com.grig.alexios.pdaapplication.BeerMenuFragment"
        android:id="@+id/headlines_fragment"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent" />

    <fragment android:name="com.grig.alexios.pdaapplication.GinMenuFragment"
        android:id="@+id/article_fragment"
        android:layout_weight="2"
        android:layout_width="0dp"
        android:layout_height="match_parent" />


</LinearLayout>

MenuActivity.class

    package com.grig.alexios.pdaapplication;

import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MenuActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);
    }
}

EDIT

Fragment example

GinMenuFragment

    package com.grig.alexios.pdaapplication;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class GinMenuFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.gin, container, false);
    }
}

You need a FragmentManager in order to display fragments. You can change fragments using FragmentManager's transactions.

Read more about FragmentManager here: https://developer.android.com/reference/android/app/FragmentManager.html

I have tested your code and it's working properly

well, this will work.

ActivityMenu.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<FrameLayout
    android:id="@+id/headlines_fragment"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1" />

<FrameLayout
    android:id="@+id/article_fragment"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="2" />

and inside MenuActivity.java

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        getSupportFragmentManager().beginTransaction().replace(R.id.article_fragment,new FragA(),"article").commit();
        getSupportFragmentManager().beginTransaction().replace(R.id.headlines_fragment,new FragB(),"headlines").commit();

    }
}

finally, might be a problem with support library version. try another version and clean your project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM