My Current Situation
I am working on an existing app that is using CustomFragment derived from android.app.Fragment. I want to add a new functionnality with a ViewPager which will show my CustomFragments. For that I need to use a FragmentPagerAdapter.
Oooh snap !
Here I am, importing android.support.v4.view.ViewPager and android.support.v4.app.FragmentPagerAdapter to achieve my goal. And now the funny part begins :
When I try to use my FragmentPagerAdapter with my CustomFragment, I get the classic error Type mismatch: cannot convert from android.app.Fragment to android.support.v4.app.Fragment
Knowing that I can't change the whole app fragments to android.support.v4.app.Fragment, I searched and found that the android.support.v13.app.FragmentPagerAdapter was made to use native fragments. Great !
Not great. When I try to import the v13 FragmentPagerAdapter, I get the error The import android.support.v13 cannot be resolved. For the record, I checked in SDKManager what support version is installed : v19.0.1
I searched again, because of course I can't be the first one with that problem. I found that you can't use support.v4 and support.v13 in the same app because v4 is included in v13. And for everyone that encoutered my problem, the solution was to use only android.support.v4.app.FragmentPagerAdapter and android.support.v4.app.Fragment
What to do now ?
Since I have to use android.app.Fragment and I can't use only support.v4, I don't know where to go search next. Any help would be highly appreciated !
Well that is simply not possible. I went to use support.v4.app.Fragment.
Related
I'm currently trying to share data between my two fragments with the help of a view model Android ViewModels.
In the this article they use "ViewModelProviders.of(getActivity()).get(SharedViewModel.class);" in the Fragment to get the model.
My Problem is, that when I write the exact same line into my fragment, I get a "Cannot resolve method of(android.app.Activity)" error
model = ViewModelProviders.of(getActivity()).get(UserAccountViewModel.class);
Can some one help me with this ?
ViewModelProviders requires that you use FragmentActivity (or something derived from it), not Activity, as the base class of your activities.
You might also get a similiar error like Cannot resolve method 'of(android.support.v4.app.FragmentActivity)' from ViewModelProviders.of(getActivity()) if you were trying to import androidx in your gradle file when your app is still using android.support.v4
This is different to the question being asked but is in the same ballpark and ranks first in Google when I had the problem, so I'm posting it!
Just cast getActivity() to specific activity. It works for me. For example:
model = ViewModelProviders.of((MainActivity)getActivity()).get(UserAccountViewModel.class);
I'm trying to upgrade to MvvmCross 5.2 and get a syntax error on the MvxCachingFragmentCompatActivity. Has this class disappeared?
As explained in the blog for 5.2: https://www.mvvmcross.com/mvvmcross-52-release/
We've refactored the Android presenter and there is no need anymore for a special Activity like MvxCachingFragmentCompatActivity. The adviced Activity to use now is the MvxAppCompatActivity.
.setCustomAnimations(R.animator.in_animation, R.animator.out_animation)
the following throws "Expected respource type of anim"
getSupportFragmentManager()
.beginTransaction()
.setCustomAnimations(R.animator.in_animation, R.animator.out_animation)
.replace(R.id.a_fragment, SingleAnswerFragment.newInstance())
.addToBackStack(null)
.commit();
I did check this stack over flow thread Android Studio's "expected resource of type" checks? and I did see where in this case I might use #Animator Res annotation, but i'm not sure how I would use that in the middle of my method chaining as shown above
https://stackoverflow.com/a/18511350/5596604 Here I see a possible solution but have no idea what he means in his steps where he, (Imported the NineOldAndroids library into the support-v4 Library
Imported the support-v4-nineoldandroids library into my project.) The github link for that support library for NineOldAndroids, states in the configuration part " remove Google's support v4 library from your classpath."
You are providing R.animator resources (#AnimatorRes) to the setCustomAnimations() method but it expects R.anim resource type (#AnimRes). If you switch to some animations which are referenced by R.anim it should compile and work.
but i'm not sure how I would use that in the middle of my method chaining as shown above
I'm not sure what you mean by this. Maybe this could shed some light on the matter. Also here you can find a list of all the Android annotations with short descriptions.
You can use setCustomAnimations with Property Animation only with android.app.Fragment. And with android.support.v4.app.Fragment you should use TweenAnimation.
I'm pretty sure that you are using fragment of class android.support.v4.app.Fragment, but not android.app.Fragment. This class is not allowing property animation that you try to use.
If so, change your fragment to android.app.Fragment (change all other accordingly, for example getSupportFragmentManager() to getFragmentManager()), this should help.
I'm trying to edit the ViewPager Source code. However, if I add the file to my project it says on the line mAdapter.registerDataSetObserver(mObserver); that the method registerDataSetObserver is invisible. mAdapter here is a PagerAdapter. If you look at the developer page, PagerAdapter really has no registerDataSetObserver method. But I also checked the PagerAdapter source code in the SDK. It turns out that there is a registerDataSetObserver method in the source.
I'm a bit confused here. Can someone please enlighten me?
viewpager is available in the compatability package (aka support library) .
why not use the one inside there ?
this way , you can also support older android versions (down to API 4 ).
example : the FragmentTabsPager.java file has it being used , inside the path "...\extras\android\support\samples\Support4Demos\src\com\example\android\supportv4\app"
I'm trying to use the SlideMenu library with ActionBarSherlock.
https://github.com/jfeinstein10/SlidingMenu
http://actionbarsherlock.com/
I had not issue installing actionbarsherlock and run the examples provided.
But, can't do the same with the example og the SlideMenu library :(
I can set up the library fine (or so i think) without errors (under eclipse with jre6).
For the slidemenu example, i created the project, and added com_actionbarssherlock.jar as a "referenced libraies".
But i have lots of :
- R cannot resolve erros ;
- can't overrides some methods erros;
- methods from actionbarsherlock are not recognized ; ex : "The method getSupportActionBar() is undefined for the type BaseActivity"
- can't load style from actionbarsherlock erros.
It is as if the project doesn't see the classes from actionbarsherlock.
Thanks for any help.
For the error
"The method getSupportActionBar() is undefined for the type BaseActivity"
Inside the SlidingMenu library, edit the class SlidingFragmentActivity to extend SherlockFragmentActivity (like below). Then clean and rebuild, this method should now be found.
public class SlidingFragmentActivity extends SherlockFragmentActivity implements SlidingActivityBase {
Source
Look add Problems tab to see there are any error ?
Right click your project and check your all Libraries.
If everthing ok, try to clean your project.
There are lots of issues on github with that bug. Mrcb123's solution works, but I suppose it will be good to separate example ABS dependency from library.