尝试使用Intent putExtra方法时出现运行时错误

时间:2013-01-19 03:21:33

标签: java android android-intent

我想将OAScene场景对象发送到下一个Activity类但我不能。它会立即停止应用程序..如果我打开其他活动类而不使用putExtra方法它可以工作......但在我的情况下我需要发送对象......

继承人的代码

public void setScene(OAScene scene) {
    final OAScene _scene = scene; 
    this.currentScene = scene;
    setColor(color_black);

    double dis; 
    dis = CalculationByDistance(latitudeOri, latitudeOri, scene.getLatitude(), scene.getLongitude());

    // Set the text fields of the notification bubble to match the data of
    // the selected scene.
    popup_name.setText("Name: " + scene.getName());
    popup_text.setText("Distance: " + dis + "m");

    more_info.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
             OAScene scene=_scene; 
             Intent FacilityIntent;
             FacilityIntent = new Intent(getContext(), MapViewActivity.class);
             FacilityIntent.putExtra("scene", _scene);
             getContext().startActivity(FacilityIntent);
        }
     });
}

登录..

01-20 15:50:31.196: E/AndroidRuntime(21403): java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.hitlabnz.outdoorar.api.OAScene)
01-20 15:50:31.196: E/AndroidRuntime(21403):    at android.os.Parcel.writeSerializable(Parcel.java:1181)
01-20 15:50:31.196: E/AndroidRuntime(21403):    at android.os.Parcel.writeValue(Parcel.java:1135)
01-20 15:50:31.196: E/AndroidRuntime(21403):    at android.os.Parcel.writeMapInternal(Parcel.java:493)
01-20 15:50:31.196: E/AndroidRuntime(21403):    at android.os.Bundle.writeToParcel(Bundle.java:1612)
01-20 15:50:31.196: E/AndroidRuntime(21403):    at android.os.Parcel.writeBundle(Parcel.java:507)
01-20 15:50:31.196: E/AndroidRuntime(21403):    at android.content.Intent.writeToParcel(Intent.java:6190)
01-20 15:50:31.196: E/AndroidRuntime(21403):    at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1696)
01-20 15:50:31.196: E/AndroidRuntime(21403):    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1382)
01-20 15:50:31.196: E/AndroidRuntime(21403):    at android.app.Activity.startActivityForResult(Activity.java:3190)
01-20 15:50:31.196: E/AndroidRuntime(21403):    at android.app.Activity.startActivity(Activity.java:3297)
01-20 15:50:31.196: E/AndroidRuntime(21403):    at com.hitlabnz.tutorialbasic.TutorialSceneNotificationBubble$2.onClick(TutorialSceneNotificationBubble.java:141)
01-20 15:50:31.196: E/AndroidRuntime(21403):    at android.view.View.performClick(View.java:3620)
01-20 15:50:31.196: E/AndroidRuntime(21403):    at android.view.View$PerformClick.run(View.java:14292)
01-20 15:50:31.196: E/AndroidRuntime(21403):    at android.os.Handler.handleCallback(Handler.java:605)
01-20 15:50:31.196: E/AndroidRuntime(21403):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-20 15:50:31.196: E/AndroidRuntime(21403):    at android.os.Looper.loop(Looper.java:137)
01-20 15:50:31.196: E/AndroidRuntime(21403):    at android.app.ActivityThread.main(ActivityThread.java:4512)
01-20 15:50:31.196: E/AndroidRuntime(21403):    at java.lang.reflect.Method.invokeNative(Native Method)
01-20 15:50:31.196: E/AndroidRuntime(21403):    at java.lang.reflect.Method.invoke(Method.java:511)

代码接收意图:

Intent i = getIntent();
        OAScene scene = (OAScene)i.getSerializableExtra("scene");

2 个答案:

答案 0 :(得分:0)

试试这个,

 facilityIntent= new Intent(YourCurrectActivity.this, MapViewActivity.class);
 facilityIntent.putExtra("scene", _scene);
 startActivity(facilityIntent);

在您可以使用

接收的第二项活动中
getIntent().getSerializableExtra("scene");
  • 尝试按照为预定义类创建对象。使用 facilityIntent 代替 FacilityIntent

答案 1 :(得分:0)

一个选项可能是让您的自定义类实现Serializable接口,然后您可以使用Intent#putExtra()方法的putExtra(Serializable ..)变体在intent中传递对象实例。

PSEUDO代码:

//to pass :
   intent.putExtra("MyClass", obj);  

// to retrieve object in second Activity
getIntent().getSerializableExtra("MyClass");

有关更多说明,请参阅this