在创建新应用并检查Use Android X artifacts
而未修改任何内容时,我的手机和仿真器上的应用都会崩溃。
我已要求我的朋友重复创建应用程序的方法,并且手机中的所有功能都可以正常工作。
这是我第一次使用Android X,我做错什么了吗?我正在快速运行Android Studio 3.3.0.20(Linux)。
2019-02-08 17:54:44.477 5487-5487/worshifter.com.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: worshifter.com.myapplication, PID: 5487
java.lang.RuntimeException: Unable to start activity ComponentInfo{worshifter.com.myapplication/worshifter.com.myapplication.MainActivity}: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class androidx.constraintlayout.widget.ConstraintLayout
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class androidx.constraintlayout.widget.ConstraintLayout
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class androidx.constraintlayout.widget.ConstraintLayout
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at android.view.LayoutInflater.createView(LayoutInflater.java:647)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at worshifter.com.myapplication.MainActivity.onCreate(MainActivity.java:12)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/constraintlayout/widget/R$styleable;
at androidx.constraintlayout.widget.ConstraintLayout.init(ConstraintLayout.java:590)
at androidx.constraintlayout.widget.ConstraintLayout.<init>(ConstraintLayout.java:567)
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at android.view.LayoutInflater.createView(LayoutInflater.java:647)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at worshifter.com.myapplication.MainActivity.onCreate(MainActivity.java:12)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2019-02-08 17:54:44.478 5487-5487/worshifter.com.myapplication E/AndroidRuntime: Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.constraintlayout.widget.R$styleable" on path: DexPathList[[zip file "/data/app/worshifter.com.myapplication-gnuqp7fLCQHjhJ5ZKGKRAQ==/base.apk"],nativeLibraryDirectories=[/data/app/worshifter.com.myapplication-gnuqp7fLCQHjhJ5ZKGKRAQ==/lib/x86, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
... 28 more
编辑发现,删除android.enableJetifier=true
时,该应用程序有效。
答案 0 :(得分:1)
我有同样的问题。
如您所见,它显示在日志中:let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let responseModel = try decoder.decode(Events.self, from: data) // data from response
您应该转到项目级别public class CustomScannerActivity : Activity, DecoratedBarcodeView.ITorchListener
{
public const int CUSTOMIZED_REQUEST_CODE = 1;
private CaptureManager capture;
private DecoratedBarcodeView barcodeScannerView;
private ImageButton switchFlashlightButton;
private ImageButton cameraButton;
private ViewfinderView viewfinderView;
TextView txtViewBottomText;
private bool torchOn;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
this.ActionBar.Hide();
SetContentView(Resource.Layout.activity_custom_scannerview);
barcodeScannerView = (DecoratedBarcodeView)FindViewById(Resource.Id.zxing_barcode_scanner);
barcodeScannerView.SetTorchListener(this);
var greenOverlay = (View)FindViewById(Resource.Id.view1);
Android.Util.DisplayMetrics displayMetrics = Resources.DisplayMetrics;
var scanWidth = (int)(displayMetrics.WidthPixels * 0.65);
barcodeScannerView.BarcodeView.FramingRectSize = new Size(scanWidth, 100);
greenOverlay.LayoutParameters.Width = scanWidth;
greenOverlay.LayoutParameters.Height = scanWidth;
CameraSettings settings = barcodeScannerView.BarcodeView.CameraSettings;
switchFlashlightButton = (ImageButton)FindViewById(Resource.Id.switch_flashlight);
cameraButton = (ImageButton)FindViewById(Resource.Id.camera);
viewfinderView = (ViewfinderView)FindViewById(Resource.Id.zxing_viewfinder_view);
txtViewBottomText = (TextView)FindViewById(Resource.Id.zxing_status_view);
txtViewBottomText.Text = AppResources.ScanAutomatically;
if (MainActivity.camPosition)
{
settings.RequestedCameraId = Convert.ToInt32(Android.Hardware.CameraFacing.Front);
switchFlashlightButton.Visibility = ViewStates.Gone;
barcodeScannerView.BarcodeView.CameraSettings = settings;
barcodeScannerView.Resume();
}
else
{
settings.RequestedCameraId = Convert.ToInt32(Android.Hardware.CameraFacing.Back);
switchFlashlightButton.Visibility = ViewStates.Visible;
barcodeScannerView.BarcodeView.CameraSettings = settings;
barcodeScannerView.Resume();
}
switchFlashlightButton.Click += (s, e) =>
{
if (!torchOn && !MainActivity.camPosition)
{
barcodeScannerView.SetTorchOn();
}
else
{
barcodeScannerView.SetTorchOff();
}
torchOn = !torchOn;
};
cameraButton.Click += (s, e) =>
{
//CameraSettings settings = barcodeScannerView.BarcodeView.CameraSettings;
if (barcodeScannerView.BarcodeView.IsPreviewActive)
{
barcodeScannerView.Pause();
}
//swap the id of the camera to be used
if (settings.RequestedCameraId == Convert.ToInt32(Android.Hardware.CameraFacing.Front))
{
settings.RequestedCameraId = Convert.ToInt32(Android.Hardware.CameraFacing.Back);
switchFlashlightButton.Visibility = ViewStates.Visible;
barcodeScannerView.SetTorchOff();
MainActivity.camPosition = false;
barcodeScannerView.BarcodeView.CameraSettings = settings;
barcodeScannerView.Resume();
}
else
{
settings.RequestedCameraId = Convert.ToInt32(Android.Hardware.CameraFacing.Front);
switchFlashlightButton.Visibility = ViewStates.Gone;
barcodeScannerView.SetTorchOff();
MainActivity.camPosition = true;
barcodeScannerView.BarcodeView.CameraSettings = settings;
barcodeScannerView.Resume();
}
};
// if the device does not have flashlight in its camera,
// then remove the switch flashlight button...
if (!hasFlash())
{
switchFlashlightButton.Visibility = ViewStates.Gone;
}
capture = new CaptureManager(this, barcodeScannerView);
capture.InitializeFromIntent(Intent, savedInstanceState);
capture.Decode();
}
protected override void OnResume()
{
base.OnResume();
if (capture != null)
{
capture.OnResume();
}
}
protected override void OnPause()
{
base.OnPause();
if (capture != null)
{
capture.OnPause();
}
}
protected override void OnDestroy()
{
base.OnDestroy();
if (capture != null)
{
capture.OnDestroy();
}
}
protected override void OnSaveInstanceState(Bundle outState)
{
base.OnSaveInstanceState(outState);
if (capture != null)
{
capture.OnSaveInstanceState(outState);
}
}
//public override bool OnKeyDown([GeneratedEnum] Keycode keyCode, KeyEvent e)
//{
// return barcodeScannerView.OnKeyDown(keyCode, e) || base.OnKeyDown(keyCode, e);
//}
private bool hasFlash()
{
return this.ApplicationContext.PackageManager.HasSystemFeature(Android.Content.PM.PackageManager.FeatureCameraFlash);//"android.hardware.camera.flash"
}
public void OnTorchOff()
{
}
public void OnTorchOn()
{
}
}
,并将ConstraintLayout更新为最新版本(并且最好从beta版本更新所有其他依赖项):
默认依赖项:
Error inflating class androidx.constraintlayout.widget.ConstraintLayout
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
实际版本:
build.gradle
我还创建了有关此问题的错误报告: https://issuetracker.google.com/issues/122892917
答案 1 :(得分:0)
解决方案非常简单,只需将库升级到build.gradle
文件中的最新版本即可。
提示:使用Android Studio的linting功能将库更新为最新版本(在Windows上为左Alt + Enter )
答案 2 :(得分:0)