Without sending the calendar obj it will work fine. So how do you pass the Calendar object from one activity to another?

advertisements

we can pass String as

Intent returnIntent = new Intent();
returnIntent.putExtra("SelectedRadio", selectedRadio);

But if I want to pass Calendar object then how to pass it from one activity to another?

Thanks in advance.

This is my First Activity:

public class MyCalendar extends Activity {
public Calendar myCal;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_my_calendar);
    goCal= (ImageButton)findViewById(R.id.imageButton1);
myCal = Calendar.getInstance();
goCal.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent i = new Intent(MyCalendar.this, MainActivity.class);
            i.putExtra("calendar", myCal);
startActivity(i);

    });
}

}

This is my second Activity:

public class MainActivity extends Activity {

Intent myIntent;
Calendar today = (Calendar) myIntent.getExtras().getSerializable("calendar");
...
}

what is my fault. Why my app closed when i click goCal button. pls help me.. I am a new one..

my logcat:

    01-09 18:55:50.338: W/dalvikvm(4252): threadid=1: thread exiting with uncaught exception (group=0xb5f41288)
01-09 18:55:50.338: E/AndroidRuntime(4252): FATAL EXCEPTION: main
01-09 18:55:50.338: E/AndroidRuntime(4252): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.mycalendar/com.example.mycalendar.MainActivity}: java.lang.NullPointerException
01-09 18:55:50.338: E/AndroidRuntime(4252):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
01-09 18:55:50.338: E/AndroidRuntime(4252):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
01-09 18:55:50.338: E/AndroidRuntime(4252):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
01-09 18:55:50.338: E/AndroidRuntime(4252):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
01-09 18:55:50.338: E/AndroidRuntime(4252):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-09 18:55:50.338: E/AndroidRuntime(4252):     at android.os.Looper.loop(Looper.java:137)
01-09 18:55:50.338: E/AndroidRuntime(4252):     at android.app.ActivityThread.main(ActivityThread.java:4745)
01-09 18:55:50.338: E/AndroidRuntime(4252):     at java.lang.reflect.Method.invokeNative(Native Method)
01-09 18:55:50.338: E/AndroidRuntime(4252):     at java.lang.reflect.Method.invoke(Method.java:511)
01-09 18:55:50.338: E/AndroidRuntime(4252):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-09 18:55:50.338: E/AndroidRuntime(4252):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-09 18:55:50.338: E/AndroidRuntime(4252):     at dalvik.system.NativeStart.main(Native Method)
01-09 18:55:50.338: E/AndroidRuntime(4252): Caused by: java.lang.NullPointerException
01-09 18:55:50.338: E/AndroidRuntime(4252):     at com.example.mycalendar.MainActivity.<init>(MainActivity.java:44)
01-09 18:55:50.338: E/AndroidRuntime(4252):     at java.lang.Class.newInstanceImpl(Native Method)
01-09 18:55:50.338: E/AndroidRuntime(4252):     at java.lang.Class.newInstance(Class.java:1319)
01-09 18:55:50.338: E/AndroidRuntime(4252):     at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
01-09 18:55:50.338: E/AndroidRuntime(4252):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
01-09 18:55:50.338: E/AndroidRuntime(4252):     ... 11 more

Without sending calendar obj, it will work fine.. Then How to pass Calendar object from one activity to another?


Since Calendar implements Serializable you can simply do the following:

Calendar cal;
<calendar initialization>
intent.putExtra("calendar", cal);

Then for retrieving:

Calendar cal;

cal = (Calendar) intent.getSerializableExtra("calendar");