Diagram: On the left are the logical steps I follow, on the right the two layouts.
Well, this is the part of the relevant code that I have in myActivityMain
bButton.Click += delegate{
LinearLayout layout = FindViewById<LinearLayout>(Resource.Id.layout2);//En esta línea layout queda a "null"
contenidoPrincipal.RemoveAllViews(); //Contenido principal es la ScrollView
contenidoPrincipal.AddView(layout); //Aquí me tira Java.Lang.IllegalArgumentException
}
It has occurred to me that it could be because it MainActivity
is loaded ActivityMain.axml
as a view , if so, **How could I load it LinearLayout
in the ScrollView
?
I have already verified that all the Id's that I call exist.
Objective:
Go changing the content of the App without changing the Toolbar
or the buttons that allow you to switch between the different views.
I also leave here the actual layout that I use, hoping that you can understand what I am trying to do.
I also leave here both the StackTrace that gives me the exception and the Message.
StackTrace:
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <f32579baafc1404fa37ba3ec1abdc0bd>:0
at Java.Interop.JniEnvironment+InstanceMethods.CallVoidMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00069] in <09bf3e262b934ffab2ba01f9fc7fd54d>:0
at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeVirtualVoidMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x0002a] in <09bf3e262b934ffab2ba01f9fc7fd54d>:0
at Android.Views.ViewGroup.AddView (Android.Views.View child) [0x00031] in <25661073a35344a89f215a4cf81af37c>:0
at NombreApp.MainActivity.<OnCreate>b__11_0 (System.Object <p0>, System.EventArgs <p1>) [0x0001a] in C:\\Proyectos\\DirectorioApp\\NombreApp\\MainActivity.cs:71 --- End of managed Java.Lang.IllegalArgumentException stack trace ---
java.lang.IllegalArgumentException: Cannot add a null child view to a ViewGroup android.view.ViewGroup.addView(ViewGroup.java:4191)
android.view.ViewGroup.addView(ViewGroup.java:4173)
mono.android.view.View_OnClickListenerImplementor.n_onClick(Native Method)
mono.android.view.View_OnClickListenerImplementor.onClick(View_OnClickListenerImplementor.java:30)\n\tat android.view.View.performClick(View.java:5647)\n\tat android.view.View$PerformClick.run(View.java:22465)
android.os.Handler.handleCallback(Handler.java:754)
android.os.Handler.dispatchMessage(Handler.java:95)
android.os.Looper.loop(Looper.java:163)
android.app.ActivityThread.main(ActivityThread.java:6378)
java.lang.reflect.Method.invoke(Native Method)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:791)
Message:
Cannot add a null child view to a ViewGroup
You can fix it using
LayoutInflater
. Since in principle you cannot directly pass aLinearLayout
by parameter to theAddView()
.*1:
View v = inflater.Inflate(Resource.Layout. activity , null, false); This activity should be the name of your "
Layout.axml
", the one you are trying to add toActivityMain
.