This app contains activity_main.xml, mainactivit.java and one Fragment class.
activity_main.xml has one Button and FrameLayout inside RelativeLayout.
When the Button is clicked, FrameLayout should be replaced by fragments, but it doesn't get replaced.
activity_main.xml
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="loadd"
android:text="Fragment No.2" />
<FrameLayout
android:id="#+id/maincontainer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</RelativeLayout>
mainactivity.java
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void loadd(View v) {
try {
Fragment fragment = new fr();
FragmentManager frgManager = getSupportFragmentManager();
FragmentTransaction trans = frgManager.beginTransaction();
trans.replace(R.id.maincontainer, fragment);
trans.commit();
} catch (Exception e) {
Toast.makeText(MainActivity.this, "error " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
Fragment class
public class fr extends Fragment{
public View OnCreateView(LayoutInflater inf,ViewGroup parent,Bundle save){
return inf.inflate(R.layout.send,parent, false);
}
}
send.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textmsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment 3" />
</LinearLayout>
There is no compile time or run time error, but when I click that Button the FrameLayout is not replaced by the Fragment.
Everything that you posted looks good, except you aren't overriding Fragment.onCreateView, instead you've made a typo.
What you have
public View OnCreateView(LayoutInflater inf, ViewGroup parent, Bundle save) {
return inf.inflate(R.layout.test, parent, false);
}
What you should have
#Override
public View onCreateView(LayoutInflater inf, ViewGroup parent, Bundle save) {
return inf.inflate(R.layout.test, parent, false);
}
Pay close attention to the name of the method. It should begin with a lowercase "o".
Related
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 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 am practicing to multi-pane screen using fragment, but it's not working properly. I want two frame in single activity. One should be static and another will dynamic list.
main_biddingwindow.xml
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="200dp"
android:weightSum="1"
android:id="#+id/ag">
<fragment
android:layout_width="163dp"
android:layout_height="match_parent"
android:name="driver.project_detail"
android:id="#+id/fragment"
android:layout_row="0"
android:layout_column="0" />
<fragment
android:layout_width="184dp"
android:layout_height="match_parent"
android:name="driver.bid_list"
android:id="#+id/fragment2"
android:layout_row="0"
android:layout_column="19" />
</GridLayout>`
main.class
public class biddingWindow extends Activity{
protected void OnCreate(Bundle b){
super.onCreate(b);
setContentView(R.layout.main_biddingwindow);
}
}
bidlist.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
bid_list.java
public class bid_list extends Fragment {
public View onCreateView(LayoutInflater inflater ,ViewGroup container ,Bundle s){
return inflater.inflate(R.layout.bidlist,container, false);
}
}
output screen
Just Create the objects of both the fragments in activity and you can access everything in fragment
public class bid_list extends Fragment {
public ListView listView;
public View onCreateView(LayoutInflater inflater ,ViewGroup container ,Bundle s){
View view = inflater.inflate(R.layout.bidlist,container, false);
listView = (ListView)view.findViewById(R.id.listView);
return view ;
}
}
public class bid_list extends Fragment {
public ListView listView;
public View onCreateView(LayoutInflater inflater ,ViewGroup container ,Bundle s){
View view = inflater.inflate(R.layout.bidlist,container, false);
listView = (ListView)view.findViewById(R.id.listView);
return view ;
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/ag">
<FrameLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:name="driver.project_detail"
android:id="#+id/fragment"/>
<FrameLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:name="driver.bid_list"
android:id="#+id/fragment2"/>
</LinearLayout>
class Main extends Activity
{
protected void OnCreate(Bundle b){
super.onCreate(b);
setContentView(R.layout.main_biddingwindow);
FrameLayout frame1 = (FrameLayout)findViewById(R.id.fragment);
FrameLayout frame2 = (FrameLayout)findViewById(R.id.fragment);
Fragment1 fragment1 = new Fragment1();
Fragment2 fragment2 = new Fragment2();
frame1.removeAllView();
frame1.addView(fragment1)
frame2.removeAllView();
frame2.addView(fragment2)
}
}
I have a FragmentActivity that is supposed to swap out the front screen fragment (with buttons) to the fragment that the button points to when one is clicked.
Suppose the button that invokes SourceListFragment is clicked. Both onCreate() and onCreateView() of SourceListFragment are called, but somehow the screen stays the same, i.e. the front screen fragment isn't getting replaced.
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.main_screen, 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:layout_height="match_parent"
android:orientation="vertical">
<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
You add a Fragment into Linear layout with id main_screen, but the fragment should be added into FrameLayout. The transitions do work in case of LinearLayout, but the newly added Fragment is outside of the display.
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