i dont know where can i put the code to start a fragment. I have a viewpager with fragments but they dont do nothing. For example:
I have the class fragmentosactivity thats inicialize de fragments:
public class FragmentosActivity extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
super.setContentView(R.layout.fragmentos_layout);
// Paginador
this.inicializaPaginas();
//ActionBar
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("Contoles");
//CirculoProgreso
setProgressBarIndeterminateVisibility(Boolean.FALSE);
}
//Este metodo inicia todos los fragments
private void inicializaPaginas() {
FragmentAdapter adapter =
new FragmentAdapter(getSupportFragmentManager());
adapter.addFragment(new Mapa());
adapter.addFragment(new Cercanos());
ViewPagerAdapter vadapter = new ViewPagerAdapter( this );
ViewPager pager =
(ViewPager)findViewById( R.id.viewpager );
TitlePageIndicator indicator =
(TitlePageIndicator)findViewById( R.id.indicator );
pager.setAdapter( adapter );
pager.setAdapter( vadapter );
indicator.setViewPager( pager );
}
}
Here i call cercanos.class
And in cercanos i have
public class Cercanos extends ListFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
return null;
}
return (LinearLayout) inflater.inflate(R.layout.cercanos, container, false);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] objetos = {"hola","adios"};
setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, objetos));
Parseador.anadedatos();
}
}
But never enter in onCreate method of cercanos, why? I think i dont understand for all the use of fragments.
this is called only in main activity, not in views
public class XXX extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
....
}
}
onCreate is a fragment of the application life-cycle, not class or view life-cycle
You can try to overriding this method:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
And put the onCreate code here.
Anyway, you can take a look at the documentation, because fragments don't have a setContentView, is in the xml layout where you tell what fragment controlls the layout. http://developer.android.com/guide/topics/fundamentals/fragments.html
Hope this helps
Related
As per title, I'm stuck in this absurd problem
I've got a ListFragment, here's the stripped down code:
public class AlarmsListFragment extends ListFragment implements AbsListView.OnItemClickListener {
public ListAdapter mAdapter;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
mAdapter = new AlarmsAdapter(getActivity(), R.layout.alarm_card_item, alarmsList);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_alarms_list, container, false);
mListView = (AbsListView) view.findViewById(android.R.id.list);
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(this);
return view;
}
}
And then there's an activity:
public class MainActivity extends Activity implements AlarmsListFragment.OnAlarmSelectedListener {
ListFragment mAlarmsListFragment;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fm = getFragmentManager();
mAlarmsListFragment = (ListFragment) fm.findFragmentByTag(ALARM_LIST_TAG);
if (mAlarmsListFragment == null)
{
mAlarmsListFragment = new AlarmsListFragment();
getFragmentManager().beginTransaction()
.add(R.id.listContainer, mAlarmsListFragment, ALARM_LIST_TAG)
.commit();
}
}
Later on, onOptionsItemSelected, I've got some code that needs to add an item to the ListAdapter and invalidate the previous List, so I'm calling:
AlarmsAdapter mAdapater = (AlarmsAdapter) mAlarmsListFragment.getListAdapter();
mAdapater.add(a);
mAdapater.notifyDataSetChanged();
Thing is, I get a nullPointerException because getListAdapter() returns null, while getListView() works fine.. what might be causing this bug?
You need to call setListAdapter on your ListFragment,ie:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
mAdapter = new AlarmsAdapter(getActivity(), R.layout.alarm_card_item, alarmsList);
setListAdapter(mAdapter);
}
You do not need to call setAdapter on your ListView. The ListFragment will handle it.
i have a fragment inside a activity, this fragment contains a viewpager, this is the code:
public class RepliesContainerFragment extends Fragment {
#InjectView(R.id.viewPager)
ViewPager pager;
private PagerAdapter adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ReplyActivity replyActivity = (ReplyActivity) getActivity();
List<Replie> replies = getArguments().getParcelableArrayList("replies");
adapter = new PagerAdapter(replyActivity.getSupportFragmentManager(), replies);
setActionBar();
}
private void setActionBar() {
getActivity().getActionBar().setDisplayHomeAsUpEnabled(false);
getActivity().getActionBar().setIcon(getResources().getDrawable(R.drawable.ic_back));
getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
getActivity().getActionBar().setTitle("Respuestas");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.work_containter_replies, container, false);
ButterKnife.inject(this, view);
pager.setAdapter(adapter);
return view;
}
}
Inside the adapter i had instantiate some fragments like this:
public class ReplyFragment extends Fragment {
#InjectView(R.id.tvQuestion)
TextView tvQuestion;
#InjectView(R.id.frameContainter)
FrameLayout replyContainer;
private ReplyView replyView;
private String question;
private String currentReplie;
private int replieType;
private int questionId;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
replieType = getArguments().getInt("replyType");
question = getArguments().getString("question");
questionId = getArguments().getInt("questionId");
currentReplie = getArguments().getString("currentReplie");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.work_reply_fragment, container, false);
ButterKnife.inject(this, view);
initQuestionText();
return view;
}
private void persistReply() {
ReplyActivity replyActivity = (ReplyActivity) getActivity();
if (replyActivity != null) {
//DO SOMETHING
//Notify to adapter from here
}
}
}
}
As you can see, i want to notify to my RepliesContainerFragment and his inside adapter that some event happens and do an action..
How can i do it this?
With broadcastreceivers, with callbacks(Interfaces)..?I am not sure.
The very simple solution for this is to pass a listener instance from your parent fragment to a child fragment. You have to create a listener interface, create an instance of it in your parent fragment, and pass that instance to all your children fragments.
By the way there is a topic regarding this problem in the official tutorial.
Hope this helps.
I think it is easier:
private void persistReply() {
ReplyActivity replyActivity = (ReplyActivity) getActivity();
if (replyActivity != null) {
//DO SOMETHING
RepliesContainerFragment frag = ((RepliesContainerFragment)this.getParentFragment());
frag.getAdapter().notifyDataSetChanged();
}
}
}
and make getter setter for adapter
i wanted to know if someone can help me solve this issue. I have a button setup and when its clicked, its supposed to navigate to another viewpager page. But everytime its clicked, the app crashes returning the error at onClick at fragmenttab2 class. Here is my Main Activity.
public class MainActivity extends SherlockFragmentActivity {
ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));
viewPager.setCurrentItem(1);
}
}
Here is the fragment containing the onClick:
public class FragmentTab1 extends SherlockFragment {
ViewPager viewPager;
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Activity activity = getActivity();
if (activity != null) {
addListenerOnButton();
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragmenttab1, container, false);
return view;
}
public void addListenerOnButton() {
Button nextfrag = (Button) getView().findViewById(R.id.btn_nextfrag);
nextfrag.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
((ViewPager) viewPager.findViewById(R.id.pager)).setCurrentItem(1);
}
});
}
}
The log:
E/AndroidRuntime java.lang.NullPointerException
E/AndroidRuntime com.test.seriessample.FragmentTab1$1.onClick(FragmentTab1.java:58)
your viewPager is probably null. I also see that viewPager itself is a ViewPager and in the onClick you search for another ViewPager within that ViewPager which doesn't seem right.
EDIT:
you should add something like this in your activity
public ViewPager getPager() {
return viewPager;
}
And in the onClick of your Fragment
((MainActivity) getActivity()).getPager().setCurrentItem(1);
I am trying to show a FragmentDialog ( created and shown as a dialog NOT added as content in a view hierarchy) where there is a ViewPager whose content is given by a FragmentPagerAdapter (provides Fragments consisting of an image).
The code works perfect when showing ViewPager + FragmentPagerAdapter from a FragmentActivity, but get the following exception when doing it from a FragmentDialog:
"IllegalArgumentException: No view found for id 0x7f040077 for fragment SimpleFragment..."
Here is my code:
A SherlockFragmentActivity with a button to create and show the dialog.
public class BorrameActivity extends SherlockFragmentActivity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.one_act);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
showTheDialog();
}});
}
private void showTheDialog(){
AchGalleryDialog newFragment = AchGalleryDialog.newInstance(achs);
newFragment.show(getSupportFragmentManager(), "dialog");
}
The FragmentDialog:
public class AchGalleryDialog extends DialogFragment{
public AchGalleryDialog(){
}
public static AchGalleryDialog newInstance(){
AchGalleryDialog f = new AchGalleryDialog();
return f;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_user_result, container);
getDialog().setTitle("Hola tronco");
//content to show in the fragments
int[] images = new int[]{R.drawable.d1, R.drawable.d2, R.drawable.d3};
ViewPager pager = (ViewPager) view.findViewById(R.id.pager);
MyFragmentAdapter adapter = new MyFragmentAdapter(getFragmentManager(),images);
pager.setAdapter(adapter);
return view;
}
}
This is the very simple MyFragmentPagerAdapter,
I put only the getItem() method, and nullPointer checks:
#Override
public Fragment getItem(int position) {
return MySimpleFragment.newInstance(images[position]);
}
And finally SimpleFragment:
public class SimpleFragment extends Fragment{
int id;
public static SimpleAchFragment newInstance(int imgId){
SimpleFragment f = new SimpleFragment();
Bundle args = new Bundle();
args.putLong(ID_BUNDLE, imgId);
f.setArguments(args);
return f;
}
public SimpleAchFragment(){
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.id = getArguments() != null ? getArguments().getInt(ID_BUNDLE) : 0;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.simple_fragment, container, false);
FragmentActivity mAct = getActivity();
ImageView img = (ImageView) v.findViewById(R.id.image);
img.setImageDrawable(mAct.getResources().getDrawable(id));
return v;
}
}
More info, if the content passed to the adapter ( an int array with 3 ints) has length zero, then the adapter doesn't try to create any Fragment so dialogs appears correctly but empty (as expected).
The Exception is thrown at SimpleFragment.onCreateView() at the time of inflating.
The id referenced in the exception (as not found) correspond to ViewPager 's id, with is properly defined in R.layout.simple_fragment.
I have try also to build the Dialog with an AlertDialog.builder and also directly with Dialog() contructor, but get the same behaviour.
Try this:
In class AchGalleryDialog
MyFragmentAdapter adapter = new MyFragmentAdapter(getChildFragmentManager(),images);
instead of
MyFragmentAdapter adapter = new MyFragmentAdapter(getFragmentManager(),images);
Because of this:
http://developer.android.com/about/versions/android-4.2.html#NestedFragments
Hope this will help!
I have the following code for my fragment. I pretty much took this from a basic view pager example and slapped it into a fragment to test for a project I'm working on.
I'm using ACtionBarSherlock, and viewPagerIndicator libraries. The Fragment below contains a viewPager that in this example is supposed to swipe between simple title strings.
I tried the example with the ViewPager and ViewPagerIndicator in an activity, and no problem. But the second I put it into a fragment. BOOM, nothing but a black screen... NO errors, just a black screen...I feel like I must be inflating, or accessing the layout incorrectly or something...
Any ideas?
Here's the code:
public class TestFragment extends SherlockFragment {
//~Constants----------------------------------------------
//~Data Fields--------------------------------------------
//~Constructors--------------------------------------------
#Override
public void onCreate(Bundle savedInstanceState) {
//Setup view
super.onCreate(savedInstanceState);
View view = onCreateView(getLayoutInflater(savedInstanceState), null, savedInstanceState);
ViewPagerAdapter adapter = new ViewPagerAdapter( this.getSherlockActivity() );
ViewPager pager =
(ViewPager)view.findViewById(R.id.free_time_day_pager);
TitlePageIndicator indicator =
(TitlePageIndicator)view.findViewById(R.id.titles);
pager.setAdapter( adapter );
indicator.setViewPager( pager );
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.free_time_fragment_layout, null, false);
return view;
}
//~Methods-------------------------------------------------
}
Dont call onCreateView that is wrong.
onCreateView is an overridden method which is called by the FragmentManger to handle the Fragment lifecycle.
private ViewPager mViewPager;
private TitlePagerIndicator mIndicator;
// Called first
#Override
public void onCreate(Bundle savedInstanceState) {
//Setup view
super.onCreate(savedInstanceState);
}
// Called second
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.free_time_fragment_layout, null, false);
return view;
}
// Called third
#Override
public View onViewCreated(View v){
super.onViewCreated(v);
mViewPager = (ViewPager) v.findViewById(R.id.free_time_day_pager);
mIndicator = (TitlePageIndicator)v.findViewById(R.id.titles);
}
public void onActivityCreated(Bundle bdl){
super.onActivityCreated(bdl);
ViewPagerAdapter adapter = new ViewPagerAdapter( getSherlockActivity() );
mViewPager.setAdapter(adapter);
mIndicator.setViewPager(pager);
}
Also be aware if you are using a FragmentPagerAdapter you require a special method to create the ViewPagerFragment, its a bit tricky but refer to this gist.
Read about Fragment Lifecycles here too.
Cheers
Use this.
compile 'com.android.support:appcompat-v7:23.0.1'
work for me.