Thank you guys for your answers.
I am now editing the question after considering all your solutions guys but I am still getting error. I am using support v4 now.
Here is the result. I keep getting the same error in the switch case block of code
]
These are my current imports
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
Here a new error I am getting after I changed the imports again guys
Remove all Fragment's import from class and import android.support.v4.app.Fragment again
There are two ways we can use fragments
By importing fragment manager and fragments from support v4 library to support lower OS versions devices.
By importing it from standard android API
Here you are trying to use fragment from 1 and FragmentManager from other.
Hence you should use both from anyone I would suggest you to use support v4 library
the problem is the fragment that you are using is a of type android.support.v4.app.Fragment and the method requires the fragment of type android.app.Fragment so you can go to your fragment remove the import statement of Fragment import android.support.v4.app.Fragment and add import android.app.Fragment it should work
the activity you are using the fragment in must extend the FragmentActivity and in place of getFragmentManger you should use getSuppotFragmentManager
public class YourActivity extends FragmentActivity{
}
and the fragments also must be of type android.support.v4.app.Fragment
Hey guys the problem is solved. I forgot to change the imports in the fragments to import android.support.v4.app.Fragment;. I only changed the imports in the main activity. That was so stupid of me that I could not recognize such a simple mistake.
Thank you for your help :)
Related
I am new in learning kotlin and trying to start a android application project with it.
After I created a project, I got stuck in the first line of code I wrote..
So this is my code and error.
I made MainFragment class extend Fragment and passed it's instance to beginTransaction().add() method.
I know that Fragment! is a platform type and it is type of Java class instantiated.
But I think there is no reason Fragment type doesn't math Fragment!.
How do I solve this problem?
+) I also tried MainFragment() as Fragment as FragmentTransaction won't Compile in Kotlin Android Project says, but it didn't work.
+) Also tried newInstance() in companion object
In your fragment MainFragment, use
import android.support.v4.app.Fragment in place of
import android.app.Fragment. and then it should work.
I am trying to get instance of the dynamically currently appearing or showing fragment as follows:
fragment = getSupportFragmentManager().findFragmentById(R.id.fragmentContainer);
but this code returns the following type android.support.v4.app.Fragment
and later I want to remove that fragment which I earlier obtains an instance of as follows:
mFragmentTransaction.remove(fragment);
but this method does not accept this type android.support.v4.app.Fragment
How to solve this issue please.
Your variable mFragmentTransaction should be initialized with the getSupportFragmentManager().beginTransaction() method. Maybe you have used the getFragmentManager() object, witch would be incorrect in your case.
Check your imports. If you see the following import:
import android.app.Fragment;
you will want to change it to the support library version to maintain consistency as follows:
import android.support.v4.app.Fragment;
Check your import statements and be sure you are not mixing "Support" fragments and "Native" fragments. You must use a consistent fragment and many recommend the "Support" version over the "Native" version, in which case you should be using getSupportFragmentManager.
Also, since you wish to remove a fragment, make sure the fragment container is not hard coded in XML. If you intend to replace or remove a fragment, then the initial fragment should be loaded dynamically by your code (you typically will load into a FrameLayout using the id as yourR.id.{frameLayoutId}), else it will not work.
I'm trying to learn how to make tabs in an android app. Reading through tutorials, I had the idea that we should implement the ActionBar.TabListener (to inherit the onTabSelected and other methods ) and getting fragments involved (so I can swipe the fragments), like this:
public class MainActivity extends FragmentActivity implements ActionBar.TabListener
However, Eclipse insists on telling me that ActionBar cannot be resolved as a type, and the suggested solusions is to create the interface, which is not helpful, thank you guys for your help
One possible problem of this, and it will give you an idea just for trying.
Add import android.app.ActionBar;
If you cannot import this package, then I think you have a problem with the Eclipse settings.
Another important thing, know that ActionBar.TabListener is deprecated by the latest API level 21, documentation # ActionBar.TabListener
I know its Bit late but hope someone will find it useful.
try importing ActionBar
import android.app.ActionBar;
if you can import it then try to implent tablistener like this
public class MainActivity extends FragmentActivity implements android.app.ActionBar.TabListener {
worked for me.
Is there any tutorial available on the net about using support library? I am little confused because in my activity I imported all the widgets from a support library and I still get markers that say I need api 11. Do I have to extend FragmentActivity instead of Activity to use it?
EDIT:
My problem was that I extended Activity instead of FragmentActivity. This problem is mentioned here: getLoaderManager().initLoader() doesn't accept 'this' as argument though the class (ListFragment) implements LoaderManager.LoaderCallbacks<Cursor>
Here is a good explanation on how to use the support library.
Regarding FragmentActivity, you have to use it if you are trying to use a Fragment in an Activity.
Also be careful to import the proper classes. An example for a Fragment:
import android.support.v4.app.Fragment;
instead of
import android.app.Fragment;
I am playing around the fragments tutorial found here on section 21. It works fine when my build target is android 4.0 but when i try to change it to make it work with android 2.2, i get Error inflating class fragment on the line setContentView(R.layout.main); on MainActivity class.
I already added the support package, changed classes ListFragment and DetailFragment to extend android.support.v4.app.ListFragment and android.support.v4.app.Fragment respectively. There are no compile errors.
Did I miss something? TIA!
--EDIT---
Made it work. I changed DetailActivity.java and MainActivity.java to extend FragmentActivity instead of just Activity. Everything's okay now. But still don't know why I have to do that.
Changed DetailActivity.java and MainActivity.java to extend FragmentActivity instead of just Activity.
Using the v4 Library APIs