-1

我想将 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");
4

2 回答 2

0

一种选择可能是让您的自定义类实现 Serializable 接口,然后您可以使用 Intent#putExtra() 方法的 putExtra(Serializable..) 变体以额外的意图传递对象实例。

伪代码:

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

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

有关更多说明,请参阅

于 2013-01-19T04:42:09.357 回答
0

试试这个,

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

在第二个活动中,您可以使用

getIntent().getSerializableExtra("scene");
  • 尝试按照为预定义类创建对象。使用facilityIntent而不是FacilityIntent
于 2013-01-19T03:26:18.590 回答