1
我把這個網站日曆 https://github.com/roomorama/Caldroid的Android重新啓動日曆
成功安裝到片段
但經過我更換片段
一切都將變成空白日曆。
我必須更換以正確顯示月份。
但我改變指定日期的顏色失敗。
而調用格式數據將導致nullpointerexception。
我該如何解決。
活動
public class index_Activity extends FragmentActivity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
fm2 = getSupportFragmentManager();
DSE2 = fm2.beginTransaction();
e_f=fm2.findFragmentById(R.id.main_frame_e);
mRadioButton1.toggle();
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
Log.i("zj", "checkedid="+checkedId);
fm2 = getSupportFragmentManager();
DSE2 = fm2.beginTransaction();
DF2 = fm2.findFragmentById(R.id.main_frame);
if (checkedId == R.id.btn1) {
if (DF2 == null) {
fg_a usermune = new fg_a("nono");
DSE2.add(R.id.main_frame,usermune);
DSE2.commit();
} else if (DF2 != null) {
DSE2.remove(DF2);
fg_a usermune = new fg_a("nono");
DSE2.add(R.id.main_frame,usermune);
DSE2.commit();
}
}else if (checkedId == R.id.btn2) {
if (DF2== null) {
fg_b usermune = new fg_b("nono");
DSE2.add(R.id.main_frame, usermune);
DSE2.commit();
} else if (DF2 != null) {
DSE2.remove(DF2);
fg_b usermune = new fg_b("nono");
DSE2.add(R.id.main_frame,usermune);
DSE2.commit();
}
}else if (checkedId == R.id.btn3) {
///////////////////Open the calendar//
if (DF2== null) {
fg_c usermune = new fg_c("nono");
DSE2.add(R.id.main_frame, usermune);
DSE2.commit();
} else if (DF2 != null) {
DSE2.remove(DF2);
fg_c usermune = new fg_c("nono");
DSE2.add(R.id.main_frame, usermune);
DSE2.commit();
}
////////////////////////////////////////
}else if (checkedId == R.id.btn4) {
page[i]=4;
i++;
startActivity(fg_d_link);
}else if (checkedId == R.id.btn5) {
touch=4;
page[i]=5;
i++;
if (DF2== null) {
fg_e usermune = new fg_e("nono");
DSE2.add(R.id.main_frame, usermune);
DSE2.commit();
} else if (DF2 != null) {
DSE2.remove(DF2);
fg_e usermune = new fg_e("nono");
DSE2.add(R.id.main_frame, usermune);
DSE2.commit();
}
}
mCurrentCheckedRadioLeft = getCurrentCheckedRadioLeft();
}
日曆Frament:
public class fg_c extends Fragment {
SimpleDateFormat formatter;
Date[] setday;
int t;
FragmentManager fm;
FragmentTransaction DSE2;
Fragment DF2;
private DBOpenHelper dbHelper;
private ArrayList<Site> sites;
private final String title1;
int page_open=0;
public fg_c(String string) {
this.title1 = string;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View fragmentView = inflater.inflate(R.layout.caldroid_main,container, false);
formatter = new SimpleDateFormat("dd MMM yyyy");
// Setup caldroid fragment
// **** If you want normal CaldroidFragment, use below line ****
caldroidFragment = new CaldroidFragment();
// //////////////////////////////////////////////////////////////////////
// **** This is to show customized fragment. If you want customized
// version, uncomment below line ****
// caldroidFragment = new CaldroidSampleCustomFragment();
// Setup arguments
// If Activity is created after rotation
if (savedInstanceState != null) {
caldroidFragment.restoreStatesFromKey(savedInstanceState,
"CALDROID_SAVED_STATE");
}
// If activity is created from fresh
else {
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));
args.putBoolean(CaldroidFragment.ENABLE_SWIPE, true);
args.putBoolean(CaldroidFragment.SIX_WEEKS_IN_CALENDAR, true);
caldroidFragment.setArguments(args);
}
setCustomResourceForDates();
// Attach to the activity
android.support.v4.app.FragmentTransaction t = getActivity().getSupportFragmentManager().beginTransaction();
t.replace(R.id.calendar1, caldroidFragment);
t.commit();
// Setup listener
final CaldroidListener listener = new CaldroidListener() {
@Override
public void onSelectDate(Date date, View view) {
//nullpointerexception
Toast.makeText(getActivity(), formatter.format(date),
Toast.LENGTH_SHORT).show();
//
}
@Override
public void onChangeMonth(int month, int year) {
String text = "month: " + month + " year: " + year;
Toast.makeText(getActivity(), text,
Toast.LENGTH_SHORT).show();
}
@Override
public void onLongClickDate(Date date, View view) {
Toast.makeText(getActivity(),
"Long click " + formatter.format(date),
Toast.LENGTH_SHORT).show();
}
@Override
public void onCaldroidViewCreated() {
if (caldroidFragment.getLeftArrowButton() != null) {
Toast.makeText(getActivity(),
"Caldroid view is created", Toast.LENGTH_SHORT)
.show();
}
}
};
// Setup Caldroid
caldroidFragment.setCaldroidListener(listener);
final TextView textView = (TextView) fragmentView.findViewById(R.id.textview);
return fragmentView;
}
private boolean undo = false;
private CaldroidFragment caldroidFragment;
private CaldroidFragment dialogCaldroidFragment;
private void setCustomResourceForDates() {
Calendar cal = Calendar.getInstance();
for(int i=0;i<t;i++){
cal.set(Integer.valueOf(year[i]).intValue(),Integer.valueOf(mon[i]).intValue(),Integer.valueOf(day[i]).intValue() , 0, 0, 0);
setday[i] = cal.getTime();
}
for (int j=0;j<t;j++){
caldroidFragment.setBackgroundResourceForDate(R.color.green,
setday[j]);
caldroidFragment.setTextColorForDate(R.color.white, setday[j]);
}
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
if (caldroidFragment != null) {
caldroidFragment.saveStatesToKey(outState, "CALDROID_SAVED_STATE");
}
if (dialogCaldroidFragment != null) {
dialogCaldroidFragment.saveStatesToKey(outState,
"DIALOG_CALDROID_SAVED_STATE");
}
}