0

我们可以将字符串传递为

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

但是如果我想传递 Calendar 对象,那么如何将它从一个活动传递到另一个活动?

提前致谢。

这是我的第一个活动:

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

    });     
}

}

这是我的第二个活动:

public class MainActivity extends Activity {

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

我有什么错。为什么当我单击 goCal 按钮时我的应用程序关闭。请帮助我..我是新人..

我的日志猫:

    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     

如果不发送日历 obj,它会正常工作。那么如何将日历对象从一个活动传递到另一个活动?

4

2 回答 2

4

我们知道 Calander 实现了 Serializable 接口,因此您可以将其作为额外的 Intent 发送。

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

您可以使用此代码获取它

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

这是调试代码的屏幕截图,它运行顺利,没有任何异常在此处输入图像描述 ,希望对您有所帮助

于 2013-01-09T12:49:15.750 回答
3

由于Calendar实现了Serializable您可以简单地执行以下操作:

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

然后用于检索:

Calendar cal;

cal = (Calendar) intent.getSerializableExtra("calendar");
于 2013-01-09T12:46:17.553 回答