我试图在日历项目的日历项目的onclicklistener上添加eventactivity,但我得到空指针异常。
主要Activity.java
public class MainActivity extends Activity {
public GregorianCalendar month, itemmonth;// calendar instances.
public CalendarAdapter adapter;// adapter instance
public Handler handler;// for grabbing some event values for showing the dot
// marker.
public ArrayList<String> items; // container to store calendar items which
// needs showing the event marker
ArrayList<String> event;
LinearLayout rLayout;
ArrayList<String> date;
ArrayList<String> desc;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Locale.setDefault(Locale.US);
rLayout = (LinearLayout) findViewById(R.id.text);
month = (GregorianCalendar) GregorianCalendar.getInstance();
itemmonth = (GregorianCalendar) month.clone();
items = new ArrayList<String>();
adapter = new CalendarAdapter(this, month);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(adapter);
handler = new Handler();
handler.post(calendarUpdater);
TextView title = (TextView) findViewById(R.id.title);
title.setText(android.text.format.DateFormat.format("MMMM yyyy", month));
RelativeLayout previous = (RelativeLayout) findViewById(R.id.previous);
previous.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setPreviousMonth();
refreshCalendar();
}
});
RelativeLayout next = (RelativeLayout) findViewById(R.id.next);
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setNextMonth();
refreshCalendar();
}
});
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// removing the previous view if added
if (((LinearLayout) rLayout).getChildCount() > 0) {
((LinearLayout) rLayout).removeAllViews();
}
desc = new ArrayList<String>();
date = new ArrayList<String>();
((CalendarAdapter) parent.getAdapter()).setSelected(v);
String selectedGridDate = CalendarAdapter.dayString
.get(position);
String[] separatedTime = selectedGridDate.split("-");
String gridvalueString = separatedTime[2].replaceFirst("^0*",
"");// taking last part of date. ie; 2 from 2012-12-02.
int gridvalue = Integer.parseInt(gridvalueString);
// navigate to next or previous month on clicking offdays.
if ((gridvalue > 10) && (position < 8)) {
setPreviousMonth();
refreshCalendar();
} else if ((gridvalue < 7) && (position > 28)) {
setNextMonth();
refreshCalendar();
}
((CalendarAdapter) parent.getAdapter()).setSelected(v);
for (int i = 0; i < Utility.startDates.size(); i++) {
if (Utility.startDates.get(i).equals(selectedGridDate)) {
desc.add(Utility.nameOfEvent.get(i));
}
}
if (desc.size() > 0) {
for (int i = 0; i < desc.size(); i++) {
TextView rowTextView = new TextView(MainActivity.this);
// set some properties of rowTextView or something
rowTextView.setText("Event:" + desc.get(i));
rowTextView.setTextColor(Color.BLACK);
// add the textview to the linearlayout
rLayout.addView(rowTextView);
}
}
desc = null;
}
});
}
protected void setNextMonth() {
if (month.get(GregorianCalendar.MONTH) == month
.getActualMaximum(GregorianCalendar.MONTH)) {
month.set((month.get(GregorianCalendar.YEAR) + 1),
month.getActualMinimum(GregorianCalendar.MONTH), 1);
} else {
month.set(GregorianCalendar.MONTH,
month.get(GregorianCalendar.MONTH) + 1);
}
}
protected void setPreviousMonth() {
if (month.get(GregorianCalendar.MONTH) == month
.getActualMinimum(GregorianCalendar.MONTH)) {
month.set((month.get(GregorianCalendar.YEAR) - 1),
month.getActualMaximum(GregorianCalendar.MONTH), 1);
} else {
month.set(GregorianCalendar.MONTH,
month.get(GregorianCalendar.MONTH) - 1);
}
}
protected void showToast(String string) {
Toast.makeText(this, string, Toast.LENGTH_SHORT).show();
}
public void refreshCalendar() {
TextView title = (TextView) findViewById(R.id.title);
adapter.refreshDays();
adapter.notifyDataSetChanged();
handler.post(calendarUpdater); // generate some calendar items
title.setText(android.text.format.DateFormat.format("MMMM yyyy", month));
}
public Runnable calendarUpdater = new Runnable() {
@Override
public void run() {
items.clear();
// Print dates of the current week
DateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
String itemvalue;
event = Utility.readCalendarEvent(MainActivity.this);
Log.d("=====Event====", event.toString());
Log.d("=====Date ARRAY====", Utility.startDates.toString());
for (int i = 0; i < Utility.startDates.size(); i++) {
itemvalue = df.format(itemmonth.getTime());
itemmonth.add(GregorianCalendar.DATE, 1);
items.add(Utility.startDates.get(i).toString());
}
adapter.setItems(items);
adapter.notifyDataSetChanged();
}
};
}
AddEventActivity.java
public class AddEventActivity extends AppCompatActivity implements View.OnClickListener,AdapterView.OnItemSelectedListener {
private Context mContext;
private String provinceIdSend;
Spinner sp_am,sp_hh,sp_mm,sp_amed,sp_province,sp_hhed,sp_mmed,sp_states;
private RelativeLayout main;
private String stateId,selectedProvince,cityIdSend,cityId,selectedState,stateIdSend;
private ArrayList<StatesBean> statesBeanArrayList;
private ArrayList<ProvinceBean> provinceBeanArrayList;
private int countclickedState = 0;
private TextView tv_province;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_event);
mContext = AddEventActivity.this;
new MakeRequestForGetStates().execute();
new MakeRequestForGetProvinces(stateId).execute();
sp_am = (Spinner) findViewById(R.id.sp_am);
sp_hh = (Spinner) findViewById(R.id.sp_hh);
sp_mm = (Spinner) findViewById(R.id.sp_mm);
sp_amed = (Spinner) findViewById(R.id.sp_amed);
sp_mmed = (Spinner) findViewById(R.id.sp_mmed);
sp_hhed = (Spinner) findViewById(R.id.sp_hhed);
sp_province = (Spinner) findViewById(R.id.sp_province);
sp_states= (Spinner) findViewById(R.id.sp_states);
tv_province= (TextView) findViewById(R.id.tv_province);
sp_am.setOnItemSelectedListener(this);
sp_hh.setOnItemSelectedListener(this);
sp_mm.setOnItemSelectedListener(this);
sp_amed.setOnItemSelectedListener(this);
sp_mmed.setOnItemSelectedListener(this);
sp_hhed.setOnItemSelectedListener(this);
sp_province.setOnItemSelectedListener(this);
sp_states.setOnItemSelectedListener(this);
List<String> list_hh = new ArrayList<String>();
list_hh.add("00");
list_hh.add("01");
list_hh.add("02");
list_hh.add("03");
list_hh.add("04");
list_hh.add("05");
list_hh.add("06");
list_hh.add("07");
list_hh.add("08");
list_hh.add("09");
list_hh.add("10");
list_hh.add("11");
list_hh.add("12");
list_hh.add("HH");
List<String> list_mm = new ArrayList<String>();
list_mm.add("00");
list_mm.add("01");
list_mm.add("02");
list_mm.add("03");
list_mm.add("04");
list_mm.add("05");
list_mm.add("06");
list_mm.add("07");
list_mm.add("08");
list_mm.add("09");
list_mm.add("10");
list_mm.add("11");
list_mm.add("12");
list_mm.add("13");
list_mm.add("14");
list_mm.add("15");
list_mm.add("16");
list_mm.add("17");
list_mm.add("18");
list_mm.add("19");
list_mm.add("20");
list_mm.add("21");
list_mm.add("22");
list_mm.add("23");
list_mm.add("24");
list_mm.add("25");
list_mm.add("26");
list_mm.add("27");
list_mm.add("28");
list_mm.add("29");
list_mm.add("30");
list_mm.add("31");
list_mm.add("32");
list_mm.add("33");
list_mm.add("34");
list_mm.add("35");
list_mm.add("36");
list_mm.add("37");
list_mm.add("38");
list_mm.add("39");
list_mm.add("40");
list_mm.add("41");
list_mm.add("42");
list_mm.add("43");
list_mm.add("44");
list_mm.add("45");
list_mm.add("46");
list_mm.add("47");
list_mm.add("48");
list_mm.add("49");
list_mm.add("50");
list_mm.add("51");
list_mm.add("52");
list_mm.add("53");
list_mm.add("54");
list_mm.add("55");
list_mm.add("56");
list_mm.add("57");
list_mm.add("58");
list_mm.add("59");
list_mm.add("MM");
List<String> list_am = new ArrayList<String>();
list_am.add("AM");
list_am.add("PM");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>
(this, android.R.layout.simple_spinner_item,list_hh);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ArrayAdapter<String> dataAdapter_mm = new ArrayAdapter<String>
(this, android.R.layout.simple_spinner_item,list_mm);
dataAdapter_mm.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ArrayAdapter<String> dataAdapter_am = new ArrayAdapter<String>
(this, android.R.layout.simple_spinner_item,list_am);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp_am.setAdapter(dataAdapter_am);
sp_hh.setAdapter(dataAdapter);
sp_hhed.setAdapter(dataAdapter);
sp_mm.setAdapter(dataAdapter_mm);
sp_amed.setAdapter(dataAdapter_am);
sp_mmed.setAdapter(dataAdapter_mm);
// Spinner item selection Listener
// Button click Listener
}
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (parent.getId()) {
case R.id.sp_states:
String idToSendForCities = statesBeanArrayList.get(position).getStateID();
stateIdSend = idToSendForCities;
if (countclickedState == 1) {
new MakeRequestForGetProvinces(idToSendForCities).execute();
}
countclickedState=1;
selectedState = sp_states.getSelectedItem().toString();
break;
case R.id.sp_province:
cityIdSend = provinceBeanArrayList.get(position).getCityID();
selectedProvince = sp_province.getSelectedItem().toString();
break;
default:
break;
}
}
@Override
public void onClick(View v) {
}
private class MakeRequestForGetStates extends AsyncTask<String, Void, String> {
private String response;
private MakeServiceClass makeServiceClass = new MakeServiceClass();
@Override
protected void onPreExecute() {
}
@Override
protected String doInBackground(String... params) {
try {
HashMap<String, String> parms = new HashMap<String, String>();
response = makeServiceClass.makeServiceConnectionPost(Const.STATE_DETAILS_URL, parms);
Log.e("response states", response);
} catch (Exception ex) {
ex.printStackTrace();
}
return response;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(response);
if (response != null) {
try {
JSONObject mainObject = new JSONObject(response);
if (mainObject.has("Status")) {
String Status = mainObject.getString("Status");
if (Status.equalsIgnoreCase("200")) {
if (mainObject.has("Data")) {
JSONArray datArray = mainObject.getJSONArray("Data");
statesBeanArrayList = new ArrayList<>();
if (datArray.length() > 0) {
for (int i = 0; i < datArray.length(); i++) {
StatesBean statesBean = new StatesBean();
JSONObject internalDataObject = datArray.getJSONObject(i);
if (internalDataObject.has("StateID")) {
statesBean.setStateID(internalDataObject.getString("StateID"));
}
if (internalDataObject.has("Title")) {
statesBean.setTitle(internalDataObject.getString("Title"));
}
statesBeanArrayList.add(statesBean);
}
setStatesSpinnerdata();
} else {
showMessage("No occupations found for your profile.");
}
}
} else {
showMessage("Server error, Please try again");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
private class MakeRequestForGetProvinces extends AsyncTask<String, Void, String> {
private String response, stateIdNew;
private MakeServiceClass makeServiceClass = new MakeServiceClass();
MakeRequestForGetProvinces(String id) {
this.stateIdNew = id;
}
@Override
protected void onPreExecute() {
}
@Override
protected String doInBackground(String... params) {
try {
HashMap<String, String> parms = new HashMap<String, String>();
parms.put("StateID", stateIdNew);
response = makeServiceClass.makeServiceConnectionPost(Const.GETCITIES_FORSTATE_URL, parms);
Log.e("response cities", response);
} catch (Exception ex) {
ex.printStackTrace();
}
return response;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(response);
if (response != null) {
try {
JSONObject mainObject = new JSONObject(response);
if (mainObject.has("Status")) {
String Status = mainObject.getString("Status");
if (Status.equalsIgnoreCase("200")) {
if (mainObject.has("Data")) {
JSONArray datArray = mainObject.getJSONArray("Data");
provinceBeanArrayList = new ArrayList<>();
if (datArray.length() > 0) {
for (int i = 0; i < datArray.length(); i++) {
ProvinceBean provinceBean = new ProvinceBean();
JSONObject internalDataObject = datArray.getJSONObject(i);
if (internalDataObject.has("CityID")) {
provinceBean.setCityID(internalDataObject.getString("CityID"));
}
if (internalDataObject.has("Title")) {
provinceBean.setTitle(internalDataObject.getString("Title"));
}
provinceBeanArrayList.add(provinceBean);
}
setCitiesSpinnerdata();
} else {
showMessage("No province found for your profile.");
}
}
} else {
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
private void setStatesSpinnerdata() {
String stateName = "";
for (int i = 0; i < statesBeanArrayList.size(); i++) {
if (statesBeanArrayList.get(i).getStateID().equalsIgnoreCase(stateId)) {
stateName = statesBeanArrayList.get(i).getTitle();
break;
}
}
stateIdSend = stateId;
ArrayList<String> allData = new ArrayList<>();
if (!stateName.equalsIgnoreCase("")) {
allData.add(0, stateName);
selectedState = stateName;
} else {
allData.add(0, "Select State");
selectedState = "Select State";
}
for (int i = 1; i < statesBeanArrayList.size(); i++) {
allData.add(statesBeanArrayList.get(i).getTitle());
}
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_spinner_item, allData);
// Drop down followup_details_layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
sp_states.setAdapter(dataAdapter);
}
private void setCitiesSpinnerdata() {
String cityName = "";
for (int i = 0; i < provinceBeanArrayList.size(); i++) {
if (provinceBeanArrayList.get(i).getCityID().equalsIgnoreCase(cityId)) {
cityName = provinceBeanArrayList.get(i).getTitle();
break;
}
}
cityIdSend = cityId;
ArrayList<String> allData = new ArrayList<>();
if (!cityName.equalsIgnoreCase("")) {
allData.add(0, cityName);
selectedProvince = cityName;
} else {
allData.add(0, "Select Province");
selectedProvince = "Select Province";
}
for (int i = 1; i < provinceBeanArrayList.size(); i++) {
allData.add(provinceBeanArrayList.get(i).getTitle());
}
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_spinner_item, allData);
// Drop down followup_details_layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
sp_province.setAdapter(dataAdapter);
}
private void showMessage(String message) {
Snackbar snackbar = Snackbar
.make(main, message, Snackbar.LENGTH_LONG);
View snackbarView = snackbar.getView();
snackbarView.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
snackbar.show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
日历adapter.java
public class CalendarAdapter extends BaseAdapter {
private Context mContext;
Fragment fragment;
private java.util.Calendar month;
public GregorianCalendar pmonth; // calendar instance for previous month
/**
* calendar instance for previous month for getting complete view
*/
public GregorianCalendar pmonthmaxset;
private GregorianCalendar selectedDate;
int firstDay;
int maxWeeknumber;
int maxP;
int calMaxP;
int lastWeekDay;
int leftDays;
int mnthlength;
String itemvalue, curentDateString;
DateFormat df;
private ArrayList<String> items;
public static List<String> dayString;
private View previousView;
public CalendarAdapter(Context c, GregorianCalendar monthCalendar) {
CalendarAdapter.dayString = new ArrayList<String>();
Locale.setDefault(Locale.US);
month = monthCalendar;
selectedDate = (GregorianCalendar) monthCalendar.clone();
mContext = c;
month.set(GregorianCalendar.DAY_OF_MONTH, 1);
this.items = new ArrayList<String>();
df = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
curentDateString = df.format(selectedDate.getTime());
refreshDays();
}
public void setItems(ArrayList<String> items) {
for (int i = 0; i != items.size(); i++) {
if (items.get(i).length() == 1) {
items.set(i, "0" + items.get(i));
}
}
this.items = items;
}
public int getCount() {
return dayString.size();
}
public Object getItem(int position) {
return dayString.get(position);
}
public long getItemId(int position) {
return 0;
}
// create a new view for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
TextView dayView;
final ViewHolder viewHolder; // view lookup cache stored in tag
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(R.layout.calendar_item, parent, false);
viewHolder.date = (TextView) convertView.findViewById(R.id.date);
viewHolder.date_icon = (ImageView) convertView.findViewById(R.id.date_icon);
viewHolder.lay_cal_item = (LinearLayout) convertView.findViewById(R.id.lay_cal_item);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.lay_cal_item.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mContext.startActivity(new Intent(mContext, AddEventActivity.class));
}
});
dayView = (TextView) v.findViewById(R.id.date);
// separates daystring into parts.
String[] separatedTime = dayString.get(position).split("-");
// taking last part of date. ie; 2 from 2012-12-02
String gridvalue = separatedTime[2].replaceFirst("^0*", "");
// checking whether the day is in current month or not.
if ((Integer.parseInt(gridvalue) > 1) && (position < firstDay)) {
// setting offdays to white color.
dayView.setTextColor(Color.WHITE);
dayView.setClickable(false);
dayView.setFocusable(false);
} else if ((Integer.parseInt(gridvalue) < 7) && (position > 28)) {
dayView.setTextColor(Color.WHITE);
dayView.setClickable(false);
dayView.setFocusable(false);
} else {
// setting curent month's days in blue color.
dayView.setTextColor(Color.BLUE);
}
if (dayString.get(position).equals(curentDateString)) {
setSelected(v);
previousView = v;
} else {
v.setBackgroundResource(R.drawable.list_item_background);
}
dayView.setText(gridvalue);
// create date string for comparison
String date = dayString.get(position);
if (date.length() == 1) {
date = "0" + date;
}
String monthStr = "" + (month.get(GregorianCalendar.MONTH) + 1);
if (monthStr.length() == 1) {
monthStr = "0" + monthStr;
}
// show icon if date is not empty and it exists in the items array
ImageView iw = (ImageView) v.findViewById(R.id.date_icon);
if (date.length() > 0 && items != null && items.contains(date)) {
iw.setVisibility(View.VISIBLE);
} else {
iw.setVisibility(View.INVISIBLE);
}
return convertView;
}
class ViewHolder {
private TextView date;
private ImageView date_icon;
private LinearLayout lay_cal_item;
}
public View setSelected(View view) {
if (previousView != null) {
previousView.setBackgroundResource(R.drawable.list_item_background);
}
previousView = view;
view.setBackgroundResource(R.drawable.calendar_cel_selectl);
return view;
}
public void refreshDays() {
// clear items
items.clear();
dayString.clear();
Locale.setDefault(Locale.US);
pmonth = (GregorianCalendar) month.clone();
// month start day. ie; sun, mon, etc
firstDay = month.get(GregorianCalendar.DAY_OF_WEEK);
// finding number of weeks in current month.
maxWeeknumber = month.getActualMaximum(GregorianCalendar.WEEK_OF_MONTH);
// allocating maximum row number for the gridview.
mnthlength = maxWeeknumber * 7;
maxP = getMaxP(); // previous month maximum day 31,30....
calMaxP = maxP - (firstDay - 1);// calendar offday starting 24,25 ...
/**
* Calendar instance for getting a complete gridview including the three
* month's (previous,current,next) dates.
*/
pmonthmaxset = (GregorianCalendar) pmonth.clone();
/**
* setting the start date as previous month's required date.
*/
pmonthmaxset.set(GregorianCalendar.DAY_OF_MONTH, calMaxP + 1);
/**
* filling calendar gridview.
*/
for (int n = 0; n < mnthlength; n++) {
itemvalue = df.format(pmonthmaxset.getTime());
pmonthmaxset.add(GregorianCalendar.DATE, 1);
dayString.add(itemvalue);
}
}
private int getMaxP() {
int maxP;
if (month.get(GregorianCalendar.MONTH) == month
.getActualMinimum(GregorianCalendar.MONTH)) {
pmonth.set((month.get(GregorianCalendar.YEAR) - 1),
month.getActualMaximum(GregorianCalendar.MONTH), 1);
} else {
pmonth.set(GregorianCalendar.MONTH,
month.get(GregorianCalendar.MONTH) - 1);
}
maxP = pmonth.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);
return maxP;
}
}
错误: 致命异议:主要 过程:com.cogneesol.mycalendar,PID:18708 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'android.view.View android.view.View.findViewById(int)'
答案 0 :(得分:1)
如果条件
将其置于内部v = convertView;
convertView.setTag(viewHolder);
问题可能出在dayView = (TextView) v.findViewById(R.id.date);
,如果v
为null
,则convertView
可以是null
或使用convertView.findViewById
代替v.findViewById
答案 1 :(得分:0)
删除你的v变量而不是它,使用convertView