Android 如何使用参数将活动从一个活动重定向到下一个活动

Android 如何使用参数将活动从一个活动重定向到下一个活动,android,android-intent,Android,Android Intent,我希望当一个日期被点击时,它将进入下一个活动。我包含了意图,但它不起作用。引发空指针错误。我在onclick函数中将此意图放在不同的位置,但它不起作用并引发异常。请帮助我如何获取并转到此活动中的下一个活动(日期为参数) package com.examples; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Cale

我希望当一个日期被点击时,它将进入下一个活动。我包含了意图,但它不起作用。引发空指针错误。我在onclick函数中将此意图放在不同的位置,但它不起作用并引发异常。请帮助我如何获取并转到此活动中的下一个活动(日期为参数)

package com.examples;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class SimpleCalendarViewActivity extends Activity implements OnClickListener
    {
        private static final String tag = "SimpleCalendarViewActivity";

        private ImageView calendarToJournalButton;
        private Button selectedDayMonthYearButton;
        private Button currentMonth;
        private ImageView prevMonth;
        private ImageView nextMonth;
        private GridView calendarView;
        private GridCellAdapter adapter;
        private Calendar _calendar;
        private int month, year;
        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);
                setContentView(R.layout.simple_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 = (Button) this.findViewById(R.id.currentMonth);
                currentMonth.setText(dateFormatter.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(dateFormatter.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 final int month, year;
                private int daysInMonth, prevMonthDays;
                private int currentDayOfMonth;
                private int currentWeekDay;
                private Button gridcell;
                private TextView num_events_per_day;
                private final HashMap eventsPerMonthMap;
                private final SimpleDateFormat dateFormatter = new SimpleDateFormat("dd-MMM-yyyy");

                // Days in Current Month
                public GridCellAdapter(Context context, int textViewResourceId, int month, int year)
                    {
                        super();
                        this._context = context;
                        this.list = new ArrayList<String>();
                        this.month = month;
                        this.year = year;

                        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();
                    }

                /**
                 * Prints Month
                 * 
                 * @param mm
                 * @param yy
                 */
                private void printMonth(int mm, int yy)
                    {
                        Log.d(tag, "==> printMonth: mm: " + mm + " " + "yy: " + yy);
                        // The number of days to leave blank at
                        // the start of this month.
                        int trailingSpaces = 0;
                        int leadSpaces = 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.");

                        // Gregorian Calendar : MINUS 1, set to FIRST OF MONTH
                        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);
                            }

                        // Compute how much to leave before before the first day of the
                        // month.
                        // getDay() returns 0 for Sunday.
                        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)) && mm == 1)
                            {
                                ++daysInMonth;
                            }

                        // 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);
                            }
                    }
                /**
                 * NOTE: YOU NEED TO IMPLEMENT THIS PART Given the YEAR, MONTH, retrieve
                 * ALL entries from a SQLite database for that month. Iterate over the
                 * List of All entries, and get the dateCreated, which is converted into
                 * day.
                 * 
                 * @param year
                 * @param month
                 * @return
                 */
                private HashMap findNumberOfEventsPerMonth(int year, int month)
                    {
                        HashMap map = new HashMap<String, Integer>();
                        // DateFormat dateFormatter2 = new DateFormat();
                        //                      
                        // String day = dateFormatter2.format("dd", dateCreated).toString();
                        //
                        // if (map.containsKey(day))
                        // {
                        // Integer val = (Integer) map.get(day) + 1;
                        // map.put(day, val);
                        // }
                        // else
                        // {
                        // map.put(day, 1);
                        // }
                        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.calendar_day_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("GREY"))
                            {
                                gridcell.setTextColor(Color.LTGRAY);
                            }
                        if (day_color[1].equals("WHITE"))
                            {
                                gridcell.setTextColor(Color.WHITE);
                            }
                        if (day_color[1].equals("BLUE"))
                            {
                                gridcell.setTextColor(getResources().getColor(R.color.static_text_color));
                            }
                        return row;
                    }
                @Override
                public void onClick(View view)
                    {
                        String date_month_year = (String) view.getTag();
                        selectedDayMonthYearButton.setText("Selected: " + date_month_year);

                        try
                            {
                                Date parsedDate = dateFormatter.parse(date_month_year);
                                Log.d(tag, "Parsed Date: " + parsedDate.toString());

                                Intent intent = new Intent(SimpleCalendarViewActivity.this,ViewReport.class);
                                intent.putExtra("date",parsedDate);
                                startActivity(intent);

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

                    }

                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;
                    }
            }
    }
package.com.examples;
导入java.text.ParseException;
导入java.text.simpleDataFormat;
导入java.util.ArrayList;
导入java.util.Calendar;
导入java.util.Date;
导入java.util.GregorianCalendar;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Locale;
导入android.app.Activity;
导入android.content.Context;
导入android.content.Intent;
导入android.graphics.Color;
导入android.os.Bundle;
导入android.text.format.DateFormat;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.view.view.OnClickListener;
导入android.widget.BaseAdapter;
导入android.widget.Button;
导入android.widget.GridView;
导入android.widget.ImageView;
导入android.widget.TextView;
公共类SimpleCalendarViewActivity扩展了活动实现OnClickListener
{
私有静态最终字符串标记=“SimpleCalendarViewActivity”;
private ImageView CalendarToJournal按钮;
私人按钮选择Daymonthyearbutton;
私人按钮当前月;
每月私人影像浏览量;
下个月的私有ImageView;
私有GridView日历视图;
专用网格适配器;
私人日历;
私人int月,年;
private final DateFormat dateFormatter=new DateFormat();
私有静态最终字符串dateTemplate=“MMMM yyyy”;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.simple\u calendar\u视图);
_calendar=calendar.getInstance(Locale.getDefault());
月=_calendar.get(calendar.month)+1;
year=\u calendar.get(calendar.year);
Log.d(标记,“日历实例:=”+“月:”+“+”年:“+年”);
selectedDayMonthYearButton=(按钮)this.findViewById(R.id.selectedDayMonthYear);
selectedDayMonthYearButton.setText(“Selected:”);
prevMonth=(ImageView)this.findViewById(R.id.prevMonth);
setOnClickListener(此);
currentMonth=(按钮)this.findViewById(R.id.currentMonth);
currentmount.setText(dateFormatter.format(dateTemplate,_calendar.getTime());
nextMonth=(ImageView)this.findViewById(R.id.nextMonth);
下一个月的setOnClickListener(此);
calendarView=(GridView)this.findViewById(R.id.calendar);
//草签
adapter=new GridCellAdapter(getApplicationContext(),R.id.calendar\u day\u gridcell,月,年);
adapter.notifyDataSetChanged();
calendarView.setAdapter(适配器);
}
/**
* 
*@param月
*@param年
*/
私有无效setGridCellAdapterToDate(整数月,整数年)
{
adapter=new GridCellAdapter(getApplicationContext(),R.id.calendar\u day\u gridcell,月,年);
_calendar.set(年,月-1,_calendar.get(calendar.DAY,OF_month));
currentmount.setText(dateFormatter.format(dateTemplate,_calendar.getTime());
adapter.notifyDataSetChanged();
calendarView.setAdapter(适配器);
}
@凌驾
公共void onClick(视图v)
{
如果(v==上个月)
{
如果(第11个月)
{
月=1;
年份++;
}
其他的
{
月份++;
}
Log.d(标签,“在GridCellAdapter中设置下个月:“+”月:“+月+”年:“+年”);
setGridCellAdapterToDate(月,年);
}
}
@凌驾
公共空间
{
Log.d(标签“销毁视图…”);
super.ondestory();
}
// ///////////////////////////////////////////////////////////////////////////////////////
//内部阶级
公共类GridCellAdapter扩展BaseAdapter实现OnClickListener
{
私有静态最终字符串tag=“GridCellAdapter”;
私有最终上下文_上下文;
私人最终名单;
私人静态最终整数日_偏移=1;
私人最终字符串[]工作日=新字符串[]{“Sun”、“Mon”、“Tue”、“Wed”、“Thu”、“Fri”、“Sat”};
私人最终字符串[]个月={“一月”、“二月”、“三月”、“四月”、“五月”、“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”};
私人期末整数[]天每月={31,28,31,30,31,30,31,31,30,31,30,31,31};
私人最终int月,年;
每月的私人国际日,每月的前几天;
私人int currentDayOfMonth;
工作日内的私人时间;
私有按钮网格单元;
私有文本查看每天的事件数;
私有最终HashMap事件映射;
专用最终SimpleDataFormat dateFormatter=新SimpleDataFormat(“dd MMM yyyy”);
//Da
Intent intent = new Intent(SimpleCalendarViewActivity.this,ViewReport.class);     
intent.putExtra("date",parsedDate);
startActivity(intent);  
Intent intent = new Intent(view.getContext(), ViewReport.class);
intent.putExtra("date", parsedDate);
startActivity(intent);