价值通过活动

时间:2013-10-28 13:36:19

标签: android

我正在制作一个Android应用程序,我希望将值从一个活动传递到另一个活动。我在示例项目中对其进行了编码,他们得到了所需的结果,但是当我将该代码放入我的项目时,它会给出错误消息“不幸的是,项目应用已停止”。任何人都可以告诉我为什么会这样吗???

第一个活动代码:

   public void btnAdd_Click(View view)
    {
        boolean ok=true;
        try {
            String str=a;
            Intent i=new Intent();
            i=new Intent(MyCalendarActivity.this, MainActivity.class);
            i.putExtra("mytext", str);
            startActivity(i);
        } catch (Exception e) {
        }}

第二个活动代码:

  et.setText(getIntent().getStringExtra("mytext"));

在Logcat中它给出了一个消息,即对intent有太多的工作,然后我更改它的名字,然后在它给出消息说主线程上有太多工作之后,在这种情况下我该怎么办?

logcat的

10-28 09:55:57.882: E/Selected date(2784): 17-October-2013
10-28 09:55:58.003: D/GridCellAdapter(2784): Parsed Date: Thu Oct 17 00:00:00 EDT 2013
10-28 09:55:58.024: I/Choreographer(2784): Skipped 146 frames!  The application may be doing too much work on its main thread.
10-28 09:56:08.854: D/AndroidRuntime(2784): Shutting down VM
10-28 09:56:08.904: W/dalvikvm(2784): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
10-28 09:56:09.103: E/AndroidRuntime(2784): FATAL EXCEPTION: main
10-28 09:56:09.103: E/AndroidRuntime(2784): java.lang.IllegalStateException: Could not find a method btnAdd_Click(View) in the activity class com.andi.smarttravels.MyCalendarActivity for onClick handler on view class android.widget.Button with id 'button1'

MyCalenderActivity.java

String a; 
private TextView currentMonth;
private Button selectedDayMonthYearButton;
private ImageView prevMonth;
private ImageView nextMonth;
private GridView calendarView;
private GridCellAdapter adapter;
private Calendar _calendar;
@SuppressLint("NewApi")
private int month, year;
@SuppressLint({ "NewApi", "NewApi", "NewApi", "NewApi" })
private final DateFormat dateFormatter = new DateFormat();
private static final String dateTemplate = "MMMM yyyy";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.my_calendar_view);


    _calendar = Calendar.getInstance(Locale.getDefault());
    month = _calendar.get(Calendar.MONTH) + 1;
    year = _calendar.get(Calendar.YEAR);
    Log.d(tag, "Calendar Instance:= " + "Month: " + month + " " + "Year: "
            + year);

    selectedDayMonthYearButton = (Button) this
            .findViewById(R.id.selectedDayMonthYear);
    selectedDayMonthYearButton.setText("Selected: ");

    prevMonth = (ImageView) this.findViewById(R.id.prevMonth);
    prevMonth.setOnClickListener(this);

    currentMonth = (TextView) this.findViewById(R.id.currentMonth);
    currentMonth.setText(DateFormat.format(dateTemplate,
            _calendar.getTime()));

    nextMonth = (ImageView) this.findViewById(R.id.nextMonth);
    nextMonth.setOnClickListener(this);

    calendarView = (GridView) this.findViewById(R.id.calendar);

    // Initialised
    adapter = new GridCellAdapter(getApplicationContext(),
            R.id.calendar_day_gridcell, month, year);
    adapter.notifyDataSetChanged();
    calendarView.setAdapter(adapter);
}

/**
 * 
 * @param month
 * @param year
 */
private void setGridCellAdapterToDate(int month, int year) {
    adapter = new GridCellAdapter(getApplicationContext(),
            R.id.calendar_day_gridcell, month, year);
    _calendar.set(year, month - 1, _calendar.get(Calendar.DAY_OF_MONTH));
    currentMonth.setText(DateFormat.format(dateTemplate,
            _calendar.getTime()));
    adapter.notifyDataSetChanged();
    calendarView.setAdapter(adapter);
}

@Override
public void onClick(View v) {
    if (v == prevMonth) {
        if (month <= 1) {
            month = 12;
            year--;
        } else {
            month--;
        }
        Log.d(tag, "Setting Prev Month in GridCellAdapter: " + "Month: "
                + month + " Year: " + year);
        setGridCellAdapterToDate(month, year);
    }
    if (v == nextMonth) {
        if (month > 11) {
            month = 1;
            year++;
        } else {
            month++;
        }
        Log.d(tag, "Setting Next Month in GridCellAdapter: " + "Month: "
                + month + " Year: " + year);
        setGridCellAdapterToDate(month, year);
    }

}

@Override
public void onDestroy() {
    Log.d(tag, "Destroying View ...");
    super.onDestroy();
}

// Inner Class
public class GridCellAdapter extends BaseAdapter implements OnClickListener {
    private static final String tag = "GridCellAdapter";
    private final Context _context;

    private final List<String> list;
    private static final int DAY_OFFSET = 1;
    private final String[] weekdays = new String[] { "Sun", "Mon", "Tue",
            "Wed", "Thu", "Fri", "Sat" };
    private final String[] months = { "January", "February", "March",
            "April", "May", "June", "July", "August", "September",
            "October", "November", "December" };
    private final int[] daysOfMonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30,
            31, 30, 31 };
    private int daysInMonth;
    private int currentDayOfMonth;
    private int currentWeekDay;
    private Button gridcell;
    private TextView num_events_per_day;
    private final HashMap<String, Integer> eventsPerMonthMap;
    private final SimpleDateFormat dateFormatter = new SimpleDateFormat(
            "dd-MMM-yyyy");

    // Days in Current Month
    @SuppressLint("SimpleDateFormat")
    public GridCellAdapter(Context context, int textViewResourceId,
            int month, int year) {
        super();
        this._context = context;
        this.list = new ArrayList<String>();
        Log.d(tag, "==> Passed in Date FOR Month: " + month + " "
                + "Year: " + year);
        Calendar calendar = Calendar.getInstance();
        setCurrentDayOfMonth(calendar.get(Calendar.DAY_OF_MONTH));
        setCurrentWeekDay(calendar.get(Calendar.DAY_OF_WEEK));
        Log.d(tag, "New Calendar:= " + calendar.getTime().toString());
        Log.d(tag, "CurrentDayOfWeek :" + getCurrentWeekDay());
        Log.d(tag, "CurrentDayOfMonth :" + getCurrentDayOfMonth());

        // Print Month
        printMonth(month, year);

        // Find Number of Events
        eventsPerMonthMap = findNumberOfEventsPerMonth(year, month);
    }

    private String getMonthAsString(int i) {
        return months[i];
    }

    private String getWeekDayAsString(int i) {
        return weekdays[i];
    }

    private int getNumberOfDaysOfMonth(int i) {
        return daysOfMonth[i];
    }

    public String getItem(int position) {
        return list.get(position);
    }

    @Override
    public int getCount() {
        return list.size();
    }


    private void printMonth(int mm, int yy) {
        Log.d(tag, "==> printMonth: mm: " + mm + " " + "yy: " + yy);
        int trailingSpaces = 0;
        int daysInPrevMonth = 0;
        int prevMonth = 0;
        int prevYear = 0;
        int nextMonth = 0;
        int nextYear = 0;

        int currentMonth = mm - 1;
        String currentMonthName = getMonthAsString(currentMonth);
        daysInMonth = getNumberOfDaysOfMonth(currentMonth);

        Log.d(tag, "Current Month: " + " " + currentMonthName + " having "
                + daysInMonth + " days.");

        GregorianCalendar cal = new GregorianCalendar(yy, currentMonth, 1);
        Log.d(tag, "Gregorian Calendar:= " + cal.getTime().toString());

        if (currentMonth == 11) {
            prevMonth = currentMonth - 1;
            daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
            nextMonth = 0;
            prevYear = yy;
            nextYear = yy + 1;
            Log.d(tag, "*->PrevYear: " + prevYear + " PrevMonth:"
                    + prevMonth + " NextMonth: " + nextMonth
                    + " NextYear: " + nextYear);
        } else if (currentMonth == 0) {
            prevMonth = 11;
            prevYear = yy - 1;
            nextYear = yy;
            daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
            nextMonth = 1;
            Log.d(tag, "**--> PrevYear: " + prevYear + " PrevMonth:"
                    + prevMonth + " NextMonth: " + nextMonth
                    + " NextYear: " + nextYear);
        } else {
            prevMonth = currentMonth - 1;
            nextMonth = currentMonth + 1;
            nextYear = yy;
            prevYear = yy;
            daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
            Log.d(tag, "***---> PrevYear: " + prevYear + " PrevMonth:"
                    + prevMonth + " NextMonth: " + nextMonth
                    + " NextYear: " + nextYear);
        }

        int currentWeekDay = cal.get(Calendar.DAY_OF_WEEK) - 1;
        trailingSpaces = currentWeekDay;

        Log.d(tag, "Week Day:" + currentWeekDay + " is "
                + getWeekDayAsString(currentWeekDay));
        Log.d(tag, "No. Trailing space to Add: " + trailingSpaces);
        Log.d(tag, "No. of Days in Previous Month: " + daysInPrevMonth);

        if (cal.isLeapYear(cal.get(Calendar.YEAR)))
            if (mm == 2)
                ++daysInMonth;
            else if (mm == 3)
                ++daysInPrevMonth;

        // Trailing Month days
        for (int i = 0; i < trailingSpaces; i++) {
            Log.d(tag,
                    "PREV MONTH:= "
                            + prevMonth
                            + " => "
                            + getMonthAsString(prevMonth)
                            + " "
                            + String.valueOf((daysInPrevMonth
                                    - trailingSpaces + DAY_OFFSET)
                                    + i));
            list.add(String
                    .valueOf((daysInPrevMonth - trailingSpaces + DAY_OFFSET)
                            + i)
                    + "-GREY"
                    + "-"
                    + getMonthAsString(prevMonth)
                    + "-"
                    + prevYear);
        }

        // Current Month Days
        for (int i = 1; i <= daysInMonth; i++) {
            Log.d(currentMonthName, String.valueOf(i) + " "
                    + getMonthAsString(currentMonth) + " " + yy);
            if (i == getCurrentDayOfMonth()) {
                list.add(String.valueOf(i) + "-BLUE" + "-"
                        + getMonthAsString(currentMonth) + "-" + yy);
            } else {
                list.add(String.valueOf(i) + "-WHITE" + "-"
                        + getMonthAsString(currentMonth) + "-" + yy);
            }
        }

        // Leading Month days
        for (int i = 0; i < list.size() % 7; i++) {
            Log.d(tag, "NEXT MONTH:= " + getMonthAsString(nextMonth));
            list.add(String.valueOf(i + 1) + "-GREY" + "-"
                    + getMonthAsString(nextMonth) + "-" + nextYear);
        }
    }


    private HashMap<String, Integer> findNumberOfEventsPerMonth(int year,
            int month) {
        HashMap<String, Integer> map = new HashMap<String, Integer>();

        return map;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        if (row == null) {
            LayoutInflater inflater = (LayoutInflater) _context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.screen_gridcell, parent, false);
        }

        // Get a reference to the Day gridcell
        gridcell = (Button) row.findViewById(R.id.calendar_day_gridcell);
        gridcell.setOnClickListener(this);

        // ACCOUNT FOR SPACING

        Log.d(tag, "Current Day: " + getCurrentDayOfMonth());
        String[] day_color = list.get(position).split("-");
        String theday = day_color[0];
        String themonth = day_color[2];
        String theyear = day_color[3];
        if ((!eventsPerMonthMap.isEmpty()) && (eventsPerMonthMap != null)) {
            if (eventsPerMonthMap.containsKey(theday)) {
                num_events_per_day = (TextView) row
                        .findViewById(R.id.num_events_per_day);
                Integer numEvents = (Integer) eventsPerMonthMap.get(theday);
                num_events_per_day.setText(numEvents.toString());
            }
        }

        // Set the Day GridCell
        gridcell.setText(theday);
        gridcell.setTag(theday + "-" + themonth + "-" + theyear);
        Log.d(tag, "Setting GridCell " + theday + "-" + themonth + "-"
                + theyear);

        if (day_color[1].equals("BLACK")) {
            gridcell.setTextColor(getResources()
                    .getColor(R.color.lightgray));
        }
        if (day_color[1].equals("WHITE")) {
            gridcell.setTextColor(getResources().getColor(
                    R.color.black));
        }
        if (day_color[1].equals("BLUE")) {
            gridcell.setTextColor(getResources().getColor(R.color.orrange));
        }
        return row;
    }

    @Override
    public void onClick(View view) {
        String date_month_year = (String) view.getTag();
        a=date_month_year;
        selectedDayMonthYearButton.setText("Selected: " + date_month_year);
        Log.e("Selected date", date_month_year);
        try {
            Date parsedDate = dateFormatter.parse(date_month_year);
            Log.d(tag, "Parsed Date: " + parsedDate.toString());

        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    public void btnAdd_Click(View view)
    {
        boolean ok=true;
        try {
            String str=a;
            Intent i=new Intent();
            i=new Intent(MyCalendarActivity.this, MainActivity.class);
            //i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            i.putExtra("mytext", str);
            startActivity(i);
        } catch (Exception e) {

        }}

    public int getCurrentDayOfMonth() {
        return currentDayOfMonth;
    }

    private void setCurrentDayOfMonth(int currentDayOfMonth) {
        this.currentDayOfMonth = currentDayOfMonth;
    }

    public void setCurrentWeekDay(int currentWeekDay) {
        this.currentWeekDay = currentWeekDay;
    }

    public int getCurrentWeekDay() {
        return currentWeekDay;
    }
}

}

2 个答案:

答案 0 :(得分:0)

你的logcat说明了一切:

java.lang.IllegalStateException: Could not find a method btnAdd_Click(View) in the activity class com.andi.smarttravels.MyCalendarActivity for onClick handler on view class android.widget.Button with id 'button1'

显然无法找到方法btnAdd_Click /未在适当的位置声明。

答案 1 :(得分:0)

使用View.OnClickListener接口而不是按钮的onCLick()属性。它比那更有效率。

修改

在您的活动中,在XML布局中创建对按钮的引用 (检查onCreate方法)

String a; 
private TextView currentMonth;
private Button selectedDayMonthYearButton;
private ImageView prevMonth;
private ImageView nextMonth;
private GridView calendarView;
private GridCellAdapter adapter;
private Calendar _calendar;
@SuppressLint("NewApi")
private int month, year;
@SuppressLint({ "NewApi", "NewApi", "NewApi", "NewApi" })
private final DateFormat dateFormatter = new DateFormat();
private static final String dateTemplate = "MMMM yyyy";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.my_calendar_view);


    _calendar = Calendar.getInstance(Locale.getDefault());
    month = _calendar.get(Calendar.MONTH) + 1;
    year = _calendar.get(Calendar.YEAR);
    Log.d(tag, "Calendar Instance:= " + "Month: " + month + " " + "Year: "
            + year);

    selectedDayMonthYearButton = (Button) this
            .findViewById(R.id.selectedDayMonthYear);
    selectedDayMonthYearButton.setText("Selected: ");


    /*************************************************/
    // this is how you add an onClickListener to a button or any other view
    selectedDayMonthYearButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //write the code here that you want to execute on button click
            btnAdd_Click(); //I removed the View argument of this method as it was of no use
        }
    });
    /***************************************************/



    prevMonth = (ImageView) this.findViewById(R.id.prevMonth);
    prevMonth.setOnClickListener(this);

    currentMonth = (TextView) this.findViewById(R.id.currentMonth);
    currentMonth.setText(DateFormat.format(dateTemplate,
            _calendar.getTime()));

    nextMonth = (ImageView) this.findViewById(R.id.nextMonth);
    nextMonth.setOnClickListener(this);

    calendarView = (GridView) this.findViewById(R.id.calendar);

    // Initialised
    adapter = new GridCellAdapter(getApplicationContext(),
            R.id.calendar_day_gridcell, month, year);
    adapter.notifyDataSetChanged();
    calendarView.setAdapter(adapter);
}

/**
 * 
 * @param month
 * @param year
 */
private void setGridCellAdapterToDate(int month, int year) {
    adapter = new GridCellAdapter(getApplicationContext(),
            R.id.calendar_day_gridcell, month, year);
    _calendar.set(year, month - 1, _calendar.get(Calendar.DAY_OF_MONTH));
    currentMonth.setText(DateFormat.format(dateTemplate,
            _calendar.getTime()));
    adapter.notifyDataSetChanged();
    calendarView.setAdapter(adapter);
}

@Override
public void onClick(View v) {
    if (v == prevMonth) {
        if (month <= 1) {
            month = 12;
            year--;
        } else {
            month--;
        }
        Log.d(tag, "Setting Prev Month in GridCellAdapter: " + "Month: "
                + month + " Year: " + year);
        setGridCellAdapterToDate(month, year);
    }
    if (v == nextMonth) {
        if (month > 11) {
            month = 1;
            year++;
        } else {
            month++;
        }
        Log.d(tag, "Setting Next Month in GridCellAdapter: " + "Month: "
                + month + " Year: " + year);
        setGridCellAdapterToDate(month, year);
    }

}

@Override
public void onDestroy() {
    Log.d(tag, "Destroying View ...");
    super.onDestroy();
}

// Inner Class
public class GridCellAdapter extends BaseAdapter implements OnClickListener {
    private static final String tag = "GridCellAdapter";
    private final Context _context;

    private final List<String> list;
    private static final int DAY_OFFSET = 1;
    private final String[] weekdays = new String[] { "Sun", "Mon", "Tue",
            "Wed", "Thu", "Fri", "Sat" };
    private final String[] months = { "January", "February", "March",
            "April", "May", "June", "July", "August", "September",
            "October", "November", "December" };
    private final int[] daysOfMonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30,
            31, 30, 31 };
    private int daysInMonth;
    private int currentDayOfMonth;
    private int currentWeekDay;
    private Button gridcell;
    private TextView num_events_per_day;
    private final HashMap<String, Integer> eventsPerMonthMap;
    private final SimpleDateFormat dateFormatter = new SimpleDateFormat(
            "dd-MMM-yyyy");

    // Days in Current Month
    @SuppressLint("SimpleDateFormat")
    public GridCellAdapter(Context context, int textViewResourceId,
            int month, int year) {
        super();
        this._context = context;
        this.list = new ArrayList<String>();
        Log.d(tag, "==> Passed in Date FOR Month: " + month + " "
                + "Year: " + year);
        Calendar calendar = Calendar.getInstance();
        setCurrentDayOfMonth(calendar.get(Calendar.DAY_OF_MONTH));
        setCurrentWeekDay(calendar.get(Calendar.DAY_OF_WEEK));
        Log.d(tag, "New Calendar:= " + calendar.getTime().toString());
        Log.d(tag, "CurrentDayOfWeek :" + getCurrentWeekDay());
        Log.d(tag, "CurrentDayOfMonth :" + getCurrentDayOfMonth());

        // Print Month
        printMonth(month, year);

        // Find Number of Events
        eventsPerMonthMap = findNumberOfEventsPerMonth(year, month);
    }

    private String getMonthAsString(int i) {
        return months[i];
    }

    private String getWeekDayAsString(int i) {
        return weekdays[i];
    }

    private int getNumberOfDaysOfMonth(int i) {
        return daysOfMonth[i];
    }

    public String getItem(int position) {
        return list.get(position);
    }

    @Override
    public int getCount() {
        return list.size();
    }


    private void printMonth(int mm, int yy) {
        Log.d(tag, "==> printMonth: mm: " + mm + " " + "yy: " + yy);
        int trailingSpaces = 0;
        int daysInPrevMonth = 0;
        int prevMonth = 0;
        int prevYear = 0;
        int nextMonth = 0;
        int nextYear = 0;

        int currentMonth = mm - 1;
        String currentMonthName = getMonthAsString(currentMonth);
        daysInMonth = getNumberOfDaysOfMonth(currentMonth);

        Log.d(tag, "Current Month: " + " " + currentMonthName + " having "
                + daysInMonth + " days.");

        GregorianCalendar cal = new GregorianCalendar(yy, currentMonth, 1);
        Log.d(tag, "Gregorian Calendar:= " + cal.getTime().toString());

        if (currentMonth == 11) {
            prevMonth = currentMonth - 1;
            daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
            nextMonth = 0;
            prevYear = yy;
            nextYear = yy + 1;
            Log.d(tag, "*->PrevYear: " + prevYear + " PrevMonth:"
                    + prevMonth + " NextMonth: " + nextMonth
                    + " NextYear: " + nextYear);
        } else if (currentMonth == 0) {
            prevMonth = 11;
            prevYear = yy - 1;
            nextYear = yy;
            daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
            nextMonth = 1;
            Log.d(tag, "**--> PrevYear: " + prevYear + " PrevMonth:"
                    + prevMonth + " NextMonth: " + nextMonth
                    + " NextYear: " + nextYear);
        } else {
            prevMonth = currentMonth - 1;
            nextMonth = currentMonth + 1;
            nextYear = yy;
            prevYear = yy;
            daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
            Log.d(tag, "***---> PrevYear: " + prevYear + " PrevMonth:"
                    + prevMonth + " NextMonth: " + nextMonth
                    + " NextYear: " + nextYear);
        }

        int currentWeekDay = cal.get(Calendar.DAY_OF_WEEK) - 1;
        trailingSpaces = currentWeekDay;

        Log.d(tag, "Week Day:" + currentWeekDay + " is "
                + getWeekDayAsString(currentWeekDay));
        Log.d(tag, "No. Trailing space to Add: " + trailingSpaces);
        Log.d(tag, "No. of Days in Previous Month: " + daysInPrevMonth);

        if (cal.isLeapYear(cal.get(Calendar.YEAR)))
            if (mm == 2)
                ++daysInMonth;
            else if (mm == 3)
                ++daysInPrevMonth;

        // Trailing Month days
        for (int i = 0; i < trailingSpaces; i++) {
            Log.d(tag,
                    "PREV MONTH:= "
                            + prevMonth
                            + " => "
                            + getMonthAsString(prevMonth)
                            + " "
                            + String.valueOf((daysInPrevMonth
                                    - trailingSpaces + DAY_OFFSET)
                                    + i));
            list.add(String
                    .valueOf((daysInPrevMonth - trailingSpaces + DAY_OFFSET)
                            + i)
                    + "-GREY"
                    + "-"
                    + getMonthAsString(prevMonth)
                    + "-"
                    + prevYear);
        }

        // Current Month Days
        for (int i = 1; i <= daysInMonth; i++) {
            Log.d(currentMonthName, String.valueOf(i) + " "
                    + getMonthAsString(currentMonth) + " " + yy);
            if (i == getCurrentDayOfMonth()) {
                list.add(String.valueOf(i) + "-BLUE" + "-"
                        + getMonthAsString(currentMonth) + "-" + yy);
            } else {
                list.add(String.valueOf(i) + "-WHITE" + "-"
                        + getMonthAsString(currentMonth) + "-" + yy);
            }
        }

        // Leading Month days
        for (int i = 0; i < list.size() % 7; i++) {
            Log.d(tag, "NEXT MONTH:= " + getMonthAsString(nextMonth));
            list.add(String.valueOf(i + 1) + "-GREY" + "-"
                    + getMonthAsString(nextMonth) + "-" + nextYear);
        }
    }


    private HashMap<String, Integer> findNumberOfEventsPerMonth(int year,
            int month) {
        HashMap<String, Integer> map = new HashMap<String, Integer>();

        return map;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        if (row == null) {
            LayoutInflater inflater = (LayoutInflater) _context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.screen_gridcell, parent, false);
        }

        // Get a reference to the Day gridcell
        gridcell = (Button) row.findViewById(R.id.calendar_day_gridcell);
        gridcell.setOnClickListener(this);

        // ACCOUNT FOR SPACING

        Log.d(tag, "Current Day: " + getCurrentDayOfMonth());
        String[] day_color = list.get(position).split("-");
        String theday = day_color[0];
        String themonth = day_color[2];
        String theyear = day_color[3];
        if ((!eventsPerMonthMap.isEmpty()) && (eventsPerMonthMap != null)) {
            if (eventsPerMonthMap.containsKey(theday)) {
                num_events_per_day = (TextView) row
                        .findViewById(R.id.num_events_per_day);
                Integer numEvents = (Integer) eventsPerMonthMap.get(theday);
                num_events_per_day.setText(numEvents.toString());
            }
        }

        // Set the Day GridCell
        gridcell.setText(theday);
        gridcell.setTag(theday + "-" + themonth + "-" + theyear);
        Log.d(tag, "Setting GridCell " + theday + "-" + themonth + "-"
                + theyear);

        if (day_color[1].equals("BLACK")) {
            gridcell.setTextColor(getResources()
                    .getColor(R.color.lightgray));
        }
        if (day_color[1].equals("WHITE")) {
            gridcell.setTextColor(getResources().getColor(
                    R.color.black));
        }
        if (day_color[1].equals("BLUE")) {
            gridcell.setTextColor(getResources().getColor(R.color.orrange));
        }
        return row;
    }

    @Override
    public void onClick(View view) {
        String date_month_year = (String) view.getTag();
        a=date_month_year;
        selectedDayMonthYearButton.setText("Selected: " + date_month_year);
        Log.e("Selected date", date_month_year);
        try {
            Date parsedDate = dateFormatter.parse(date_month_year);
            Log.d(tag, "Parsed Date: " + parsedDate.toString());

        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    public void btnAdd_Click()
    {
        boolean ok=true;
        try {
            String str=a;
            Intent i=new Intent();
            i=new Intent(MyCalendarActivity.this, MainActivity.class);
            //i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            i.putExtra("mytext", str);
            startActivity(i);
        } catch (Exception e) {

        }}

    public int getCurrentDayOfMonth() {
        return currentDayOfMonth;
    }

    private void setCurrentDayOfMonth(int currentDayOfMonth) {
        this.currentDayOfMonth = currentDayOfMonth;
    }

    public void setCurrentWeekDay(int currentWeekDay) {
        this.currentWeekDay = currentWeekDay;
    }

    public int getCurrentWeekDay() {
        return currentWeekDay;
    }
}
}

从XML布局文件中删除onClick属性。