我在开发Android应用程序时遇到了一个奇怪的问题。我实现了caldroid(一种流行的第三方日历),当用户选择日期时,它应该将此信息传递给新活动,然后启动它。信息格式正确,但活动无法启动。以下错误:
java.lang.IllegalStateException: Fragment MySchedule{4271d8a8} not attached to Activity
at android.support.v4.app.Fragment.startActivity(Fragment.java:878)
at salespharma.sptouchexpress.modules.salescalls.MySchedule.mySchedule(MySchedule.java:149)
at salespharma.sptouchexpress.modules.salescalls.MySchedule$2.onLongClickDate(MySchedule.java:131)
at com.roomorama.caldroid.CaldroidFragment$2.onItemLongClick(CaldroidFragment.java:895)
at android.widget.AbsListView.performLongPress(AbsListView.java:2979)
at android.widget.AbsListView$CheckForLongPress.run(AbsListView.java:2928)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
活动在此方法之外启动:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
CaldroidFragment caldroidFragment = new CaldroidFragment();
Bundle args = new Bundle();
Calendar cal = Calendar.getInstance();
args.putInt(CaldroidFragment.MONTH, cal.get(Calendar.MONTH) + 1);
args.putInt(CaldroidFragment.YEAR, cal.get(Calendar.YEAR));
caldroidFragment.setArguments(args);
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, caldroidFragment);
fragmentTransaction.commit();
final CaldroidListener listener = new CaldroidListener() {
@Override
public void onSelectDate(Date date, View view) {
month = (String) android.text.format.DateFormat.format("MM", date); //06
year = (String) android.text.format.DateFormat.format("yyyy", date); //2013
day = (String) android.text.format.DateFormat.format("dd", date); //20
Log.d("Given_Date", month + "/" +day + "/" + year);
}
@Override
public void onChangeMonth(int month, int year) {
// String text = "month: " + month + " year: " + year;
}
@Override
public void onLongClickDate(Date date, View view) {
month = (String) android.text.format.DateFormat.format("MM", date); //06
year = (String) android.text.format.DateFormat.format("yyyy", date); //2013
day = (String) android.text.format.DateFormat.format("dd", date); //20
mySchedule();
//Activity opens up fine outside of this method.
}
@Override
public void onCaldroidViewCreated() {
}
};
caldroidFragment.setCaldroidListener(listener);
}
public void mySchedule() {
Intent in = new Intent(context, MySalesCalls.class);
in.putExtra("Given_Date", month + "/" +day + "/" + year);
startActivity(in);
}
我不知道为什么它会抛出这个错误,因为我总是使用这种方法开始我的活动。 任何帮助表示赞赏。