2012-12-09 35 views
0

Android编程的新手。无法在活动之间传递对象并使其可以进行parcelable。有人能告诉我问题可能是什么?我想知道逻辑是否正确。Android:在活动之间传递对象并使其可以登录

public class MainActivity extends Activity { 

    private Bundle MyActivityParams; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     Intent intent = new Intent(this, MyActivity.class); 
     MyActivityParams = fillInData(MyActivityParams); 
     intent.putExtras(MyActivityParams); 
     startActivity(intent, savedInstanceState); 
    } 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 
    private Bundle fillInData(Bundle bundle){ 

     bundle.putFloat("com.company.MyActivity.FL", 12); 
     bundle.putFloat("com.company.MyActivity.VH", 100); 
     bundle.putFloat("com.company.MyActivity.const", 1); 
     return bundle; 
    } 
} 

public class DisplayActivity extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     displayData(); 

    } 
    private void displayData(){ 
     //ActivityData is from MyActivity 
     ActivityData data = new ActivityData(getIntent().getExtras()); 

    } 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_display, menu); 
     return true; 
    } 
} 

public class MyActivity extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     displayParams(); 
    } 
    private void displayParams(){ 
     ActivityData dataBundle = new ActivityData((getIntent()).getExtras()); 
     transfer(dataBundle); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.activity_algorithm, menu); 
     return true; 
    } 

    public void transfer(ActivityData incomingdata){ 
     startActivity(incomingdata.getIntent()); 
    } 

    protected static class ActivityData{ 
     private Bundle data; 
     private TextView text; 
     private Intent intent; 
     public ActivityData(Bundle bundle){ 
      /*data = bundle; 
      intent = new Intent(null, DisplayActivity.class); 
      text = new TextView(null);*/ 
     } 

     public void display(String key){ 
      this.text.setText(data.getString(key)); 
        //not allowed: startActivity(intent); 
     //not allowed either: setContentView(text); 
     } 

     public Intent getIntent() { 
      return intent; 
     } 
     public void setIntent(Intent intent) { 
      this.intent = intent; 
     } 
     public TextView getText() { 
      return text; 
     } 
     public void setText(TextView text) { 
      this.text = text; 
     } 
     public Bundle getData() { 
      return data; 
     } 
     public void setData(Bundle data) { 
      this.data = data; 
     } 
    } 
} 
+0

因为你想,你正在做错误的方式对活动之间共享数据的一切在其他活动中访问一个活动方法,第二点是当你想分享非定制数据类型如String .Integer,Float ...在使用intent或bundle的应用程序之间,然后不需要实现parcelable接口。 –

+0

运行此代码时出现任何错误? –

+0

你没有初始化'MyActivityParams'包。初始化'MyActivityParams'捆绑包,然后在'MainActivity'活动中使用它:'Bundle MyActivityParams = new Bundle();' –

回答

2

让我们有一个Person类

public class Person { 
    private String name; 
    private String email; 
    private int age; 

    public Person(int age, String name, String email) { 
     this.age = age; 
     this.name = name; 
     this.email = email; 
    } 
} 

一个parcelable一个看起来像这样

public class ParcelablePerson implements Parcelable { 

    private final Person person; 

    private ParcelablePerson(Parcel parcel) { 
     this.person = new Person(parcel.readInt(), parcel.readString(), parcel.readString()); 
    } 

    public ParcelablePerson(Person person) { 
     this.person = person; 
    } 

    public Person getPerson() { 
     return person; 
    } 

    @Override 
    public int describeContents() { 
     return 0; 
    } 

    // This is the method where you disassembly your object to pieces 
    @Override 
    public void writeToParcel(Parcel parcel, int flags) { 
     parcel.writeInt(person.getAge()); 
     parcel.writeString(person.getName()); 
     parcel.writeString(person.getEmail()); 
    } 

    public static final Creator<ParcelablePerson> CREATOR = new Creator<ParcelablePerson>() { 

     // And here you create a new instance from a parcel using the first constructor 
     @Override 
     public ParcelablePerson createFromParcel(Parcel parcel) { 
      return new ParcelablePerson(parcel); 
     } 

     @Override 
     public ParcelablePerson[] newArray(int size) { 
      return new ParcelablePerson[size]; 
     } 

    }; 
} 

在这个过程中关键的东西是有private ParcelablePerson(Parcel parcel)变量的相同顺序构造函数和public void writeToParcel(Parcel parcel, int flags)方法...你可以在年龄属性上看到它,它是一个整数。

+0

请尝试详细说明您的答案并提供一个基本示例(例如,从您的链接复制它并引用源代码) 。否则,如果链接不正确,你的回答就毫无价值。 – Veger

+0

他只是传递整数然后我认为没有必要为它创建一个Parcelable类 –

+2

@ρяσѕρєяK我认为他可以通过这个例子来解决它,并且对于更多的情况,答案会很有用... – user219882

-1

试试这个代码适合我。但我已经实现了可序列化而不是可以parcelable。 尝试实现parcelable而不是serializable。并尝试它的工作。我试过这个,但实施parcelable。

要传递和对象到另一个活动:

in activity A 
make sure the class implements the serializable interface 
create a new intent 
create a new bundle 
use the putSerializeable method to add the object to the bundle with a key 
put the bundle in the intent by using the putExtras method 
start the activity using the startActivity method 


Specifically, 
     Intent intent = new Intent(ShoulderExerciseScreen.this, ExercisesScreen.class); 

     Bundle bundle = new Bundle(); 
     bundle.putSerializable("exercise", new Exercise("BenchPress)); 

     intent.putExtras(bundle); 

     startActivity(intent); 
in activity B 
declare and object of the type you want passed 
use the getIntent method to get the intent 
use the getSerializeableExtra to get the the value using the key 
cast the returned value from getSerializeableExtra and the desired type 
    Specifically, 
     Exercise ex = (Exercise)getIntent().getSerializableExtra("exercise"); 

     if(ex != null) 
     { 
      //do something 
     } 
+2

可序列化不可Parcelable ... – user219882

0

试试这个。

ParcelTest.java

import java.util.HashMap; 

import android.os.Parcel; 
import android.os.Parcelable; 

public class ParcelTest implements Parcelable { 
    private HashMap map; 

    public ParcelTest() { 
     map = new HashMap(); 
    } 

    public ParcelTest(Parcel in) { 
     map = new HashMap(); 
     readFromParcel(in); 
    } 

    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { 
     public ParcelTest createFromParcel(Parcel in) { 
      return new ParcelTest(in); 
     } 

     public ParcelTest[] newArray(int size) { 
      return new ParcelTest[size]; 
     } 
    }; 

    @Override 
    public int describeContents() { 
     return 0; 
    } 

    @Override 
    public void writeToParcel(Parcel dest, int flags) { 
     dest.writeInt(map.size()); 
     for (String s: map.keySet()) { 
      dest.writeString(s); 
      dest.writeString(map.get(s)); 
     } 
    } 

    public void readFromParcel(Parcel in) { 
     int count = in.readInt(); 
     for (int i = 0; i < count; i++) { 
      map.put(in.readString(), in.readString()); 
     } 
    } 

    public String get(String key) { 
     return map.get(key); 
    } 

    public void put(String key, String value) { 
     map.put(key, value); 
    } 
} 

ExampleSupplierActivity.java

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 

public class ExampleSupplierActivity extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     ParcelTest p = new ParcelTest(); 
     p.put("green", "go"); 
     p.put("yellow", "wait"); 
     p.put("red", "stop"); 

     Bundle b = new Bundle(); 
     b.putParcelable("com.example.trafficlight", p); 

     Intent i = new Intent(this, ExampleConsumerActivity.class); 
     i.putExtras(b); 

     startActivity(i); 
    } 
} 

ExampleConsumerActivity.java

import android.app.Activity; 
import android.os.Bundle; 

public class ExampleConsumerActivity extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     Bundle b = getIntent().getExtras(); 

     ParcelTest p = b.getParcelable("com.example.trafficlight"); 

     String red = p.get("red"); 
     // ... 
    } 
}