使用ActionBar的ClassCastException片段android

时间:2014-06-06 23:17:56

标签: android android-fragments

我曾FragmentActivity使用DatePicker拨打getSupportFragmentManager()。一切都很好,直到我实施了#34;标签"使用本教程http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/

为了使其工作,我的旧MainActivity(用于扩展FragmentActivity)现在是一个片段(我将在下面放置一些代码)。

对于Eclipse上显示的一些错误(如下所述),我不得不将getSupportFragmentManager()更改为getFragmentManager(),现在当我尝试选择日期时应用程序崩溃了。

以下是代码:

我实现了TabsPageAdapter,就像教程一样,支持导入

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

MainActivity.java(就像教程)版本

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) {
}

} 在这里,我首先怀疑是否导入了Frag.Tra4的support.v4版本我实现了ActionBar.TabListener函数(即onTabSelected)时出错。它们不被承认,它抱怨没有实施。

SearchEventsFragment.java是其中一个标签(曾经是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);    

从这个片段我调用PickDate

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

如果我尝试使用getSupportFragmentManager()我收到错误,因为未定义类型View.onClickListener

最后执行返回选择日期的DatePickerFragment

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); 

        }   
    }   
}

日志错误

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

我知道我可能正在搞乱支持的版本和新版本,但我不知道如何解决它(如果我改变了其他问题)。

我已经阅读了很多类似的问题并且有很多答案,建议不要使用支持的版本,其他人建议使用ActionBarSherlock

1 个答案:

答案 0 :(得分:0)

问题是你将DateListener实现为Fragment not activity。

listener = (DateListener) getActivity();

当你调用getActivity()时,它会调用activity。

您应该实现对MainActivity的侦听器