Passing an argument between actions - null

I submit an object of my custom class that implements Serializable:

public class MyObject extends ParseObject implements Serializable { private static final long serialVersionUID = 1L; public MyObject() { } public String getTitle() { return getString("title"); } public void setTitle(String title) { put("title", title); } public ParseFile getParseFile() { return getParseFile("file"); } public void setParseFile(ParseFile file) { put("file", file); } } 

The class contains atm one line and one ParseFile.

I am trying to transfer an object from one action to another.

 private void editMyObject(MyObject myObject) { Intent intent = new Intent(AActivity.this, BActivity.class); intent.putExtra("MyObject", myObject); Toast.makeText(getApplicationContext(), "OUT: "+myObject.getTitle(), Toast.LENGTH_SHORT).show(); // here i get the content, proper string startActivity(intent); } 

And in the work of 'receiver':

 protected void onCreate(Bundle savedInstanceState) { ... Intent intent = getIntent(); myObject = (MyObject)intent.getSerializableExtra("MyObject"); if (myObject != null) { editText.setText(myObject.getTitle()); } Toast.makeText(getApplicationContext(), "IN: "+myObject.getTitle(), Toast.LENGTH_SHORT).show(); // here is null } 

What am I doing wrong...? I followed other questions on SOF, but that didn't help;) BTW: No, ParseFile is a class from Developer Collaboration

+4
source share
5 answers

I think it should be something like this ParsObject implements Serializable , because according to the topic of Serializable, if the Super class is not serializable, the state of the instance is not supported. So try to make the ParseObject Serializable class and see the result

Try something like this

 public class MyObject implements Serializable { private ParseObject parseObject; public MyObject() { parseObject = new ParseObject(); } public String getTitle() { return parseObject.getString("title"); } public void setTitle(String title) { parseObject.put("title", title); } public ParseFile getParseFile() { return parseObject.getParseFile("file"); } public void setParseFile(ParseFile file) { parseObject.put("file", file); } private void writeObject(java.io.ObjectOutputStream stream) throws IOException { stream.writeObject(parseObject); } private void readObject(java.io.ObjectInputStream stream) throws IOException, ClassNotFoundException { parseObject = (ParseObject) stream.readObject(); } } 
+1
source

You can use bundle to transfer data

 Bundle bundle = new Bundle(); bundle.putSerializable("MyObject", myObject); intent.putExtras(bundle); 

And get with

 Intent intent = getIntent(); Bundle bundle = intent.getExtras(); myObject = (MyObject)bundle.getSerializable("MyObject"); 
+1
source

I think your MyObject is not Serializable. To serialize a class, you need 2 conditions:

  • implement serializable interface
  • all participants must be Serializable

I doubt ParseFile is serializable, so this doesn't work.

0
source

here is my code that I use to pass the Object class between Activity:

 public class ClassName extends Activity{ static ClassName2 abc_obj; public ClassName(ClassName2 a_b_c){ abc_obj = a_b_c; } @Override public void onCreate(Bundle savedInstanceState) { // here you can access everything of class ClassName2 } } 

// now here is the code for moving the Abc class to ClassName:

 ClassName2 objAbc = new ClassName2(); //update ClassName2 according to you ClassName x_y_z = new ClassName(objAbc); Intent intent = new Intent(this, x_y_z.getClass()); startActivity(intent); 

Hope this helps you. happy coding :)

0
source
 public class MyObject extends ParseObject implements Serializable { 

Here, if ParseObject is your class? if so, try making it serializable . Also check that some of the Android objects are not serializable to ignore those who use transient .

also use intent.putSerializable("MyObject", myObject); and intent.gettSerializable("MyObject");

0
source

Source: https://habr.com/ru/post/1500927/


All Articles