我在Visual Studio中编写了一个Xamarin Android应用程序来扫描QR-Barcodes。我的项目中有两个布局和两个lctivity。当我启动应用程序时,模拟器加载并且应用程序正确启动而没有问题(主屏幕只是一个按钮,导致新的Intent,这是我的第二个布局/活动)。现在,当我单击按钮切换到下一个活动/布局时,我收到异常,应用程序崩溃。
异常发生在ActNewOrder.cs中,其中“ SetContentView(Resource.Layout.lay1Copy); ”是:
Unhandled Exception:
Android.Views.InflateException: <Timeout exceeded getting exception details>
我在互联网上找不到任何东西,所以我希望你们可以帮助我。
非常感谢你!
以下是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()
{
}
}
}
这是布局文件的代码,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>
答案 0 :(得分:2)
对于每个Android android:layout_width
和android:layout_height
都必须添加,即使是约束布局。
xml布局中的问题是从SurfaceView
开始,您的视图中只定义了android:width
和android:height
。
因此,要解决此问题,您需要将android:width
更改为android:layout_width
,将android:height
更改为android:layout_height
。
答案 1 :(得分:0)
我也面临同样的问题,我的布局不包含任何android:width和android:height属性。我错过了为我的活动添加主题。我通过以下步骤解决了这个问题:
使用以下代码在Resources \ values \ folder下创建styles.xml文件:
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
</style>
</resources>
在MainActivity.cs中定义Activity属性以利用AppTheme,如下所示:
[Activity(Label = "DinexFeedback", MainLauncher = true, Icon = "@drawable/icon",Theme ="@style/AppTheme")]
public class MainActivity : Android.Support.V7.App.AppCompatActivity
{
...
}
在此更新答案,因为这可能有助于其他人......
答案 2 :(得分:0)