I wrote a Xamarin Android app in Visual Studio to scan QR-Barcodes. I have two layouts and two lctivitys in my project. When i start the app, the emulator loads and the App starts correctly without a problem (The main screen is just one button that leads to a new Intent, which is my second layout/activity). Now when i click the button to switch to the next activity / layout, i get an exception, and the app crashes.
The exception happens in ActNewOrder.cs, where "SetContentView(Resource.Layout.lay1Copy);" is:
Unhandled Exception:
Android.Views.InflateException: <Timeout exceeded getting exception details>
i couldn't find anything on the internet, so i hope you guys can help me out.
Thank you very much in advance!
Here is my code for the ActNewOrder.cs:
using Android.App;
using Android.Widget;
using Android.OS;
using Android.Support.V7.App;
using Android.Views;
using Android.Gms.Vision.Barcodes;
using Android.Gms.Vision;
using Android.Graphics;
using Android.Runtime;
using System;
using Android.Support.V4.App;
using Android;
using Android.Content.PM;
using static Android.Gms.Vision.Detector;
using Android.Util;
using Android.Content;
namespace InstandhaltungApp
{
[Activity(Label = "ActNewOrder", Theme = "@style/Theme.AppCompat.Light.NoActionBar")]
public class ActNewOrder : AppCompatActivity, ISurfaceHolderCallback, IProcessor
{
SurfaceView cameraPreview;
TextView txtResult;
BarcodeDetector barcodeDetector;
CameraSource cameraSource;
const int RequestCameraPermissionID = 1001;
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Permission[] grantResults)
{
switch (requestCode)
{
case RequestCameraPermissionID:
{
if (grantResults[0] == Permission.Granted)
{
if (ActivityCompat.CheckSelfPermission(ApplicationContext, Manifest.Permission.Camera) != Android.Content.PM.Permission.Granted)
{
//Request permission
ActivityCompat.RequestPermissions(this, new string[]
{
Manifest.Permission.Camera
}, RequestCameraPermissionID);
return;
}
try
{
cameraSource.Start(cameraPreview.Holder);
}
catch (InvalidOperationException)
{
}
}
}
break;
}
}
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.lay1Copy);
cameraPreview = FindViewById<SurfaceView>(Resource.Id.cameraPreview);
txtResult = FindViewById<TextView>(Resource.Id.txtResult);
barcodeDetector = new BarcodeDetector.Builder(this)
.SetBarcodeFormats(BarcodeFormat.QrCode)
.Build();
cameraSource = new CameraSource
.Builder(this, barcodeDetector)
.SetRequestedPreviewSize(640, 480)
.Build();
cameraPreview.Holder.AddCallback(this);
barcodeDetector.SetProcessor(this);
}
public void SurfaceChanged(ISurfaceHolder holder, [GeneratedEnum] Format format, int width, int height)
{
}
public void SurfaceCreated(ISurfaceHolder holder)
{
if (ActivityCompat.CheckSelfPermission(ApplicationContext, Manifest.Permission.Camera) != Android.Content.PM.Permission.Granted)
{
//Request permission
ActivityCompat.RequestPermissions(this, new string[]
{
Manifest.Permission.Camera
}, RequestCameraPermissionID);
return;
}
try
{
cameraSource.Start(cameraPreview.Holder);
}
catch (InvalidOperationException)
{
}
}
public void SurfaceDestroyed(ISurfaceHolder holder)
{
cameraSource.Stop();
}
public void ReceiveDetections(Detections detections)
{
SparseArray qrcodes = detections.DetectedItems;
if (qrcodes.Size() != 0)
{
txtResult.Post(() => {
Vibrator vib = (Vibrator)GetSystemService(Context.VibratorService);
vib.Vibrate(1000);
txtResult.Text = ((Barcode)qrcodes.ValueAt(0)).RawValue;
});
}
}
public void Release()
{
}
}
}
And here is my code for the layout file, lay1Copy.xaml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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">
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/main_left_gl"
app:layout_constraintGuide_percent=".02"
android:orientation="vertical" />
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/main_right_gl"
app:layout_constraintGuide_percent=".98"
android:orientation="vertical" />
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button_left_gl"
app:layout_constraintGuide_percent=".48"
android:orientation="vertical" />
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button_right_gl"
app:layout_constraintGuide_percent=".52"
android:orientation="vertical" />
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/main_top_gl"
app:layout_constraintGuide_percent=".01"
android:orientation="horizontal" />
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/main_bottom_gl"
app:layout_constraintGuide_percent=".75"
android:orientation="horizontal" />
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/elem_bottom_gl"
app:layout_constraintGuide_percent=".85"
android:orientation="horizontal" />
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/elem_bottom_gl2"
app:layout_constraintGuide_percent="0.99"
android:orientation="horizontal" />
<SurfaceView
android:id="@+id/cameraPreview"
android:width="0dp"
android:height="0dp"
app:layout_constraintLeft_toLeftOf="@+id/main_left_gl"
app:layout_constraintRight_toRightOf="@+id/main_right_gl"
app:layout_constraintTop_toTopOf="@+id/main_top_gl"
app:layout_constraintBottom_toBottomOf="@+id/main_bottom_gl" />
<TextView
android:id="@+id/txtResult"
android:layout_centerInParent="true"
android:gravity="center_horizontal"
android:width="0dp"
android:height="0dp"
android:layout_marginTop="15dp"
android:textSize="20sp"
android:layout_below="@+id/cameraPreview"
android:text="Please focus Camera to QR Code"
app:layout_constraintLeft_toLeftOf="@+id/main_left_gl"
app:layout_constraintRight_toRightOf="@+id/main_right_gl"
app:layout_constraintTop_toTopOf="@+id/main_bottom_gl"
app:layout_constraintBottom_toBottomOf="@+id/elem_bottom_gl" />
<Button
android:id="@+id/btnSubmit"
android:text="Wagen ansehen"
android:width="0dp"
android:height="0dp"
app:layout_constraintLeft_toLeftOf="@+id/main_left_gl"
app:layout_constraintRight_toRightOf="@+id/button_left_gl"
app:layout_constraintTop_toTopOf="@+id/elem_bottom_gl"
app:layout_constraintBottom_toBottomOf="@+id/elem_bottom_gl2" />
<Button
android:id="@+id/btnReset"
android:text="Reset QR-Code"
android:width="0dp"
android:height="0dp"
app:layout_constraintLeft_toLeftOf="@+id/button_right_gl"
app:layout_constraintRight_toRightOf="@+id/main_right_gl"
app:layout_constraintTop_toTopOf="@+id/elem_bottom_gl"
app:layout_constraintBottom_toBottomOf="@+id/elem_bottom_gl2" />
</android.support.constraint.ConstraintLayout>
I am also face this same issue and my layout does not contain any android:width and android:height properties. I missed to add theme for my activity. I solved this issue by following steps:
Create a styles.xml file under Resources\values\ folder with below codes:
In MainActivity.cs define the Activity attribute to leverage AppTheme like this:
Updating the answer here because this might help others...
For every android
android:layout_width
andandroid:layout_height
must be added, even with constraint layout.The problem in your xml layout is starting from your
SurfaceView
, you only haveandroid:width
andandroid:height
defined in your views.So to resolve the problem, you need to change
android:width
toandroid:layout_width
andandroid:height
toandroid:layout_height
.Unhandled Exception: Android.Views.InflateException:
Make sure the necessary nuget package installed in visual studio.
I used card view in my project, i got this issue becoz i didn't install Xamarin.Android.Support.cardview.
so we should install all support nuget package in order to support all widgets here i use card view.
click on project -> Manage Nuget-> check out the installed pakages, if the package were not installed just search the particular package and then download it.
updated on 10/12/2018
check your layout whether all the layout fields are started with capital letter. for ex : In android we used <view> in small letter but In c# we should use <View> otherwise it throws the inflate view exception.