Attempt to invoke virtual method 'void android.view.View.resolveLayoutParams()' on a null object reference - xamarin.android

I have a ScrollView added to a FrameLayout. In a button click I remove the ScrollView from the FrameLayout and add an element derived from ViewGroup. If the button is clicked again the ViewGroup is removed and the ScrollView is added back. While adding the ScrollView back to the FrameLayout after removing the ViewGroup I get the following exception from the AddView method of the FrameLayout. Actually it is thrown from the AddView() method of ViewGroup class, since FrameLayout is derived from ViewGroup.
Unhandled Exception:
Java.Lang.NullPointerException: Attempt to invoke virtual method 'void
android.view.View.resolveLayoutParams()' on a null object reference
occurred
I have already tried to toggle the visibility of both Views instead of adding one and removing the other. But after toggling visibility, the application crashes every time when the FrameLayout's OnLayout is called such as when the Android keyboard appears or if the device orientation changes.
I am sorry that I am not able to provide the code. Because my code is very complex and I could not reproduce this problem in a simple sample. I just posted this question as a last resort.
I know it is not possible to get solution if code is not included. But any input related to this problem will be helpful.

Since you cannot provide the code here, I can only write part of the code to show your requirements:
in xaml,i add a ScrolView (with its children) and a LinearLayout(which as your ViewGroup) into a FrameLayout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/framelayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above = "#+id/click"
>
<ScrollView
android:id="#+id/scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text ="text in scrollview"
/>
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="#+id/linearlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text ="text iFn LinearLayout"
/>
</LinearLayout>
</FrameLayout>
<Button
android:id = "#+id/click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="click"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
in activity show and hide the Scrolview and LinearLayout by click Button:
private int num = 1;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
Button button = FindViewById<Button>(Resource.Id.click);
FrameLayout frameLayout = FindViewById<FrameLayout>(Resource.Id.framelayout);
ScrollView scrollView = FindViewById<ScrollView>(Resource.Id.scroll);
LinearLayout linearLayout = FindViewById<LinearLayout>(Resource.Id.linearlayout);
button.Click += delegate
{
if (num % 2 ==0)
{
scrollView.Visibility = Android.Views.ViewStates.Gone;
linearLayout.Visibility = Android.Views.ViewStates.Visible;
}
else
{
linearLayout.Visibility = Android.Views.ViewStates.Gone;
scrollView.Visibility = Android.Views.ViewStates.Visible;
}
num++;
};
}

Related

center the text in position 0, code problem xamarin

I looked everywhere, but for the moment works.
here is my code:
<Spinner
android:id="#+id/departement"
android:layout_width="match_parent"
android:layout_height="25dp"
android:background="#color/colorAccent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:gravity="center_vertical|center_horizontal"
/>
Spinner departement = (Spinner)sender;
((TextView)departement.GetChildAt(0)).SetForegroundGravity(GravityFlags.CenterHorizontal);
((TextView)departement.GetChildAt(0)).SetTextColor(Color.White);
the color works, but the center position does not. do you have an idea ?
You can use a new xml to define the content of that item in Spinner.
And cusotmize the style in that xml .
Item.xml
<?xml version="1.0" encoding="utf-8" ?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" //center
android:textColor="#android:color/holo_red_dark"/> //Textcolor
Set in Adapter
ArrayAdapter<string> adapter = new ArrayAdapter<string>(this,Resource.Layout.Item,items);
departement.Adapter = adapter;

Xamarin forms layout initialisation issue

I'm getting the following errors with my code
LinearLayout selectMain = FindViewById<GridLayout>(Resource.Id.select);
Error CS0029 Cannot implicitly convert type 'Android.Widget.GridLayout' to 'Android.Widget.LinearLayout'
GridLayout inputMain = FindViewById<LinearLayout>(Resource.Id.input);
Error CS0029 Cannot implicitly convert type 'Android.Widget.LinearLayout' to 'Android.Widget.GridLayout'
this is confusing me because they are both the correct layout types, and if I swap the layout types in my code, it builds fine but when run it throws almost the same exemption.
protected override void OnCreate(Bundle savedInstanceState) //run on startup.
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
SetContentView(Resource.Layout.activity_main);
LinearLayout selectMain = FindViewById<GridLayout>(Resource.Id.select);
GridLayout inputMain = FindViewById<LinearLayout>(Resource.Id.input);
}
<GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="4"
android:rowCount="9"
android:id="#+id/input"
tools:gridSpec="1|8|#0093eeff|K:#ee8700ff:16,l:72,l:16,r|S:#83ee00ff:16,0,l:16,0,r">
</GridLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/select">
</LinearLayout>
You have your types swapped.
LinearLayout selectMain = FindViewById<GridLayout>(Resource.Id.select);
GridLayout inputMain = FindViewById<LinearLayout>(Resource.Id.input);
it should be:
//FindViewById<LinearLayout>
LinearLayout selectMain = FindViewById<LinearLayout>(Resource.Id.select);
//FindViewById<GridLayout >
GridLayout inputMain = FindViewById<GridLayout>(Resource.Id.input);

How to set vertical LinearLayout layout_gravity to center_horizontal programatically? (Xamarin.Android)

I'd like to center the following LinearLayout to the center of his parent programatically:
<LinearLayout
android:id="#+id/SignButtonsLinearLayout"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<homes.shared.components.customviews.HomesButton
android:id="#+id/CheckListPDFClosureButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="3dip"
android:drawableLeft="#drawable/CheckListPDFClosure"
android:background="#drawable/MainBlueButton"
style="#style/WhiteDeliveryNoteMenuBold"
android:text="#string/CheckListPDFClosure" />
<homes.shared.components.customviews.HomesButton
android:id="#+id/SignatureAndCloseButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/SignatureAndClosure"
android:background="#drawable/MainBlueButton"
style="#style/WhiteDeliveryNoteMenuBold"
android:text="#string/SignatureAndClose" />
</LinearLayout>
I have instanced the LinearLayout in code like this:
LinearLayout ll = this.FindViewById<LinearLayout>(Resource.Id.SignButtonsLinearLayout);
but the object doesn't have LayoutGravity property
You can do it quick hand like this:
LinearLayout ll = this.FindViewById<LinearLayout>(Resource.Id.SignButtonsLinearLayout);
ll.SetGravity(GravityFlags.Center);
Or you can do it this way too:
LinearLayout ll = this.FindViewById<LinearLayout>(Resource.Id.SignButtonsLinearLayout);
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams)lin.LayoutParameters;
layoutParams.Gravity = GravityFlags.Center;
ll.LayoutParameters = layoutParams;
Some more info can be found in this SO reply here as well.
EDIT:
Apologies, to adjust the layout gravity which sets the gravity of the View or Layout relative to its parent then you should just be able to add this attribute to your axml layout file. android:layout_gravity="center" like this:
<LinearLayout
android:id="#+id/SignButtonsLinearLayout"
android:layout_gravity="center"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
The other option is to stack layouts, so you could wrap your linear layout in a relative layout like this and use center in parent.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:focusableInTouchMode="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/SignButtonsLinearLayout"
android:layout_centerInParent="true"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</LinearLayout>
</RelativeLayout>

how to combine status bar and toolbar with gradient in xamarin android

I want to combine status bar and toolbar with gradient like this
I have tried to set status bar transparent and make the toolbar big and set the gradient .
But It also makes your UI goes under the Soft navigation keys.
And also soft keyboard is covering the EditTexts.
android:windowSoftInputMode="adjustPan" not working
if (Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat)
{
Window window = Window;
window.AddFlags(WindowManagerFlags.LayoutNoLimits);
window.AddFlags(WindowManagerFlags.TranslucentNavigation);
}
Is there any way only the status bar make transparent rather than transparent both status bar and navigation button on the bottom.
You could alse do like this,add the code into the OnCreate method :
protected override void OnCreate(Bundle savedInstanceState)
{
if (Build.VERSION.SdkInt >= Build.VERSION_CODES.Kitkat)
{
Window w = Window;
w.AddFlags(WindowManagerFlags.TranslucentStatus);
w.SetSoftInputMode(SoftInput.StateHidden|SoftInput.AdjustResize);
}
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.activity_other);
}
then create the gradient.xml in Resources/drawable :
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:angle="135"
android:startColor="#f56f2c"
android:endColor="#fa9f46"/>
</shape>
at last in the layout.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation = "vertical"
android:layout_height="match_parent"
android:fitsSystemWindows= "true"
android:background= "#drawable/gradient"
>
<!--use Toolbar-->
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:title="标题"
app:titleTextColor="#fff"
app:subtitle="副标题"
app:subtitleTextColor="#fff"/>
<!--if dont use Toolbar,RePresent Toolbar-->
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="44dp">
<ImageView
android:layout_gravity="center_vertical"
android:src="#drawable/ic_arrow_back_white_24dp"
android:layout_marginLeft="10dp"
android:layout_width="35dp"
android:layout_height="35dp" />
<TextView
android:layout_gravity="center_vertical"
android:textSize="25sp"
android:gravity="center_horizontal"
android:textColor="#fff"
android:text="Test Title"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:id="#+id/scrollView1"
android:background = "#fff" //set the background color of your content
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation ="vertical" >
<!--your content -->
...
</LinearLayout>
</ScrollView>
</LinearLayout>
Why don't you set the color of status bar same as the startColor of gradient and let the toolbar start from where it does.
Create a static method that sets the status bar colour something like below:
public static void SetStatusBarGradiant(Activity activity)
{
if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
{
Window window = activity.Window;
Drawable background = ResourcesCompat.GetDrawable(activity.Resources, Resource.Drawable.abc_ab_share_pack_mtrl_alpha, null);
window.AddFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);
window.SetStatusBarColor(Android.Graphics.Color.Transparent);
window.SetNavigationBarColor(Android.Graphics.Color.Transparent);
window.SetBackgroundDrawable(background);
}
}
Then add a gradient.xml in drawable something like below see to it you set the start color and end color as per your requirement:
gradient.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:angle="90"
android:startColor="#FFFF0000"
android:endColor="#FF00FF00"
android:type="linear" />
</shape>
</item>
</selector>
Goodluck
Revert if you have queries

TextView in PagerAdapter: scrollable in API15-17, not in API7

This FragmentActivity creates a few fragments that are put in a slider (horizontal. Only one fragment is shown here):
public class MainActivity2 extends FragmentActivity{
private ViewPager mPager;
private FragmentStatePagerAdapter mPagerAdapter;
private Logger mLogger;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_container);
mLogger = new Logger();
mPager = (ViewPager)findViewById(R.id.pager);
mPagerAdapter = new SliderAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
... //goes on with activity, the adapter is set correctly..
The Logger fragment creates its view inflating this xml:
<TextView
android:id="#+id/LoggerLoTitle"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:ems="10"
android:layout_weight="2"
android:gravity="center"
android:textStyle="bold"
android:textColor="#000033"
android:textSize="16sp"
android:text="#string/logger"
android:layout_marginTop="30sp" />
<TextView
android:id="#+id/numberOutput"
android:textIsSelectable="true"
android:layout_width="0dp"
android:layout_weight="13"
android:layout_height="fill_parent"
android:gravity="top"
android:freezesText="true"
android:maxLines = "50"
android:layout_marginLeft="22dp"
android:scrollbars = "vertical"
android:layout_marginTop="45sp"/>
<Button
android:background="#80FFFFFF"
android:gravity="center"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="fill_parent"
android:freezesText="true"
android:scrollbars = "vertical"
android:layout_marginTop="45sp"
android:textColor="#000000"
android:textStyle="bold"
android:text="#string/logger_ad"
android:onClick="whatsNew"/>
</LinearLayout>
For API 7, everything works (all fragments are loaded and the slider works) fine except that the TextView "numberOutput" does not scroll and can't be selected. For API 15, 16, 17 it wors fine. Any idea?
Solved it by getting that TextView (numberOutput) at runtime and setting a movement method:
rootView = (ViewGroup) inflater.inflate(R.layout.dicelogger_lo, container, false);
mLoggerBody = (TextView)rootView.findViewById(R.id.numberOutput);
mLoggerBody.setMovementMethod(new ScrollingMovementMethod());
I did not find in the documentation that higher api levels automatically set any movement method.. so the why would be another question

Resources