Asked  7 Months ago    Answers:  5   Viewed   21 times

I have a wizard generated app with navigation drawer in android studio 0.8.2

I have created a fragment and added it with newInstance() and I get this error:

com.domain.myapp E/AndroidRuntime? FATAL EXCEPTION: main java.lang.ClassCastException: com.domain.myapp.MainActivity@422fb8f0 must implement OnFragmentInteractionListener

I can't find anywhere how to implement this OnFragmentInteractionListener ?? It cannot be found even in android sdk documentation!


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

public class MainActivity extends Activity
    implements NavigationDrawerFragment.NavigationDrawerCallbacks {

 * Fragment managing the behaviors, interactions and presentation of the navigation drawer.
private NavigationDrawerFragment mNavigationDrawerFragment;

 * Used to store the last screen title. For use in {@link #restoreActionBar()}.
private CharSequence mTitle;

protected void onCreate(Bundle savedInstanceState) {

    mNavigationDrawerFragment = (NavigationDrawerFragment)
    mTitle = getTitle();

    // Set up the drawer.
            (DrawerLayout) findViewById(;

public void onNavigationDrawerItemSelected(int position) {
    // update the main content by replacing fragments
    FragmentManager fragmentManager = getFragmentManager();

    switch (position) {
        case 0: fragmentManager.beginTransaction()
                .replace(, PlaceholderFragment.newInstance(position + 1))
                .commit(); break; 
        case 1: fragmentManager.beginTransaction() 
                .replace(, AboutFragment.newInstance("test1", "test2"))
                .commit(); break; // this crashes the app
        case 2: fragmentManager.beginTransaction()
                .replace(, BrowseQuotesFragment.newInstance("test1", "test2"))
                .commit(); break; // this crashes the app

public void onSectionAttached(int number) {
    switch (number) {
        case 1:
            mTitle = getString(R.string.title_section1);
        case 2:
            mTitle = getString(R.string.title_section2);
        case 3:
            mTitle = getString(R.string.title_section3);

public void restoreActionBar() {
    ActionBar actionBar = getActionBar();

public boolean onCreateOptionsMenu(Menu menu) {
    if (!mNavigationDrawerFragment.isDrawerOpen()) {
        // Only show items in the action bar relevant to this screen
        // if the drawer is not showing. Otherwise, let the drawer
        // decide what to show in the action bar.
        getMenuInflater().inflate(, menu);
        return true;
    return super.onCreateOptionsMenu(menu);

public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == {
        return true;
    return super.onOptionsItemSelected(item);

 * A placeholder fragment containing a simple view.
public static class PlaceholderFragment extends Fragment {
     * The fragment argument representing the section number for this
     * fragment.
    private static final String ARG_SECTION_NUMBER = "section_number";

     * Returns a new instance of this fragment for the given section
     * number.
    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        return fragment;

    public PlaceholderFragment() {

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        return rootView;

    public void onAttach(Activity activity) {
        ((MainActivity) activity).onSectionAttached(




Answers posted here did not help, but the following link did:

Define an Interface

public class HeadlinesFragment extends ListFragment {
    OnHeadlineSelectedListener mCallback;

    // Container Activity must implement this interface
    public interface OnHeadlineSelectedListener {
        public void onArticleSelected(int position);

    public void onAttach(Activity activity) {

        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception
        try {
            mCallback = (OnHeadlineSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnHeadlineSelectedListener");


For example, the following method in the fragment is called when the user clicks on a list item. The fragment uses the callback interface to deliver the event to the parent activity.

public void onListItemClick(ListView l, View v, int position, long id) {
    // Send the event to the host activity

Implement the Interface

For example, the following activity implements the interface from the above example.

public static class MainActivity extends Activity
        implements HeadlinesFragment.OnHeadlineSelectedListener{

    public void onArticleSelected(int position) {
        // The user selected the headline of an article from the HeadlinesFragment
        // Do something here to display that article

Update for API 23: 8/31/2015

Overrided method onAttach(Activity activity) is now deprecated in, code should be upgraded to onAttach(Context context)

public void onAttach(Context context) {

public void onStart() {
    try {
        mListener = (OnFragmentInteractionListener) getActivity();
    } catch (ClassCastException e) {
        throw new ClassCastException(getActivity().toString()
                + " must implement OnFragmentInteractionListener");
Tuesday, June 1, 2021
answered 7 Months ago

You'd need to multiplex io operations into an event based interface(select/poll), so you can leverage the OS to do the waiting, while still being able to schedule other fibers. select/poll have a timeout argument - for fibers that want to sleep, you can create a priority queue that uses that option of select/poll to emulate a sleep call.

Trying to serve fibers that does blocking operations (call read/write/sleep etc). directly won't work unless you schedule each fiber in a native thread - which kind of beats the purpose.

See for a working implementation.

Tuesday, August 10, 2021
answered 4 Months ago

To implement an overloaded function, write all the overload call signatures you want to be visible first, followed by an implementation signature that is a superset of all the overload signatures. Example:

class Thing implements IThing {
    // Implement the name and age fields
    name: string;
    age: number;

    // Overload signature #1
    sayHello(name: string): string;
    // Overload signature #2
    sayHello(age: number): number;
    // Implementation signature, not visible to external callers
    sayHello(arg: any): any {
        if(typeof arg === 'string') {
        } else if(typeof arg === 'number') {
            return this.age;
        } else {
            throw new Error('sayHello can only take string or a number');

var x = new Thing();
var n = x.sayHello('world'); // n: string
var a = x.sayHello(42); // a: number
var e = x.sayHello(false); // error: false is not string or number
Monday, September 27, 2021
answered 3 Months ago

Recently the is deprecated

It is not deprecated at the present time. For example, it is not marked as deprecated in the documentation.

'FragmentStatePagerAdapter(' is deprecated

The single-parameter FragmentStatePagerAdapter constructor is deprecated. However, if you read the documentation for that constructor, you will find:

This constructor is deprecated. use FragmentStatePagerAdapter(FragmentManager, int) with BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT

So, replace FragmentStatePagerAdapter(fm) with FragmentStatePagerAdapter(fm, FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT), to retain the functionality from the original one-parameter constructor.

Wednesday, October 6, 2021
Deepanshu Goyal
answered 2 Months ago
public static final int SERVICES_ALL = 1;
public static final int SERVICES_FEATURED = 2;
public static final int SERVICES_NEW = 3;

should be

public static final int SERVICES_ALL = 0;
public static final int SERVICES_FEATURED = 1;
public static final int SERVICES_NEW = 2;

getItem is 0-based-index, which means that in your code, the case 0 of your Adapter never matches one, and in that case you are returning a null fragment causing the exception

Tuesday, October 12, 2021
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :