Huy guys, I have problem with my App. I created viewpager with some fragment.
this my viewpager ini my parent activity
private int current_posisition_page ;
#Override
protected v
oid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
.....
viewpager.setAdapter(new PagePagerAdapter(getSupportFragmentManager()));
viewpager.setCurrentItem(999);
viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
// This method will be invoked when a new page becomes selected.
#Override
public void onPageSelected(int position) {
current_posisition_page = position;
}
// This method will be invoked when the current page is scrolled
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
.......
}
private class PagePagerAdapter extends FragmentPagerAdapter {
public PagePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int pos) {
return PageFragment.newInstance(pos);
}
#Override
public int getCount() {
return 1000;
}
}
}
and this my fragment
public class PageFragment extends Fragment {
public static PageFragment newInstance(int page_index) {
PageFragment fragment = new PageFragment();
Bundle args = new Bundle();
args.putString(Variabel.page_index, String.valueOf(page_index));
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
page_index = getArguments().getString(Variabel.page_index);
}
activity=(MainActivity)getActivity();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.item_page, container, false);
ButterKnife.bind(this, v);
return v;
}
public void setSelectedAyat(String aya_id, String sura_name) {
surat.setText("Surat " + sura_name);
ayat.setText("Ayat " + aya_id);
}
}
Now I called method current selected fragment viewpager from parent activity:
PageFragment page = (PageFragment) adapter.getItem(current_posisition_page);
page.setSelectedAyat(aya_id, sura_name);
but I get Error :
java.lang.ClassCastException: java.lang.Integer cannot be cast to
com.ad.alquran.kemendag.fragment.PageFragment
in line : PageFragment page = (PageFragment) adapter.getItem(current_posisition_page);
so how to fix it ? thanks
Probably this line causing issue:
viewpager.setAdapter(new PagePagerAdapter(getSupportFragmentManager()));
Because adapter object for calling getItem method but in ViewPager setAdapter method passing different object of PagePagerAdapter class:
adapter=new PagePagerAdapter(getSupportFragmentManager());
viewpager.setAdapter(adapter);
and also make sure calling getItem method after setting adapter for ViewPager.
EDIT:
java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.widget.TextView.setText(java.lang.CharSequence)' on a null
object reference
Because not initializing surat and ayat. do it as:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.item_page, container, false);
//...
surat=(TextView)v. findViewById(R.id.<surat_id_in_xml>);
ayat=(TextView)v. findViewById(R.id.<ayat_id_in_xml>);
return v;
}
Related
I'm pretty new in android development and I have some questions about communication between fragments and main activity.
I didn't success to solve my problem in my particular case. What I want to do is to modify the alpha of a view inside a fragment (something like a card) when scrolling from one fragment to the one with this view. I tried different things but I failed (null pointer exception), you can see some experiments in a comment. Here is my code and my architecture:
Fragment with the view:
public class VideoviewFragment extends BaseFragment {
/* public View card;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.fragment_videoview, container, false);
card = (View) rootview.findViewById(R.id.card_video);
return rootview;
}
public void setalphacard(float value){
card.setAlpha(value);
}*/
public static VideoviewFragment create() {
return new VideoviewFragment();
}
#Override
public int getLayoutResId() {
return R.layout.fragment_videoview;
}
#Override
public void inOnCreateView(View root, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
}
}
Base fragment:
public abstract class BaseFragment extends Fragment {
private View mRoot;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
mRoot = inflater.inflate(getLayoutResId(), container, false);
inOnCreateView(mRoot, container, savedInstanceState);
return mRoot;
}
#LayoutRes
public abstract int getLayoutResId();
public abstract void inOnCreateView(View root, #Nullable ViewGroup container,#Nullable Bundle savedInstanceState);
}
Main Activity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Viewpager
final ViewPager viewPager = (ViewPager) findViewById(R.id.am_view_pager);
final MainPagerAdapter adapter = new MainPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
// tabview et viewpager
viewPager.setCurrentItem(1);
// Changement couleurs swipe
viewPager.setBackgroundColor(Color.rgb(135,206,250));
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
if(position == 1) {
viewPager.setBackgroundColor((Integer) new ArgbEvaluator().evaluate(positionOffset, Color.rgb(135,206,250),Color.rgb(240,15,45)));
//videoviewFragment.setalphacard(positionOffset);
}
if(position == 0) {
viewPager.setBackgroundColor((Integer) new ArgbEvaluator().evaluate(positionOffset, Color.TRANSPARENT,Color.rgb(135,206,250)));
}
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
}
}
Adapter:
public class MainPagerAdapter extends FragmentPagerAdapter {
public MainPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
return CameraFragment.create();
case 1:
return OptionFragment.create();
case 2:
return VideoviewFragment.create();
}
return null;
}
#Override
public int getCount() {
return 3;
}
}
XML VideoviewFragment
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout0"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="#+id/card_video"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="70dp"
android:background="#drawable/card_background_video"/>
</RelativeLayout>
(I know there is no instance of my fragment in this activity...)
I found few similar topics on StackOverflow like this one Use view from fragment xml file, but I'm doing something wrong...
Thanks for your help!
When the screen updates and changes tabs, the text of the other tabs still have the characteristics of the first.
private int mPage;
public static FeedFrag newInstance(int page) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
FeedFrag fragment = new FeedFrag();
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPage = getArguments().getInt(ARG_PAGE);
}
// Inflate the fragment layout we defined above for this fragment
// Set the associated text for the title
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.feed_frag, container, false);
TextView first = (TextView) view.findViewById(R.id.Feed);
first.setText("feed");
return view;
}
And this is another tab
public class InStockFrag extends Fragment {
public static final String ARG_PAGE = "ARG_PAGE";
private int mPage;
public static InStockFrag newInstance(int page) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
InStockFrag fragment = new InStockFrag();
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPage = getArguments().getInt(ARG_PAGE);
}
// Inflate the fragment layout we defined above for this fragment
// Set the associated text for the title
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.in_stock_frag, container, false);
TextView second = (TextView) view.findViewById(R.id.InStock);
second.setText("In Stock");
return view;
}
}
The Main Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the ViewPager and set it's PagerAdapter so that it can display items
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(new SimpleFragmentPagerAdapter(getSupportFragmentManager()));
// Give the PagerSlidingTabStrip the ViewPager
PagerSlidingTabStrip tabsStrip = (PagerSlidingTabStrip) findViewById(R.id.tabs);
tabsStrip.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
// This method will be invoked when a new page becomes selected.
#Override
public void onPageSelected(int position) {
// Toast.makeText(getActivity().this, "Selected page positon: " + position, Toast.LENGTH_SHORT).show();
}
// This method will be invoked when the current page is scrolled
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
// Code goes here
}
// Called when the scroll state changes:
// SCROLL_STATE_IDLE, SCROLL_STATE_DRAGGING, SCROLL_STATE_SETTLING
#Override
public void onPageScrollStateChanged(int state) {
// Code goes here
}
});
tabsStrip.setViewPager(viewPager);
}
SimpleFragmentAdapter:
public class SimpleFragmentPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 3;
private String tabTitles[] = new String[] { "Feed", "In Stock", "Upcoming" };
public SimpleFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getCount() {
return PAGE_COUNT;
}
#Override
public Fragment getItem(int position) {
return FeedFrag.newInstance(position + 1);
}
#Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
return tabTitles[position];
}
}
The app returns the text on the 3 tabs as "feed" instead of the other words.
Yes, obviously. It's because you are not calling correctly your fragments inside the getItem method in your SimpleFragmentPagerAdapter: you were always calling the FeedFrag.
Try to change the code of your view pager adapter like this, and inside the getItem method, put the code to call your third fragment, the one that have title "Upcoming":
public class SimpleFragmentPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 3;
private String tabTitles[] = new String[] { "Feed", "In Stock", "Upcoming" };
public SimpleFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getCount() {
return PAGE_COUNT;
}
#Override
public Fragment getItem(int position) {
if (position==0) {
return FeedFrag.newInstance(position + 1);
} else if (position == 1) {
return InStockFrag.newInstance(position + 1);
} else {
//HERE RETURN SAME METHOD OF UPCOMING FRAGMENT
}
}
#Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
return tabTitles[position];
}
}
I have an Activity with viewpager in one page should be EditText and other page be the TextView, get data from first fragment to show in second view but I'm struggling because of lack of knowledge, I have Main Activity as
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
}
private class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int pos) {
switch(pos) {
case 0: return EditFragment.newInstance("FirstFragment, Instance 1");
case 1: {
Log.e("TAG", " is " + new EditFragment().getTag());
return PreviewFragment.newInstance(new EditFragment().getTag());
}
default: return EditFragment.newInstance("ThirdFragment, Default");
}
}
#Override
public int getCount() {
return 2;
}
}
}
main layout is
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/viewpager"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
EditFragment is
public class EditFragment extends Fragment {
public EditFragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_editor, container, false);
EditText tv = (EditText) v.findViewById(R.id.content);
tv.setText(getArguments().getString("msg"));
return v;
}
public static EditFragment newInstance(String text) {
EditFragment f = new EditFragment();
Bundle b = new Bundle();
b.putString("msg", text);
f.setArguments(b);
return f;
}
}
PreviewFragment
public class PreviewFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_markdown, container, false);
TextView tvView = (TextView) v.findViewById(R.id.markdownView);
tvView.loadMarkdown(getArguments().getString("msg"));
return v;
}
public static PreviewFragment newInstance(String text) {
PreviewFragment f = new PreviewFragment();
Bundle b = new Bundle();
b.putString("msg", text);
f.setArguments(b);
return f;
}
}
there I do not know how to pass data from EditFragment to PreviewFragment, please help.
I'm newbie to Fragments please help me.
UPDATE
I have changed
public class PreviewFragment extends Fragment {
private TextView textView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_markdown, container, false);
textView = (TextView) v.findViewById(R.id.title);// initialized here
return v;
}
public void b_updateText(String t){
Log.e("LOAD TXT", t);// Here I can get data Data is available
textView.setText(t); // but Here I get NPE, don't know why.
}
}
but Here I get NPE, don't know why.
and EditFragment is
public class EditFragment extends Fragment {
public EditFragment(){}
private static Bundle bundle = null;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_editor, container, false);
final EditText tv = (EditText) v.findViewById(R.id.content);
final PsgerActivity mainActivity = ( PsgerActivity) getActivity();
mainActivity.message= tv.getText().toString();
tv.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
bundle = new Bundle();
bundle.putString("md", tv.getText().toString());
Log.e("BUNDLE", bundle.getString("md"));// Data is available
}
#Override
public void afterTextChanged(Editable editable) {
}
});
return v;
}
public static EditFragment newInstance() {
EditFragment f = new EditFragment();
f.setArguments(bundle);
return f;
}
}
MainActivity is
public class MainActivity extends AppCompatActivity {
public String message;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
if (position == 1) {
PreviewFragment fragmentB = new PreviewFragment();
EditFragment ef = EditFragment.newInstance();
try{
Log.e("ACTIVITY", ef.getArguments().getString("md"));// Data is available
} catch (Exception e){
e.printStackTrace();
}
fragmentB.b_updateText(ef.getArguments().getString("md"));
}
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
}
private class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int pos) {
switch(pos) {
case 0: return EditFragment.newInstance();
case 1: return new PreviewFragment();
default: return EditFragment.newInstance();
}
}
#Override
public int getCount() {
return 2;
}
}
}
but Here I get NPE, don't know why.
Have you had a chance go through android docs?
Communicating with Other Fragments
Of course you can use Otto or EventBus library.
And also have a look at this answer.
Create a variable in MainActivity and access that variable from fragment through
MainActivity mainActivity = ( MainActivity) getActivity();
mainActivity.message="Hai from EditFragment"
Check out the below code:
public class MainActivity extends AppCompatActivity {
public String message;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
}
private class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int pos) {
switch(pos) {
case 0: return EditFragment.newInstance("FirstFragment, Instance 1");
case 1: {
Log.e("TAG", " is " + new EditFragment().getTag());
return PreviewFragment.newInstance(new EditFragment().getTag());
}
default: return EditFragment.newInstance("ThirdFragment, Default");
}
}
#Override
public int getCount() {
return 2;
}
}
}
EditFragment is
public class EditFragment extends Fragment {
public EditFragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_editor, container, false);
EditText tv = (EditText) v.findViewById(R.id.content);
tv.setText(getArguments().getString("msg"));
MainActivity mainActivity = ( MainActivity) getActivity();
mainActivity.message="Hai from EditFragment"
//store data through message variable inside activity
return v;
}
}
PreviewFragment
public class PreviewFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_markdown, container, false);
TextView tvView = (TextView) v.findViewById(R.id.markdownView);
MainActivity mainActivity = ( MainActivity) getActivity();
tvView.loadMarkdown(mainActivity.message);// get data through message variable of main activity
return v;
}
}
With FragmentManager, we can access another fragment from our current fragment by calling findFragmentByTag(<tag of target fragment>).
// First Fragment, from we're sending data
public class MyFragmentA extends Fragment {
EditText A_input;
Button A_enter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myFragmentView = inflater.inflate(R.layout.fragment_a, container, false);
A_input = (EditText)myFragmentView.findViewById(R.id.a_input);
A_enter = (Button)myFragmentView.findViewById(R.id.a_enter);
A_enter.setOnClickListener(A_enterOnClickListener);
return myFragmentView;
}
OnClickListener A_enterOnClickListener = new OnClickListener(){
#Override
public void onClick(View arg0) {
String textPassToB = A_input.getText().toString();
String TabOfFragmentB = ((MainActivity)getActivity()).getTabFragmentB();
MyFragmentB fragmentB = (MyFragmentB)getActivity()
.getSupportFragmentManager()
.findFragmentByTag(TabOfFragmentB);
fragmentB.b_updateText(textPassToB);
Toast.makeText(getActivity(),
"text sent to Fragment B:\n " + TabOfFragmentB,
Toast.LENGTH_LONG).show();
}};
}
// Second Fragment, where we'll get data
public class MyFragmentB extends Fragment {
TextView b_received;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myFragmentView = inflater.inflate(R.layout.fragment_b, container, false);
b_received = (TextView)myFragmentView.findViewById(R.id.b_received);
String myTag = getTag();
((MainActivity)getActivity()).setTabFragmentB(myTag);
Toast.makeText(getActivity(),
"MyFragmentB.onCreateView(): " + myTag,
Toast.LENGTH_LONG).show();
return myFragmentView;
}
public void b_updateText(String t){
b_received.setText(t);
}
}
I am trying to call a Fragment's public Method from its Parent View and I followed this, this and these questions on Stack. However the method I need is not avilable when I try to call it from the Parent view.
Here's my code:
Fragment:
public class FirstTabView extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_first_tab_view, container, false);
return rootView;
}
//The method I need to call
public void PostStuff(String caption){
}
}
Parent View: (Which contains the Main View Pager)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_view);
pager = (ViewPager) findViewById(R.id.mainpager);
mAdapter = new HomePagerAdapter(getSupportFragmentManager());
pager.setAdapter(mAdapter);
pager.setOffscreenPageLimit(4);
//Getting the Fragment's Tag. o is the Pager's Fragment index. i.e.: The first fragment
String fragment_tag = "android:switcher:" + pager.getId() + ":" + 0;
FirstTabView fragment = (FirstTabView) getSupportFragmentManager().findFragmentByTag(fragment_tag);
fragment. <This is where I need to call the method and pass the parameter> ();
//The method however doesn't appear when I try to reference it
}
Am I calling the Fragment's method correctly from my Parent View? What am I doing wrong?
EDIT
This is the adapter that I used to push the Fragments:
public class HomePagerAdapter extends FragmentPagerAdapter {
public HomePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new FirstTabView();
case 1:
return new SecondTabView();
case 2:
return new ThirdTabView();
case 3:
return new FourthTabView();
case 4:
return new FifthTabView();
}
return null;
}
#Override
public int getCount() {
// get item count - equal to number of tabs
return 5;
}
}
You can save a reference of the FirstTabView to your MainActivity, then call the method of the fragment.
FirstTabView
public class FirstTabView extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_first_tab_view, container, false);
return rootView;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
//set this fragment to your MainActivity
MainActivity mainActivity = (MainActivity)getActivity();
mainActivity.setFirstTabView(this);
}
//The method I need to call
public void PostStuff(String caption){
}
}
MainActivity
private FirstTabView firstTabView;
#Override
public void onCreate(Bundle bundle) {
........
if(firstTabView != null) {
firstTabView.PostStuff("Hello World");
}
}
public void setFirstTabView(FirstTabView firstTabView){
this.firstTabView = firstTabView
}
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?