使用动作条ClassCastException异常片段的android [英] ClassCastException fragment android using ActionBar
问题描述
我有一个 FragmentActivity
是用来调用的DatePicker
使用 getSupportFragmentManager()
。一切正常,直到我实现了标签使用本教程中的http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/
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/
为了使其工作,我的老MainActivity(用于延长 FragmentActivity
)现在是一个片段(我会放一些code以下)。
In order to make it work, my old MainActivity (used to extend FragmentActivity
) is now a Fragment (I'll put some code below).
有关Eclipse中显示一些错误(下文解释)我不得不改变 getSupportFragmentManager()
按 getFragmentManager()
现在的应用程序时,我尽量挑选日期崩溃。
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.
下面是code:
我实现了TabsPageAdapter就像教程的支持下进口
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;
该MainActivity.java(就像教程)版本
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) {
}
}
在这里,我有我的第一个疑问,如果我进口FragmentTransaction的support.v4版本,我得到了落实ActionBar.TabListener功能(即onTabSelected)错误。他们不承认,它抱怨,有没有落实。
} 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.
该SearchEventsFragment.java这是一个选项卡(以前是在MainActivity)
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);
从这个片段我叫PickDate
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");
}
});
如果我尝试使用 getSupportFragmentManager()
,因为是未定义的类型我得到一个错误View.onClickListener
If I try to use getSupportFragmentManager()
I got an error because is undefined for the type View.onClickListener
最后DatePickerFragment的实现,它返回采摘日期
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);
}
}
}
和日志错误
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).
我已经读了很多类似的问题还有很多答案,建议不要使用支持的版本,和其他人提议用ActionBarSherlock
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
推荐答案
问题是你实现DateListener为碎片不活动。
The problem is you implement DateListener into Fragment not activity.
listener = (DateListener) getActivity();
当你调用getActivity()调用的活动。
when you call getActivity() it calls activity.
您应该实现监听器MainActivity
You should implement the listener to MainActivity
这篇关于使用动作条ClassCastException异常片段的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!