Android Studio - FragmentPageAdapter - android

I'm having a problem with calling me fragments when using FragmentPageAdapter in a swipe-function
The problem in images:
http://www.afbeeldinguploaden.nl/photo/view/90082/sxP3Rre
http://www.afbeeldinguploaden.nl/photo/view/90083/EefjiS4q
Anyone can help me with this or what can I do?

Your HomeFragment needs to extend android.support.v4.app.Fragment, so that means you need to import:
import android.support.v4.app.Fragment;

Related

Android Studio: ActionBar.TabListener does not work with AppCompatActivity?

I am trying to upgrade some code from FragmentActivity to AppCompatActivity and ActionBar to android.support.v7.app.ActionBar due to deprecation. I've spent a few hours trying to fix this, but I am sure someone with more experience might be able to solve this very quickly.
So my class looks like this now:
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
//...many other imports here
public class MyClass extends AppCompatActivity implements ActionBar.TabListener{
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
//other code here...
}
My problem is that I am getting an error because Android Studio does not recognize the onTabSelected method signature in the superclass. Changing the tab paramenter to simply "Tab tab" instead of "ActionBar.Tab tab" doesn't help either.
Does anybody have any ideas?
Like i said, it is deprecated.
See: http://developer.android.com/intl/en/reference/android/support/v7/app/ActionBar.TabListener.html
This interface is deprecated. Action bar navigation modes are
deprecated and not supported by inline toolbar action bars. Consider
using other common navigation patterns instead.
And about your problem:
Action bar tabs can be replaced by:
TabLayout from the Android Design Support Library, with or without a
ViewPager; or
a ViewPager and your favorite other tab solution for it, whether that
be PagerTabStrip or any number of third-party tab implementations; or
FragmentTabHost
Check this link: ActionBarActivity and ActionBar.TabListener is deprecated inside Android Tab Fragment ( Eclipse ApI 22 )

Android viewpager in fragment

Dear ladies and gents,
First of all, please do not mark my question down. If you think that my question is too stupid please let me know and i will edit it or remove.
So my actual question is that I have an activity (extends Activity). In that activity's layout I created FrameLayout where I then attach different four fragments(so each of them extends Fragment. So in one of that fragments I would like to implements swipeable tabs(see screenshot).
I know that it is usually done by implementing viewPager and FragmentPagerAdapter, but I can not do this as if I'm calling getSupportFragmentManager in my fragment it gives my an error. If i am using getActivity().getFragmentManager() it gives me error that android.support.v4.app.fragmentmanager cannot be applied to android.app.fragmentmanager. I have to use android.support.v4.app.fragment in my fragment, because otherwise I will not be able to implement my activity's view(described at the beggining).
Any ideas or suggestions would be very appreciated.screenshot
Make sure you are using Fragment class that you extends your fragments comes from support library. You also need to use FragmentActivity to call method getSupportFragmentManager();
On the other hand, viewpager which is in your fragment need to implemented as usual that you can find on internet except getChildSupportFragmentManager();
This one called "nested fragments".
PS: I am not sure but you can also use AppCompatActivity instead of FragmentActivity. FragmentActivity and ActionBarActivity must be deprecated.
Good luck
You can use getChildFragmentManager() when using Fragments inside other Fragments.
New version of Support Library v4 (Android 4.2) resolve this problem. For do this, simply do constructor of your custom FragmentPagerAdapter like this:
public CustomFragmentPagerAdapter(android.support.v4.app.Fragment fragment)
{
super(fragment.getChildFragmentManager());
// write your code here
}
This work because new Android version approve using nested Fragments

imports colliding when creating blank activity with fragment

I am using Eclipse and when I create Blank activity with Fragment it is using
if (savedInstanceState == null) {
getFragmentManager().beginTransaction().add(R.id.container, new placeholder()).commit();
}
in the onCreate method of MainActivity which needs import app.fragment and I need to use loginbutton.setfragment(this) in Placeholder class needs import android.v4.app.Fragment and I cannot import both of them together. somebody please help
It is advised to use Fragment or v4.Fragment thru out the project.
In your case import android.v4.app.Fragment and use getSupportFragmentManager instead of getFragmentManager

Problems with fragment libraries

The problem it solved, in this discussion, I gave the solution of the problem: Solution to the problem
I created a project with Android Studio 1.1.
With support by the API 19 to 22
But now I'm having problems with compatibility libraries:
import android.support.v4.app.Fragment;
import android.app.Fragment;
I created a Navigation Drawer Activity as a base.
I created following a fragment blank.
I simply had to call this new fragment in my Navigation Drawer, or rather in my ActivityMain in onNavigationDrawerItemSelected method, but I have a problem with the libraries.
In my ActivityMain, libraries are: android.support.v4.app.Fragment;
while in the new Fragment Blank I created, the libraries are: import android.app.Fragment;
And of course when I try to interact with objects of type Fragment to maneuver the switch case, I have compatibility problems? between:
import android.app.Fragment;
import android.support.v4.app.Fragment;
What causes all this confusion?
I state that I have done nothing, if not simply create classes.
Where am I wrong?

Error setting Toolbar as ActionBar

I'm trying to replace the ActionBar of my app with a Toolbar but I can't because I can't use setSupportActionBar(). It says "setSupportActionBar() is undefined for the type MainActivity" even if MainActivity extends ActionBarActivity.
I don't know what to do, any help? Do you need me to post my code?
EDIT:
Now the code works but my app crashes on start and it says: "Attempt to invoke virtual method getTitle()". What do I miss?
EDIT2:
Everything works I forgot I had a Fragment and because of that it gave me nullPointer
Now use :
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
setSupportActionBar(toolbar);
XML :
android.support.v7.widget.Toolbar
Extends :
public class MainActivity extends AppCompatActivity
Without code, my only recommendation, is making sure your import statement is as follows:
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
Also make sure in your XML files that you are using
<android.support.v7.widget.Toolbar
As your toolbar tag
import androidx.appcompat.app.AppCompatActivity;
import this class instead of:
import android.support.v7.widget.Toolbar;
or
import android.widget.Toolbar;
The newer version requires the android class

Categories

Resources