0

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?

bCliks
  • 2,918
  • 7
  • 29
  • 52
  • possible duplicate of [How to send an object from one Android Activity to another using Intents?](http://stackoverflow.com/questions/2139134/how-to-send-an-object-from-one-android-activity-to-another-using-intents) – David Snabel-Caunt Jan 09 '13 at 12:43
  • you can pass parameters to constructor of the second activity class. I am not sure if this is the solutions. You can try. – Raghunandan Jan 09 '13 at 12:44
  • 1
    Shouldn't be passing arguments to the constructor of a activity class, since you don't actually create the activity. You create an Intent with data, and Android creates the activtiy. – NickL Jan 09 '13 at 12:58
  • [Parecelable](http://techdroid.kbeanie.com/2010/06/parcelable-how-to-do-that-in-android.html) Class seems the best option to me. Have a look at these posts, - [Pass Objects to Next Activity](http://stackoverflow.com/questions/14037254/how-to-pass-cursor-object-to-next-activity-using-intents) - [Send Objects from one Activity to Another](http://stackoverflow.com/a/2141166/1626878). – Sahil Mahajan Mj Jan 09 '13 at 12:47
  • Calendar class does not extend Parecelable, but does extend Serializable – NickL Jan 09 '13 at 12:59
  • Hmm, Good to know that.:) – Sahil Mahajan Mj Jan 09 '13 at 13:01
  • Without sending calendar obj, it worked fine. but i need to send a Calendar object. – bCliks Jan 09 '13 at 14:02

2 Answers2

4

we know Calander implements Serializable interface so, you can send it as a extra in the intent.

Intent i=new Intent(context,Home.class);
                Calendar c=Calendar.getInstance();
                i.putExtra("calobject", c);
                startActivity(i);

and you can get it using this code

 Calendar cal=(Calendar) getIntent().getSerializableExtra("calobject");

Here is the screenshot of debugged code it goes smoothly without any exceptionenter image description here I hope this will help you

Pragnani
  • 20,075
  • 6
  • 49
  • 74
  • 1
    No need to 'serialize' the object, since it implements the Serializable interface, Android/Java does this by itself. – NickL Jan 09 '13 at 12:51
  • Without sending calendar obj, it will work fine. but i need to send Calendar object. – bCliks Jan 09 '13 at 14:03
  • 1
    I have posted a code snippet with the debugged code please check that @bala – Pragnani Jan 09 '13 at 17:34
3

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");
NickL
  • 4,258
  • 2
  • 21
  • 38
  • pls check my update code.. i did as ur answer but my app closed when i click goCal button. – bCliks Jan 09 '13 at 13:10
  • Use getIntent() instead of your (not initialized) myIntent object – NickL Jan 09 '13 at 13:14
  • like this..?? `Calendar today = (Calendar) getIntent().getExtras().getSerializable("calendar");` same result.. – bCliks Jan 09 '13 at 13:22
  • 01-09 18:55:50.338: E/AndroidRuntime(4252): at com.example.mycalendar.MainActivity.(MainActivity.java:44). This line tells you there accurs a NullPointerException at line 44 in your MainActivity.java. Does using getIntent() result in the same exception? Check if this is line 44. – NickL Jan 09 '13 at 13:47
  • yes.. it is.. `line 44: Calendar today = (Calendar) getIntent().getExtras().getSerializable("calendar");` it means?? wat can i do.. – bCliks Jan 09 '13 at 13:59
  • Without sending calendar obj, it will work fine. but i need to send Calendar object. – bCliks Jan 09 '13 at 14:02
  • 1
    try getIntent().getSerializableExtra("calendar"); Might be because you are using putExtra() instead of adding a Bundle to the intent (which would be what getExtras() returns). – NickL Jan 09 '13 at 14:35