im trying to set up a simple app using 2 fragments. For this i created 3 classes:
MainActivity:
public class MainActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment_A frag_A = new Fragment_A();
fragmentTransaction.add(R.id.map_container, frag_A);
Fragment_B frag_B = new Fragment_B();
fragmentTransaction.add(R.id.map_container2, frag_B);
fragmentTransaction.commit();
}
}
Fragment_A:
public class Fragment_A extends Fragment{
MapView map;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_a_layout, container, false);
map = (MapView) v.findViewById(R.id.map_object);
map.onCreate(savedInstanceState);
return v;
}
}
Fragment_B:
public class Fragment_B extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_b_layout, container, false);
v.setBackgroundColor(Color.BLUE);
return v;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="#+id/map_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/map_container1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:orientation="horizontal" >
</LinearLayout>
<LinearLayout
android:id="#+id/map_container2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:orientation="horizontal" >
</LinearLayout>
</LinearLayout>
fragment_a_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map_object"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
</FrameLayout>
fragment_b_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
It happens that everytime i try to run this app i have this:Binary XML file line #6 Error inflating class fragment, where #6 is:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="#+id/map_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
...
I dont understand what can be the problem, I simply want to see both fragments on my screen.
Related
I'm am trying to add dynamycally multiple fragments I'm creating to a framelayout, using FragmentTransaction's add() method. Each time I try to do it, Fragments are just replacing one an other.
Here is my code :
public class FragmentMerchandSearch extends Fragment {
public FragmentMerchandSearch() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_merchand_search, container, false);
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
for(int i = 0; i < 10; i++){
Fragment newFragment = new FragmentMerchandPresentation();
ft.add(R.id.container_merchand_presentation_for_search, newFragment);
}
ft.commit();
return view;
}
}
Here is the code of FragmentMerchandPresentation:
public class FragmentMerchandPresentation extends Fragment {
public FragmentMerchandPresentation(){
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_presentation_merchand, container, false);
return view;
}
}
Here is XML of fragment_merchand_search:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/container_merchand_presentation_for_search">
</FrameLayout>
</LinearLayout>
</ScrollView>
And XML of fragment_presentation_merchand:
<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hey !"/>
</LinearLayout>
You are adding your fragments inside a FrameLayout, this means one is above another.
To add them vertically, use a LinearLayout as a container.
Just change your fragment_merchand_search.xml to:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/container_merchand_presentation_for_search"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</ScrollView>
Just change this line:
ft.add(R.id.container_merchand_presentation_for_search, newFragment);
to this:
ft.replace(R.id.container_merchand_presentation_for_search, newFragment);
Can't get rid of this problem. I saw a couple other questions with a similar problem, but nothing fixed it.
Short story: I replace my placeholders with fragments in my MainActivty and would like to call a public method from that new fragment. But calling the fragment from his Tag, leads to a "Null object reference" error.
I tried multiple ways to set and call the Tag but without success
Main.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:id="#+id/mainActivity"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:menu="#menu/quick_settings"
tools:context="....">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/scrollView">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/fragment1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
<LinearLayout
android:id="#+id/fragment2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
<LinearLayout
android:id="#+id/fragment3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
...
Main.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_annual_service);
createList();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
private void createList() {
Resources r = getResources();
String packageName = getPackageName();
//Array with all Fragments
fragmentList = new Fragment[]{
new FragmentA,
new FragmentB,
new FragmentC,
...
};
numberOfFragments = fragmentList.length;
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
for (int arrayIndex=0, fragmentNumber=1; fragmentNumber <= numberOfFragments; arrayIndex++, fragmentNumber++){
fragmentTransaction.replace(r.getIdentifier("fragment" + fragmentNumber, "id", packageName), fragmentList[arrayIndex], "fragment0"+fragmentNumber); // also try to set the TagName
}
fragmentTransaction.commit();
fragmentManager.executePendingTransactions();
((Unfold)fragmentManager.findFragmentByTag("fragment01")).unfold(); // Method is implemented by a interface
FragmentA.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="#+id/FragmentA"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingEnd="20dp"
android:paddingStart="45dp"
android:visibility="gone">
...
</LinearLayout>
FragmentA.java
Button backBtn;
TextView header;
LinearLayout llayout;
Drawable defaulBG;
View view;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return setListener(inflater, container);
}
private View setListener(LayoutInflater inflater, ViewGroup container){
view = inflater.inflate(R.layout.fragment_general_information, container, false);
header = (TextView) view.findViewById(R.id.generalInformationHeaderText);
header.setOnClickListener(this);
llayout = (LinearLayout) view.findViewById(R.id.content);
defaulBG = inForkliftBtn.getBackground();
backBtn = (Button) view.findViewById(R.id.backBtn);
setRetainInstance(true);
return view;
}
public void unfold(){
if(llayout.getVisibility() == View.GONE){
llayout.setVisibility(View.VISIBLE);
}else{
llayout.setVisibility(View.GONE);
}
}
Call executePendingTransactions() on fragment manager after committing transaction
...
fragmentTransaction.commit();
fragmentManager.executePendingTransactions();
((Unfold)fragmentManager.findFragmentByTag("fragment01")).unfold();
EDIT 1
You can try another option:
Do not add the executePendingTransactions()
In fragment's onViewCreated, if getTag() mathes "fragment01" call unfold method
I have created an app when i'm testing my app or i clicked on back button my fragment is overlapping other fragment. I don't know why it's happening. Please help me i need help..
My codes
public class MainActivity extends AppCompatActivity {
private SharedPreferences pref;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pref = getPreferences(0);
initFragment();
}
private void initFragment(){
Fragment fragment;
if(pref.getBoolean(Constants.IS_LOGGED_IN,false)){
fragment = new ProfileFragment();
}else {
fragment = new LoginFragment();
}
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragment_frame,fragment);
ft.commit();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
android:background="#ff51b46d"
tools:context="com.example.hldev.docup.MainActivity">
<LinearLayout 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"
tools:context="com.example.hldev.docup.MainActivity">
<FrameLayout
android:id="#+id/fragment_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
Uploads.java
public class Uploads extends Fragment implements View.OnClickListener{
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.activity_uploads, container, false);
ivAttachment = (ImageView)view.findViewById(R.id.ivAttachment);
tv_name = (TextView)view.findViewById(R.id.tv_name);
tv_email = (TextView)view.findViewById(R.id.tv_email);
bUpload = (Button)view.findViewById(R.id.b_upload);
tvFileName = (TextView)view.findViewById(R.id.tv_file_name);
ivAttachment.setOnClickListener(this);
bUpload.setOnClickListener(this);
return view;
}
private void goToProfile(){
Fragment profile = new ProfileFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragment_frame,profile);
ft.commit();
}
}
Just set a background color to your new <fragment /> in XML file or else it will be transparent and show a replaced fragment.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="schemas.android.com/apk/res/android";
xmlns:tools="schemas.android.com/tools";
android:id="#+id/frag24"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/green">
<!-- Your Content -->
</RelativeLayout>
</FrameLayout >
set white background color of your parent layout of your activity_uploads. also do same with all layout which you use in Fragment.
replace your code with below.
<RelativeLayout xmlns:android="schemas.android.com/apk/res/android"
xmlns:tools="schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#android:color/holo_green_dark"
tools:context="com.example.hldev.docup.Uploads">
i am trying to replace a displaynews fragment with homescreen fragment but the fragment is being adding instead of replacing in the code here
content_main.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:id="#+id/content_main"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.ajitesh.newson.MainActivity"
tools:showIn="#layout/app_bar_main">
<fragment
android:id="#+id/home_fragment"
android:name="com.ajitesh.newson.DisplayNews"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
mainacitivity.class
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentClass=HomeScreen.class;
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.home_fragment,fragment).commit();
displaynews.class
public class DisplayNews extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.displaynews, container, false);
}
}
displaynews.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:id="#+id/display_news"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="helllooooo"/>
</LinearLayout>
HomeScreen.class
public class HomeScreen extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.home_screen, container, false);
}
}
home_screen.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text1"
android:text="this is home page"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="click"/>
</LinearLayout>
Fragments cannot be replaced if hard-coded in xml, you should add it dynamically to replace a fragment with another.
// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack if needed
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
source // example -- still getting problems? You may find a solution here
I'm trying to switch from an Activity with a fragment without a Toolbar (R.id.main_screen) to one that has a Toolbar (R.id.source_items_list).
However, whenever I have a Toolbar in R.id.source_items_list, it crashes. The error I'm getting is
android.view.InflateException: Binary XML file line #10: Error
inflating class android.support.v7.widget.Toolbar
which comes from the line
View view = inflater.inflate(R.layout.fragment_sourceitem_list, viewGroup, false); in the following code.
I wonder what I'm doing wrong. Here's my code.
public class MainActivity extends FragmentActivity implements MainFragment.OnFragmentInteractionListener {
...
#Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
// set uncaught exception handler for thread
// Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getSupportFragmentManager();
mMainFrame = MainFragment.newInstance();
fragmentManager.beginTransaction().add(R.id.container, mMainFrame).commit();
}
#Override
public void onFragmentInteraction(int id) {
FragmentManager fragmentManager = getSupportFragmentManager();
switch (id) {
case R.id.button_sources:
Fragment fragment = new SourceListFragment();
FragmentTransaction fragmentTrans = fragmentManager.beginTransaction();
fragmentTrans.replace(R.id.container, fragment);
fragmentTrans.addToBackStack(null);
fragmentTrans.commit();
break;
default:
break;
}
}
}
public class SourceListFragment extends Fragment implements AbsListView.OnClickListener {
...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState) {
Log.d("SOURCELIST", "SourceListFragment onCreateView\n");
View view = inflater.inflate(R.layout.fragment_sourceitem_list, viewGroup, false);
return view;
}
public static SourceListFragment newInstance(BluetoothAdapter adapter) {
SourceListFragment fragment = new SourceListFragment();
mBTAdapter = adapter;
return fragment;
}
// Container Activity must implement this interface
public interface OnFragmentInteractionListener {
public void onFragmentInteraction(int id);
}
}
Here's the layout:
activity_main.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:materialdesign="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:background="#FFF"
android:orientation="vertical"
android:id="#+id/container">
</FrameLayout>
fragment_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_screen"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="148dp">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="128dp"
android:background="#01579b"
android:paddingBottom="20dp"
android:paddingLeft="104dp" android:id="#+id/banner">
</RelativeLayout>
</RelativeLayout>
<android.support.percent.PercentRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/button_sources"
app:layout_widthPercent="30%"
app:layout_heightPercent="30%"
app:layout_marginTopPercent="10%"
app:layout_marginLeftPercent="15%"
app:layout_marginRightPercent="5%"/>
...
</android.support.percent.PercentRelativeLayout>
</LinearLayout>
fragment_sourceitem_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="#+id/source_items_list"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/toolbar"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:background="?attr/colorPrimary"/>
<ListView android:id="#android:id/list" android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<com.gc.materialdesign.views.ProgressBarCircularIndeterminate
android:id="#+id/progressBarCircularIndeterminate"
android:layout_width="32dp"
android:layout_height="32dp"
android:background="#1E88E5" />
</FrameLayout>
Any help will be appreciated.
Thanks