Chuck94 Chuck94 - 3 months ago 31
Android Question

Parcel: unable to marshal value android.widget.RelativeLayout

I have a problem with a type of data(ArrayList of ViewGroup), that I want pass at a ResultActivity.class. This ViewGroup contains a layout block created via xml.

One of this block is already present in main layout managed of IPActivity class, its name in java code is mFirstBlock. After I insert mFirstBlock in arrayList. Then the user can add an equal new block with a button. All this blocks I add in arrayList.

This is layout file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView_link_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
...

<EditText
...

<TextView
...

<EditText
...

<ImageButton
...

</RelativeLayout>


And this is java code:

public class IPActivity extends AppCompatActivity {

private static final String EXTRA_BLOCKS = "...";

ArrayList<ViewGroup> mBlockLinks; //contiene i blocchi (i link con le info)

@Override
protected void onCreate(Bundle savedInstanceState) {
final LinearLayout myContainer = (LinearLayout) findViewById(R.id.my_container); //layout presente nell'mainLayout
mBlockLinks = new ArrayList<>();

mFirstBlock = (ViewGroup) findViewById(R.id.first_block);//R.id.first_block is the id layout of the block that I include in main Layout
mDeleteLink = (ImageButton) mFirstBlock.findViewById(R.id.imageButton_delete_link);
mDeleteLink.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
myContainer.removeView((View) v.getParent());
mBlockLinks.remove( v.getParent());
}
});

mBlockLinks.add(mFirstBlock);

mAddLink = (Button) findViewById(R.id.button_add_link);
mAddLink.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ViewGroup new_block = (ViewGroup) LayoutInflater.from(v.getContext()).inflate(R.layout.layout_block_input, myContainer, false);
myContainer.addView(new_block);
mBlockLinks.add(new_block);

mDeleteLink = (ImageButton) new_block.findViewById(R.id.imageButton_delete_link);
mDeleteLink.setOnClickListener(new OnClickListener() {
//cancello il link relativo al bottone
@Override
public void onClick(View v) {
myContainer.removeView((View) v.getParent());
mBlockLinks.remove( v.getParent());
}
});

}
});

mResult = (Button) findViewById(R.id.button_subnetting);
mResult.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(),ResultActivity.class);
intent.putExtra(EXTRA_BLOCKS,mBlockLinks);
startActivity(intent); //the line where appear the Exception
}
});
}
}


I have write only the code that most importat for me for resolve this problem.

Logcat error is below:

FATAL EXCEPTION: main
java.lang.RuntimeException: Parcel: unable to marshal value android.widget.RelativeLayout{42763ed8 V.E..... ......I. 0,176-656,352}
at android.os.Parcel.writeValue(Parcel.java:1235)
at android.os.Parcel.writeList(Parcel.java:622)
at android.os.Parcel.writeValue(Parcel.java:1195)
at android.os.Parcel.writeMapInternal(Parcel.java:591)
at android.os.Bundle.writeToParcel(Bundle.java:1646)
at android.os.Parcel.writeBundle(Parcel.java:605)
at android.content.Intent.writeToParcel(Intent.java:7353)
at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:2222)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1436)
at android.app.Activity.startActivityForResult(Activity.java:3464)
at android.app.Activity.startActivityForResult(Activity.java:3425)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:842)
at android.app.Activity.startActivity(Activity.java:3661)
at android.app.Activity.startActivity(Activity.java:3629)
at giusso.simone.indirizzamentoipsubnettingrete.IPActivity$5.onClick(IPActivity.java:122)
at android.view.View.performClick(View.java:4475)
at android.view.View$PerformClick.run(View.java:18786)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)


I read a lot of question where the problem was solved with the serializable interface that it was used for the class that it was passed for a new activity but in this case I don't know a method for solved this problem.

Thank for your help!

Answer

You cannot pass views, like ViewGroup, between activities.

If those activities are that tightly coupled, combine them into a single activity, using fragments or other techniques.

Comments