Drawer navigation with Tabs - java

Googling for three days. Can't help myself. I have navigation drawer and fragment and everything works perfect except tabs in fragment. This is part of MainActiviti Class...
case R.id.linearLayoutHome:
fragment = new HomeFragment();
break; //this one works well
case R.id.linearLayoutHome:
fragment = new CategoryFragment(); //error showing here
break; //this one doesn't work
Calling fragment...
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();
Home fragment which works perfect:
public class HomeFragment extends Fragment {
public HomeFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
return rootView;
}
}
CategoryFragment which doesn't work. I think this is because of fragment in fragment:
public class CategoriesActivity extends Fragment {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
public CategoriesActivity(){
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
mSectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager());
mViewPager = (ViewPager) rootView.findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
return rootView;
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}
#Override
public int getCount() {
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title1).toUpperCase(l);
case 1:
return getString(R.string.title2).toUpperCase(l);
}
return null;
}
}
public static class DummySectionFragment extends Fragment {
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_tabbed_dummy, container, false);
TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label);
dummyTextView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}
}
I need some solution. No more ideas.

STEP 1:
Your onCreateView() should look like this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
return rootView;
}
and add this method to your Fragment:
#Override
public void onViewCreated(View view, Bundle savedInstanceState){
mSectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager());
mViewPager = (ViewPager) view.findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
STEP 2:
Make sure that your Activity extends from FragmentActivity or ActionBarActivity.
STEP 3:
Make sure that you are using android.support.v4.app.Fragment and not android.app.Fragment

Related

How to pass data from Fragment to Tablayout fragment

This is will run fragment in fragment that use viewpageAdapter.I want to pass data from fragment to tablayout fragment. i have try any method and still crash. i just want to passs once data to fragment in the tablayout.
First fragment. this line i want pass to tablayout fragment:-
specItem = getArguments().getString("spec_item");
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
imageUrl = getArguments().getString("image_url");
priceItem = getArguments().getString("price_item");
specItem = getArguments().getString("spec_item");
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_view_details_item, container, false);
textPrice = (TextView) rootView.findViewById(R.id.textPrice);
image_Details = (ImageView) rootView.findViewById(R.id.image_Detail);
tabLayout = (TabLayout) rootView.findViewById(R.id.tablayout_id);
viewPager = (ViewPager) rootView.findViewById(R.id.viewpage_id);
textPrice.setText(priceItem);
Picasso.get().load(imageUrl).into(image_Details);
ViewPageAdapter adapter = new ViewPageAdapter(getActivity().getSupportFragmentManager());
adapter.AddFragment(new FragmentSpecifications(), "Specification");
adapter.AddFragment(new FragmentReview(), "Review");
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
return rootView;
}
Second fragment in tablayout, i dont know how to recieve:-
View rootView;
TextView textSpec;
String specItem;
public FragmentSpecifications() {
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_specification, container, false);
textSpec = (TextView) rootView.findViewById(R.id.textviewspec);
textSpec.setText();
return rootView;
}
Inside your FragmentSpecifications create a instance of the Fragment to send data to it, like this:
public static FragmentSpecifications newInstance(String priceItem) {
Bundle args = new Bundle();
args.putString("priceItem", priceItem);
FragmentSpecifications fragment = new FragmentSpecifications();
fragment.setArguments(args);
return fragment;
}
After that in same FragmentSpecifications override onCreate method and get value:
private String priceItem;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null)
priceItem = getArguments().getString("priceItem");
}
And finally when you add FragmentSpecifications do adapter pass the value:
adapter.AddFragment(FragmentSpecifications.newInstance(priceItem), "Specification");

Fragment Bundle

Please Help me!
What I'm doing wrong?
in Fragment1
#Override
public static Fragment1 newInstance(String param) {
Fragment1 fragment = new Fragment1();
Bundle args = new Bundle();
args.putString("key","value");
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString("key");
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment1, container, false);
textview.setText(mParam1)
return v
in Activity onCreate
Fragment1.newInstance("text");
and in the end i have null in mParam1
Why?
I don't believe your mParam1 is null. I think your textview is null because you haven't referenced it. You need to give it a reference to the textview in the layout.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment1, container, false);
TextView textview = v.findViewById(R.id.textview);
textview.setText(mParam1);
return v;
}
Also, you need to change args.putString("key", "value") to args.putString("key", param) if you expect to get the string you passed to the new instance and not get "value" set to all your textviews.
Also, make sure you import android.support.v4.app.Fragment; and add this to your MainActivity's onCreate:
Fragment fragment = Fragment1.newInstance("Text");
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragmentContainer, fragment, null)
.commit();
Where R.id.fragmentContainer is the id of your FrameLayout in your activity_main.xml.
<FrameLayout
android:id="#+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
If you don't have this, your onCreate in your fragment will never get called and will never inflate. I can't think of anything else it can be because your fragment code is fine. So the problem is in your main Activity.
You have to put the args to the Fragment from the parent Activity.
For instance if you use a ViewPager, in the adapter do the following when instantiating :
public class ParentActivity extends FragmentActivity {
private class MyAdapter extends FragmentStatePagerAdapter {
MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
fragment.setArguments(args);
switch (position) {
case 0:
Fragment1 fragment = new Fragment1();
Bundle args = new Bundle();
args.putString("key", "MyString");
fragment.setArguments(args);
return fragment;
}
}
#Override
public int getCount() {
return 1;
}
}
}

Android , Fragments into fragment into activity communication

This is my problem.In my android app I have "Activity 1" that contains "fragment1". "Fragment1" in his layout contains a "viewpager" with 3 pages "fragment3", "fragment4" and "fragment5". I "Activity 2" that contains "Fragment2". Now I want to execute an action in "Fragment" 1, 2 or 3 and then launch the activity2, so send data from fragments 3,4 or 5 and display them in the fragment 6. I am willing to go through "interfaces" who will be in the fragments 3, 4, 5 to respect the java standards. Since, I can not, I get errors. In landscape mode the fragments 2 and 3 are side by side
For example here are
Fragment1
public class Frag1 extends Fragment implements Frag3.Communicator{
private ViewPager mPager = null;
private TabLayout mTab = null;
MyAdapter adapter;
private int p;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.frag4, container, false);
mPager = (ViewPager) v.findViewById(R.id.viewPager);
mTab = (TabLayout) v.findViewById(R.id.tabLayout);
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
mPager.setAdapter(new MyAdapter(fragmentManager));
mTab.setupWithViewPager(mPager);
return v;
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public void respond(String data) {
}
class MyAdapter extends FragmentStatePagerAdapter
{
public MyAdapter(FragmentManager fm)
{
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment fragment = null;
if(position == 0)
{
fragment = new Frag1();
p = position;
}
if(position == 1)
{
fragment = new Frag2();
p = position;
}
if(position == 2)
{
fragment = new Frag3();
p = position;
}
return fragment;
}
#Override
public int getCount() {
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
if(position == 0)
{
return "Tab 1";
}
if(position == 1)
{
return "Tab 2";
}
if(position == 2)
{
return "Tab 3";
}
return null;
}
}
}
Fragment 3
public class Frag3 extends Fragment {
Button b;
Communicator comm;
public void setCommunicator(Communicator c)
{
comm = c;
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.frag1, container, false);
return v;
}
public interface Communicator
{
public void respond(String data);
}
public void openSub()
{
comm.respond("Sub ouvert depuis le fragment numéro 1");
}
}
Fragment2
public class Frag2 extends Fragment {
private TextView tv;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.frag5, container, false);
tv = (TextView) v.findViewById(R.id.frag5Tv);
return v;
}
public void changeData(String data)
{
tv.setText(data);
}
}
I believe you should unify architecture either with one Activity or with two Activities but for both orientations. In case with one Activity you can communicate via Activity's interface. Just replace Fragment1 with Fragment2 to change view.
In case with two Activities you can communicate via startActivityForResult/onActivityResult methods.

my app crashes when I swipe from one fragment to another (error: canScrollHorizontally()' on a null object reference)

I've been trying to resolve this problem but with no result. My app is made up of 4 fragments, the second one with a recycler view. When I swipe from the first to the second my app instantly crashes. And in the logCat this error appears:
Attempt to invoke virtual method 'boolean android.support.v7.widget.RecyclerView$LayoutManager.canScrollHorizontally()' on a null object reference at android.support.v7.widget.RecyclerView.onInterceptTouchEvent(RecyclerView.java:2022)
Here's my code for the main activity:
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
toolbar = (Toolbar) findViewById(R.id.app_bar);
if (toolbar != null) {
toolbar.setTitle("Quick!");
}
setSupportActionBar(toolbar);
fragmentList.add(HomeFragment.getInstance(0));
fragmentList.add(KartFragment.getInstance(1));
fragmentList.add(FavouriteFragment.getInstance(2));
fragmentList.add(ScannerFrag.getInstance(3));
pager = (ViewPager) findViewById(R.id.Pager);
pager.setAdapter(new myPagerAdapter(getSupportFragmentManager()));
tab = (SlidingTabLayout) findViewById(R.id.SlidingTab);
tab.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
#Override
public int getIndicatorColor(int position) {
return getResources().getColor(R.color.accentColor);
}
});
tab.setViewPager(pager);
}
class myPagerAdapter extends FragmentPagerAdapter {
String[] tabs;
public myPagerAdapter(FragmentManager fm) {
super(fm);
tabs = getResources().getStringArray(R.array.tabs);
}
#Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
#Override
public CharSequence getPageTitle(int position) {
return tabs[position];
}
public int getCount() {
return fragmentList.size();
}
}
Home fragment:
public class HomeFragment extends Fragment {
private Toolbar toolbar;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View layout;
layout = inflater.inflate(R.layout.home_layout, container, false);
toolbar = (Toolbar) layout.findViewById(R.id.subToolbar);
toolbar.setTitle("Dove mangi oggi?");
return layout;
}
public static HomeFragment getInstance(int position) {
HomeFragment frag = new HomeFragment();
Bundle args = new Bundle();
args.putInt("position", position);
frag.setArguments(args);
return frag;
}
}
KartFragment:
public class KartFragment extends Fragment {
private Toolbar toolbar;
private ItemAdapter ad;
private RecyclerView rv;
private ArrayList list = new ArrayList<>();
private static final String SAVELIST = "SAVELIST";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View layout;
if (savedInstanceState != null) {
list = savedInstanceState.getParcelableArrayList(SAVELIST);
} else {
list = KartObjectClass.listBuilder(10);
}
layout = inflater.inflate(R.layout.fragment_layout, container, false);
toolbar = (Toolbar) layout.findViewById(R.id.subToolbar);
toolbar.setTitle("I tuoi ordini:");
rv = (RecyclerView) layout.findViewById(R.id.list);
rv.setLayoutManager(new LinearLayoutManager(getActivity()));
ad = new ItemAdapter(list);
rv.setHasFixedSize(true);
rv.setAdapter(ad);
return layout;
}
#Override
public void onSaveInstanceState(Bundle outState) {
outState.putParcelableArrayList(SAVELIST, list);
super.onSaveInstanceState(outState);
}
public static KartFragment getInstance(int position) {
KartFragment frag = new KartFragment();
Bundle args = new Bundle();
args.putInt("position", position);
frag.setArguments(args);
return frag;
}
}
Favourite fragment:
public class FavouriteFragment extends Fragment {
private Toolbar toolbar;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View layout = null;
layout = inflater.inflate(R.layout.scanner_layout, container, false);
toolbar = (Toolbar) layout.findViewById(R.id.subToolbar);
toolbar.setTitle("I tuoi preferiti:");
return layout;
}
public static FavouriteFragment getInstance(int position) {
FavouriteFragment frag = new FavouriteFragment();
Bundle args = new Bundle();
args.putInt("position", position);
frag.setArguments(args);
return frag;
}
}
Try to set the LayoutManager before you set the Adapter on the RecyclerView.
I solved the error, the problem was that I had set a recycler view in the homeFragment XML. As I forgot to have it, I didn't call it in the onCreateView method and the app was crashing because no adapter was set.

SectionsPagerAdapter - No view found for id for fragment

I have a App with a Slidingmenu where i can pick different Fragment, which displays different kind of ListFragments.
The ListFragments will be filled by JSON from my Database Server.
If you Click on one of the Listitems, a new Fragment is shown which contains more Details about the selected Listitem.
public void updateList(final Activity a, final ListFragment L) {
adapter = new SimpleAdapter(a, mCommentList,
R.layout.single_comment, new String[] {TAG_PIC_ID,TAG_CATEGORY, TAG_ACTIVITY, TAG_DATUM, TAG_AKTUSR, TAG_MAXUSR, TAG_GENDER, /*TAG_POST_ID,*/ TAG_TITLE, TAG_MESSAGE,
TAG_USERNAME }, new int[] { R.id.imgrow, R.id.category, R.id.activity/*R.id.id*/ , R.id.datum, R.id.aktusr, R.id.maxusr, R.id.gender, /*R.id.category,*/ R.id.title, R.id.message,
R.id.username });
L.setListAdapter(adapter);
ListView lv = L.getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
int intid = (int)id;
Details nextFrag= new Details();
L.getFragmentManager().beginTransaction()
.replace(R.id.frame_container, nextFrag, "Test")
.addToBackStack(null)
.commit();
}
});
}
When i call the function from a "single" fragment its no problem and the DetailPage is shown.
The Problem is when i call the function from a TabbedActivity with a SectionsPagerAdapter.
The TabbedActivity
package info.androidhive.slidingmenu;
public class TabbedActivity extends Fragment {
SectionsPagerAdapter mSectionsPagerAdapter;
public static final String TAG = TabbedActivity.class.getSimpleName();
ViewPager mViewPager;
public static TabbedActivity newInstance() {
return new TabbedActivity();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_item_one, container, false);
mSectionsPagerAdapter = new SectionsPagerAdapter(
getChildFragmentManager());
mViewPager = (ViewPager) v.findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
return v;
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment fragment = new newEntrys();
switch (position) {
case 0:
fragment = new newEntrys();
break;
case 1:
fragment = new oldEntrys();
break;
default:
break;
}
return fragment;
}
#Override
public int getCount() {
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return "Aktuelle Einträge";
case 1:
return "Vergangene Einträge";
}
return null;
}
}}
The TabbedActivity contains the 2 Fragments newEntry and oldEntry which are nearly equal.
package info.androidhive.slidingmenu.fragments;
public class RegisterdEvents_new extends ListFragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.read, container, false);
return rootView;
}
public void startTask(){
Read SO = new Read();
Load LEvt = SO.new Load(getActivity(), this, "NEW");
LEvt.execute();
}
public void onResume() {
startTask();
super.onResume();
}
}
So the function updateList above is called in
Load LEvt = SO.new Load(getActivity(), this, "NEW");
But the problem is that i always get the Error
No view found for id 0x7f0a000f (package:id/frame_container) for fragment Details{e3992e2 #0 id=0x7f0a000f Test}
Someone of you can help me to solve this problem?

Categories

Resources