0

I had a FragmentActivity that used to call a DatePicker using getSupportFragmentManager(). Everything was ok, until I implemented "tabs" using this tutorial http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/

In order to make it work, my old MainActivity (used to extend FragmentActivity) is now a Fragment (I'll put some code below).

For some errors shown on Eclipse (explained below) I had to change the getSupportFragmentManager() by getFragmentManager() and now the app crashes when I try to pick the date.

Here is the code :

I implemented the TabsPageAdapter just like the tutorial, with the support imports

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

The MainActivity.java (just like tutorial) version

import android.app.ActionBar;
import android.app.ActionBar.Tab;

**import android.app.FragmentTransaction;**
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;


public class MainActivity extends FragmentActivity implements ActionBar.TabListener{

private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Latest Events", "Search Events", "News Feed" };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Initilization
    viewPager = (ViewPager) findViewById(R.id.pager);
    actionBar = getActionBar();
    mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

    viewPager.setAdapter(mAdapter);
    actionBar.setHomeButtonEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        

    // Adding Tabs
    for (String tab_name : tabs) {
        actionBar.addTab(actionBar.newTab().setText(tab_name)
                .setTabListener(this));
    }

    /**
     * on swiping the viewpager make respective tab selected
     * */
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            // on changing the page
            // make respected tab selected
            actionBar.setSelectedNavigationItem(position);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    });

}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    // on tab selected
    // show respected fragment view
    viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}

} Here I have my first doubt if I imported the support.v4 version of FragmentTransaction I got an error implementing the ActionBar.TabListener functions (ie onTabSelected). They are not recognised, it complains that there not implemented.

The SearchEventsFragment.java which is one of the tabs (used to be the MainActivity)

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.util.Log;


public class SearchEventsFragment extends Fragment implements DatePickerFragment.DateListener{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  //super.onCreateView(inflater, container, savedInstanceState);
  View rootview = inflater.inflate(R.layout.fragment_search_events, container, false);    

From this fragment I call the PickDate

mPickDateFrom.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mCurrentDisplay = mDateFromDisplay;
                DialogFragment picker = new DatePickerFragment();
                picker.show(getFragmentManager(), "datePicker");
            }
        });

If I try to use getSupportFragmentManager() I got an error because is undefined for the type View.onClickListener

Finally the implementation of DatePickerFragment which return the picked date

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.app.Dialog;
import android.support.v4.app.DialogFragment;
import android.widget.DatePicker;

public class DatePickerFragment extends DialogFragment 
                                implements DatePickerDialog.OnDateSetListener {
        DateListener listener;

        public interface DateListener {
            public void returnDate(String date);
        }

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            // Use the current time as the default values for the picker
            final Calendar c = Calendar.getInstance();
            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH);
            int day = c.get(Calendar.DAY_OF_MONTH);
            listener = (DateListener) getActivity();

            // Create a new instance of TimePickerDialog and return it
            return new DatePickerDialog(getActivity(), this, year,month,day);
        }

    @Override
    public void onDateSet(DatePicker view, int year, int month,int day) {
        // Do something with the date chosen by the user
        Calendar c = Calendar.getInstance();
        c.set(year, month, day);

        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
        String formattedDate = sdf.format(c.getTime());
        if (listener != null) 
        {
          listener.returnDate(formattedDate); 

        }   
    }   
}

And the Log Error

E/AndroidRuntime(11267): FATAL EXCEPTION: main
E/AndroidRuntime(11267): java.lang.ClassCastException:com.csn.myapp.MainActivity cannot
                         be cast to com.csn.myapp.DatePickerFragment$DateListener

I know I'm probably messing with the supported version and the new one, but I don't know how to fix it (if I change something another problem arises).

I've read a lot of similar questions and there lots of answers, proposing don't use the supported version, and others proposing to use ActionBarSherlock

Pablo V.
  • 324
  • 2
  • 16

1 Answers1

0

The problem is you implement DateListener into Fragment not activity.

listener = (DateListener) getActivity();

when you call getActivity() it calls activity.

You should implement the listener to MainActivity

Orhan Obut
  • 8,756
  • 5
  • 32
  • 42
  • If I comment that line , the Dialog is deployed, thanks. But, what you mean with implement the "listener" to MainActivity? – Pablo V. Jun 09 '14 at 18:45
  • Finally I deleted the listener and implemented the OnDateSet at the class that show the picker http://stackoverflow.com/a/14751675/2631891 – Pablo V. Jun 09 '14 at 19:59
  • MainActivity extends FragmentActivity implements ActionBar.TabListener, DatePickerDialog.OnDateSetListener that's what I meant by saying implement to activity. – Orhan Obut Jun 10 '14 at 06:16