I am trying to learn fragment.I am clicking a fragment class and my app crashes.I have declared it in the manifest..But why it is happening..My Menu class
public
Any Class that extends Activity in your application must be declared in the manifest, however, that does not appear to be your problem. Technically, this does answer your question though ;)
This line:
Caused by: java.lang.ClassCastException: com.example.practise.ListFragmentDemo cannot be cast to android.app.Fragment
Does "ListFragmentDemo" extend Fragment?
Your ListFragmentDemo
extends from android.support.v4.app.Fragment
(the support version of the Fragment class) and not android.app.Fragment
(the version which is inside Android)
Change
import android.support.v4.app.Fragment;
to
import android.app.Fragment;
in the ListFragmentDemo
class
I think the problem is that you're Activity(Main Activity/ Activity that hosts the Fragments) doesn't extend the FragmentActivity
class.
If you're using the Support Library for ActionBar
, the just make sure your Activity class extends the ActionBarActivity
class.
That's why your Fragments can't be cast into the Main Activity.(ClassCastException)
So it should be:
public class MainFragmentDemoActivity extends FragmentActivity .... {
or:
public class MainFragmentDemoActivity extends ActionBarActivity .... {
Open for correction, as always! Regards, Edward Quixote.